]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cdromutl.cc
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>
23 #include <sys/statvfs.h>
35 // IsMounted - Returns true if the mount point is mounted /*{{{*/
36 // ---------------------------------------------------------------------
37 /* This is a simple algorithm that should always work, we stat the mount point
38 and the '..' file in the mount point and see if they are on the same device.
39 By definition if they are the same then it is not mounted. This should
40 account for symlinked mount points as well. */
41 bool IsMounted(string
&Path
)
43 if (Path
.empty() == true)
46 // Need that trailing slash for directories
47 if (Path
[Path
.length() - 1] != '/')
50 /* First we check if the path is actualy mounted, we do this by
51 stating the path and the previous directory (carefull of links!)
52 and comparing their device fields. */
54 if (stat(Path
.c_str(),&Buf
) != 0 ||
55 stat((Path
+ "../").c_str(),&Buf2
) != 0)
56 return _error
->Errno("stat",_("Unable to stat the mount point %s"),Path
.c_str());
58 if (Buf
.st_dev
== Buf2
.st_dev
)
63 // UnmountCdrom - Unmount a cdrom /*{{{*/
64 // ---------------------------------------------------------------------
65 /* Forking umount works much better than the umount syscall which can
66 leave /etc/mtab inconsitant. We drop all messages this produces. */
67 bool UnmountCdrom(string Path
)
69 if (IsMounted(Path
) == false)
75 int Child
= ExecFork();
80 // Make all the fds /dev/null
81 for (int I
= 0; I
!= 3; I
++)
82 dup2(open("/dev/null",O_RDWR
),I
);
84 if (_config
->Exists("Acquire::cdrom::"+Path
+"::UMount") == true)
86 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::UMount").c_str()) != 0)
94 Args
[1] = Path
.c_str();
96 execvp(Args
[0],(char **)Args
);
101 // if it can not be umounted, give it a bit more time
102 // this can happen when auto-mount magic or fs/cdrom prober attack
103 if (ExecWait(Child
,"umount",true) == true)
111 // MountCdrom - Mount a cdrom /*{{{*/
112 // ---------------------------------------------------------------------
113 /* We fork mount and drop all messages */
114 bool MountCdrom(string Path
, string DeviceName
)
116 if (IsMounted(Path
) == true)
119 int Child
= ExecFork();
124 // Make all the fds /dev/null
125 for (int I
= 0; I
!= 3; I
++)
126 dup2(open("/dev/null",O_RDWR
),I
);
128 if (_config
->Exists("Acquire::cdrom::"+Path
+"::Mount") == true)
130 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::Mount").c_str()) != 0)
136 const char *Args
[10];
138 if (DeviceName
== "")
140 Args
[1] = Path
.c_str();
143 Args
[1] = DeviceName
.c_str();
144 Args
[2] = Path
.c_str();
147 execvp(Args
[0],(char **)Args
);
153 return ExecWait(Child
,"mount",true);
156 // IdentCdrom - Generate a unique string for this CD /*{{{*/
157 // ---------------------------------------------------------------------
158 /* We convert everything we hash into a string, this prevents byte size/order
159 from effecting the outcome. */
160 bool IdentCdrom(string CD
,string
&Res
,unsigned int Version
)
163 bool writable_media
= false;
165 // if we are on a writable medium (like a usb-stick) that is just
166 // used like a cdrom don't use "." as it will constantly change,
168 if (access(CD
.c_str(), W_OK
) == 0 && DirectoryExists(CD
+string("/.disk")))
170 writable_media
= true;
171 CD
= CD
.append("/.disk");
172 if (_config
->FindB("Debug::aptcdrom",false) == true)
173 std::clog
<< "Found writable cdrom, using alternative path: " << CD
177 string StartDir
= SafeGetCWD();
178 if (chdir(CD
.c_str()) != 0)
179 return _error
->Errno("chdir",_("Unable to change to %s"),CD
.c_str());
181 DIR *D
= opendir(".");
183 return _error
->Errno("opendir",_("Unable to read %s"),CD
.c_str());
185 /* Run over the directory, we assume that the reader order will never
186 change as the media is read-only. In theory if the kernel did
187 some sort of wacked caching this might not be true.. */
189 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
192 if (strcmp(Dir
->d_name
,".") == 0 ||
193 strcmp(Dir
->d_name
,"..") == 0)
198 sprintf(S
,"%lu",(unsigned long)Dir
->d_ino
);
203 if (stat(Dir
->d_name
,&Buf
) != 0)
205 sprintf(S
,"%lu",(unsigned long)Buf
.st_mtime
);
209 Hash
.Add(Dir
->d_name
);
212 if (chdir(StartDir
.c_str()) != 0) {
213 _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
219 // Some stats from the fsys
220 if (_config
->FindB("Debug::identcdrom",false) == false)
223 if (statvfs(CD
.c_str(),&Buf
) != 0)
224 return _error
->Errno("statfs",_("Failed to stat the cdrom"));
226 // We use a kilobyte block size to advoid overflow
229 sprintf(S
,"%lu",(long)(Buf
.f_blocks
*(Buf
.f_bsize
/1024)));
231 sprintf(S
,"%lu %lu",(long)(Buf
.f_blocks
*(Buf
.f_bsize
/1024)),
232 (long)(Buf
.f_bfree
*(Buf
.f_bsize
/1024)));
235 sprintf(S
,"-%u",Version
);
238 sprintf(S
,"-%u.debug",Version
);
240 Res
= Hash
.Result().Value() + S
;
245 // FindMountPointForDevice - Find mountpoint for the given device /*{{{*/
246 string
FindMountPointForDevice(const char *devnode
)
252 // this is the order that mount uses as well
253 const char *mount
[] = { "/etc/mtab",
257 for (i
=0; mount
[i
] != NULL
; i
++) {
258 if (FileExists(mount
[i
])) {
259 FILE *f
=fopen(mount
[i
], "r");
260 while ( fgets(buf
, sizeof(buf
), f
) != NULL
) {
261 if (strncmp(buf
, devnode
, strlen(devnode
)) == 0) {
262 if(TokSplitString(' ', buf
, out
, 10))
265 // unescape the \0XXX chars in the path
266 string mount_point
= out
[1];
267 return DeEscapeString(mount_point
);