]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cdromutl.cc
936e377fbfe6af2777bc2c6133e4eb945a6d552a
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cdromutl.cc,v 1.12 2001/02/20 07:03:17 jgg Exp $
4 /* ######################################################################
6 CDROM Utilities - Some functions to manipulate CDROM mounts.
8 These are here for the cdrom method and apt-cdrom.
10 ##################################################################### */
12 // Include Files /*{{{*/
15 #include <apt-pkg/cdromutl.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/md5.h>
18 #include <apt-pkg/fileutl.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/strutl.h>
27 #include <sys/statvfs.h>
39 // IsMounted - Returns true if the mount point is mounted /*{{{*/
40 // ---------------------------------------------------------------------
41 /* This is a simple algorithm that should always work, we stat the mount point
42 and the '..' file in the mount point and see if they are on the same device.
43 By definition if they are the same then it is not mounted. This should
44 account for symlinked mount points as well. */
45 bool IsMounted(string
&Path
)
47 if (Path
.empty() == true)
50 // Need that trailing slash for directories
51 if (Path
[Path
.length() - 1] != '/')
54 // if the path has a ".disk" directory we treat it as mounted
55 // this way even extracted copies of disks are recognized
56 if (DirectoryExists(Path
+ ".disk/") == true)
59 /* First we check if the path is actually mounted, we do this by
60 stating the path and the previous directory (careful of links!)
61 and comparing their device fields. */
63 if (stat(Path
.c_str(),&Buf
) != 0 ||
64 stat((Path
+ "../").c_str(),&Buf2
) != 0)
65 return _error
->Errno("stat",_("Unable to stat the mount point %s"),Path
.c_str());
67 if (Buf
.st_dev
== Buf2
.st_dev
)
72 // UnmountCdrom - Unmount a cdrom /*{{{*/
73 // ---------------------------------------------------------------------
74 /* Forking umount works much better than the umount syscall which can
75 leave /etc/mtab inconsitant. We drop all messages this produces. */
76 bool UnmountCdrom(string Path
)
78 // do not generate errors, even if the mountpoint does not exist
79 // the mountpoint might be auto-created by the mount command
80 // and a non-existing mountpoint is surely not mounted
81 _error
->PushToStack();
82 bool const mounted
= IsMounted(Path
);
83 _error
->RevertToStack();
90 int Child
= ExecFork();
95 // Make all the fds /dev/null
96 int const null_fd
= open("/dev/null",O_RDWR
);
97 for (int I
= 0; I
!= 3; ++I
)
100 if (_config
->Exists("Acquire::cdrom::"+Path
+"::UMount") == true)
102 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::UMount").c_str()) != 0)
108 const char *Args
[10];
110 Args
[1] = Path
.c_str();
112 execvp(Args
[0],(char **)Args
);
117 // if it can not be umounted, give it a bit more time
118 // this can happen when auto-mount magic or fs/cdrom prober attack
119 if (ExecWait(Child
,"umount",true) == true)
127 // MountCdrom - Mount a cdrom /*{{{*/
128 // ---------------------------------------------------------------------
129 /* We fork mount and drop all messages */
130 bool MountCdrom(string Path
, string DeviceName
)
132 // do not generate errors, even if the mountpoint does not exist
133 // the mountpoint might be auto-created by the mount command
134 _error
->PushToStack();
135 bool const mounted
= IsMounted(Path
);
136 _error
->RevertToStack();
140 int Child
= ExecFork();
145 // Make all the fds /dev/null
146 int const null_fd
= open("/dev/null",O_RDWR
);
147 for (int I
= 0; I
!= 3; ++I
)
150 if (_config
->Exists("Acquire::cdrom::"+Path
+"::Mount") == true)
152 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::Mount").c_str()) != 0)
158 const char *Args
[10];
160 if (DeviceName
== "")
162 Args
[1] = Path
.c_str();
165 Args
[1] = DeviceName
.c_str();
166 Args
[2] = Path
.c_str();
169 execvp(Args
[0],(char **)Args
);
175 return ExecWait(Child
,"mount",true);
178 // IdentCdrom - Generate a unique string for this CD /*{{{*/
179 // ---------------------------------------------------------------------
180 /* We convert everything we hash into a string, this prevents byte size/order
181 from effecting the outcome. */
182 bool IdentCdrom(string CD
,string
&Res
,unsigned int Version
)
185 bool writable_media
= false;
187 // if we are on a writable medium (like a usb-stick) that is just
188 // used like a cdrom don't use "." as it will constantly change,
190 if (access(CD
.c_str(), W_OK
) == 0 && DirectoryExists(CD
+string("/.disk")))
192 writable_media
= true;
193 CD
= CD
.append("/.disk");
194 if (_config
->FindB("Debug::aptcdrom",false) == true)
195 std::clog
<< "Found writable cdrom, using alternative path: " << CD
199 string StartDir
= SafeGetCWD();
200 if (chdir(CD
.c_str()) != 0)
201 return _error
->Errno("chdir",_("Unable to change to %s"),CD
.c_str());
203 DIR *D
= opendir(".");
205 return _error
->Errno("opendir",_("Unable to read %s"),CD
.c_str());
207 /* Run over the directory, we assume that the reader order will never
208 change as the media is read-only. In theory if the kernel did
209 some sort of wacked caching this might not be true.. */
211 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
214 if (strcmp(Dir
->d_name
,".") == 0 ||
215 strcmp(Dir
->d_name
,"..") == 0)
220 sprintf(S
,"%lu",(unsigned long)Dir
->d_ino
);
225 if (stat(Dir
->d_name
,&Buf
) != 0)
227 sprintf(S
,"%lu",(unsigned long)Buf
.st_mtime
);
231 Hash
.Add(Dir
->d_name
);
234 if (chdir(StartDir
.c_str()) != 0) {
235 _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
241 // Some stats from the fsys
242 if (_config
->FindB("Debug::identcdrom",false) == false)
245 if (statvfs(CD
.c_str(),&Buf
) != 0)
246 return _error
->Errno("statfs",_("Failed to stat the cdrom"));
248 // We use a kilobyte block size to advoid overflow
251 sprintf(S
,"%lu",(long)(Buf
.f_blocks
*(Buf
.f_bsize
/1024)));
253 sprintf(S
,"%lu %lu",(long)(Buf
.f_blocks
*(Buf
.f_bsize
/1024)),
254 (long)(Buf
.f_bfree
*(Buf
.f_bsize
/1024)));
257 sprintf(S
,"-%u",Version
);
260 sprintf(S
,"-%u.debug",Version
);
262 Res
= Hash
.Result().Value() + S
;
266 // FindMountPointForDevice - Find mountpoint for the given device /*{{{*/
267 string
FindMountPointForDevice(const char *devnode
)
269 // this is the order that mount uses as well
270 std::vector
<std::string
> const mounts
= _config
->FindVector("Dir::state::MountPoints", "/etc/mtab,/proc/mount");
272 for (std::vector
<std::string
>::const_iterator m
= mounts
.begin(); m
!= mounts
.end(); ++m
)
273 if (FileExists(*m
) == true)
277 FILE * f
= fopen(m
->c_str(), "r");
278 while(getline(&line
, &line_len
, f
) != -1)
280 char * out
[] = { NULL
, NULL
, NULL
};
281 TokSplitString(' ', line
, out
, 3);
282 if (out
[2] != NULL
|| out
[1] == NULL
|| out
[0] == NULL
)
284 if (strcmp(out
[0], devnode
) != 0)
287 // unescape the \0XXX chars in the path
288 string mount_point
= out
[1];
289 return DeEscapeString(mount_point
);