]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cdromutl.cc
176cc2024c2c273deb624150498d4dce6e732e48
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>
26 #include <sys/statvfs.h>
38 // IsMounted - Returns true if the mount point is mounted /*{{{*/
39 // ---------------------------------------------------------------------
40 /* This is a simple algorithm that should always work, we stat the mount point
41 and the '..' file in the mount point and see if they are on the same device.
42 By definition if they are the same then it is not mounted. This should
43 account for symlinked mount points as well. */
44 bool IsMounted(string
&Path
)
46 if (Path
.empty() == true)
49 // Need that trailing slash for directories
50 if (Path
[Path
.length() - 1] != '/')
53 // if the path has a ".disk" directory we treat it as mounted
54 // this way even extracted copies of disks are recognized
55 if (DirectoryExists(Path
+ ".disk/") == true)
58 /* First we check if the path is actually mounted, we do this by
59 stating the path and the previous directory (careful of links!)
60 and comparing their device fields. */
62 if (stat(Path
.c_str(),&Buf
) != 0 ||
63 stat((Path
+ "../").c_str(),&Buf2
) != 0)
64 return _error
->Errno("stat",_("Unable to stat the mount point %s"),Path
.c_str());
66 if (Buf
.st_dev
== Buf2
.st_dev
)
71 // UnmountCdrom - Unmount a cdrom /*{{{*/
72 // ---------------------------------------------------------------------
73 /* Forking umount works much better than the umount syscall which can
74 leave /etc/mtab inconsitant. We drop all messages this produces. */
75 bool UnmountCdrom(string Path
)
77 if (IsMounted(Path
) == false)
83 int Child
= ExecFork();
88 // Make all the fds /dev/null
89 for (int I
= 0; I
!= 3; I
++)
90 dup2(open("/dev/null",O_RDWR
),I
);
92 if (_config
->Exists("Acquire::cdrom::"+Path
+"::UMount") == true)
94 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::UMount").c_str()) != 0)
100 const char *Args
[10];
102 Args
[1] = Path
.c_str();
104 execvp(Args
[0],(char **)Args
);
109 // if it can not be umounted, give it a bit more time
110 // this can happen when auto-mount magic or fs/cdrom prober attack
111 if (ExecWait(Child
,"umount",true) == true)
119 // MountCdrom - Mount a cdrom /*{{{*/
120 // ---------------------------------------------------------------------
121 /* We fork mount and drop all messages */
122 bool MountCdrom(string Path
, string DeviceName
)
124 if (IsMounted(Path
) == true)
127 int Child
= ExecFork();
132 // Make all the fds /dev/null
133 int null_fd
= open("/dev/null",O_RDWR
);
134 for (int I
= 0; I
!= 3; I
++)
137 if (_config
->Exists("Acquire::cdrom::"+Path
+"::Mount") == true)
139 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::Mount").c_str()) != 0)
145 const char *Args
[10];
147 if (DeviceName
== "")
149 Args
[1] = Path
.c_str();
152 Args
[1] = DeviceName
.c_str();
153 Args
[2] = Path
.c_str();
156 execvp(Args
[0],(char **)Args
);
162 return ExecWait(Child
,"mount",true);
165 // IdentCdrom - Generate a unique string for this CD /*{{{*/
166 // ---------------------------------------------------------------------
167 /* We convert everything we hash into a string, this prevents byte size/order
168 from effecting the outcome. */
169 bool IdentCdrom(string CD
,string
&Res
,unsigned int Version
)
172 bool writable_media
= false;
174 // if we are on a writable medium (like a usb-stick) that is just
175 // used like a cdrom don't use "." as it will constantly change,
177 if (access(CD
.c_str(), W_OK
) == 0 && DirectoryExists(CD
+string("/.disk")))
179 writable_media
= true;
180 CD
= CD
.append("/.disk");
181 if (_config
->FindB("Debug::aptcdrom",false) == true)
182 std::clog
<< "Found writable cdrom, using alternative path: " << CD
186 string StartDir
= SafeGetCWD();
187 if (chdir(CD
.c_str()) != 0)
188 return _error
->Errno("chdir",_("Unable to change to %s"),CD
.c_str());
190 DIR *D
= opendir(".");
192 return _error
->Errno("opendir",_("Unable to read %s"),CD
.c_str());
194 /* Run over the directory, we assume that the reader order will never
195 change as the media is read-only. In theory if the kernel did
196 some sort of wacked caching this might not be true.. */
198 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
201 if (strcmp(Dir
->d_name
,".") == 0 ||
202 strcmp(Dir
->d_name
,"..") == 0)
207 sprintf(S
,"%lu",(unsigned long)Dir
->d_ino
);
212 if (stat(Dir
->d_name
,&Buf
) != 0)
214 sprintf(S
,"%lu",(unsigned long)Buf
.st_mtime
);
218 Hash
.Add(Dir
->d_name
);
221 if (chdir(StartDir
.c_str()) != 0) {
222 _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
228 // Some stats from the fsys
229 if (_config
->FindB("Debug::identcdrom",false) == false)
232 if (statvfs(CD
.c_str(),&Buf
) != 0)
233 return _error
->Errno("statfs",_("Failed to stat the cdrom"));
235 // We use a kilobyte block size to advoid overflow
238 sprintf(S
,"%lu",(long)(Buf
.f_blocks
*(Buf
.f_bsize
/1024)));
240 sprintf(S
,"%lu %lu",(long)(Buf
.f_blocks
*(Buf
.f_bsize
/1024)),
241 (long)(Buf
.f_bfree
*(Buf
.f_bsize
/1024)));
244 sprintf(S
,"-%u",Version
);
247 sprintf(S
,"-%u.debug",Version
);
249 Res
= Hash
.Result().Value() + S
;
254 // FindMountPointForDevice - Find mountpoint for the given device /*{{{*/
255 string
FindMountPointForDevice(const char *devnode
)
261 // this is the order that mount uses as well
262 const char *mount
[] = { "/etc/mtab",
266 for (i
=0; mount
[i
] != NULL
; i
++) {
267 if (FileExists(mount
[i
])) {
268 FILE *f
=fopen(mount
[i
], "r");
269 while ( fgets(buf
, sizeof(buf
), f
) != NULL
) {
270 if (strncmp(buf
, devnode
, strlen(devnode
)) == 0) {
271 if(TokSplitString(' ', buf
, out
, 10))
274 // unescape the \0XXX chars in the path
275 string mount_point
= out
[1];
276 return DeEscapeString(mount_point
);