]>
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 /*{{{*/
13 #include <apt-pkg/cdromutl.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/md5.h>
16 #include <apt-pkg/fileutl.h>
17 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/strutl.h>
23 #include <sys/errno.h>
24 #include <sys/statvfs.h>
32 // IsMounted - Returns true if the mount point is mounted /*{{{*/
33 // ---------------------------------------------------------------------
34 /* This is a simple algorithm that should always work, we stat the mount point
35 and the '..' file in the mount point and see if they are on the same device.
36 By definition if they are the same then it is not mounted. This should
37 account for symlinked mount points as well. */
38 bool IsMounted(string
&Path
)
40 if (Path
.empty() == true)
43 // Need that trailing slash for directories
44 if (Path
[Path
.length() - 1] != '/')
47 /* First we check if the path is actualy mounted, we do this by
48 stating the path and the previous directory (carefull of links!)
49 and comparing their device fields. */
51 if (stat(Path
.c_str(),&Buf
) != 0 ||
52 stat((Path
+ "../").c_str(),&Buf2
) != 0)
53 return _error
->Errno("stat",_("Unable to stat the mount point %s"),Path
.c_str());
55 if (Buf
.st_dev
== Buf2
.st_dev
)
60 // UnmountCdrom - Unmount a cdrom /*{{{*/
61 // ---------------------------------------------------------------------
62 /* Forking umount works much better than the umount syscall which can
63 leave /etc/mtab inconsitant. We drop all messages this produces. */
64 bool UnmountCdrom(string Path
)
66 if (IsMounted(Path
) == false)
72 int Child
= ExecFork();
77 // Make all the fds /dev/null
78 for (int I
= 0; I
!= 3; I
++)
79 dup2(open("/dev/null",O_RDWR
),I
);
81 if (_config
->Exists("Acquire::cdrom::"+Path
+"::UMount") == true)
83 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::UMount").c_str()) != 0)
91 Args
[1] = Path
.c_str();
93 execvp(Args
[0],(char **)Args
);
98 // if it can not be umounted, give it a bit more time
99 // this can happen when auto-mount magic or fs/cdrom prober attack
100 if (ExecWait(Child
,"umount",true) == true)
108 // MountCdrom - Mount a cdrom /*{{{*/
109 // ---------------------------------------------------------------------
110 /* We fork mount and drop all messages */
111 bool MountCdrom(string Path
, string DeviceName
)
113 if (IsMounted(Path
) == true)
116 int Child
= ExecFork();
121 // Make all the fds /dev/null
122 for (int I
= 0; I
!= 3; I
++)
123 dup2(open("/dev/null",O_RDWR
),I
);
125 if (_config
->Exists("Acquire::cdrom::"+Path
+"::Mount") == true)
127 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::Mount").c_str()) != 0)
133 const char *Args
[10];
135 if (DeviceName
== "")
137 Args
[1] = Path
.c_str();
140 Args
[1] = DeviceName
.c_str();
141 Args
[2] = Path
.c_str();
144 execvp(Args
[0],(char **)Args
);
150 return ExecWait(Child
,"mount",true);
153 // IdentCdrom - Generate a unique string for this CD /*{{{*/
154 // ---------------------------------------------------------------------
155 /* We convert everything we hash into a string, this prevents byte size/order
156 from effecting the outcome. */
157 bool IdentCdrom(string CD
,string
&Res
,unsigned int Version
)
160 bool writable_media
= false;
162 // if we are on a writable medium (like a usb-stick) that is just
163 // used like a cdrom don't use "." as it will constantly change,
165 if (access(CD
.c_str(), W_OK
) == 0 && DirectoryExists(CD
+string("/.disk")))
167 writable_media
= true;
168 CD
= CD
.append("/.disk");
169 if (_config
->FindB("Debug::aptcdrom",false) == true)
170 std::clog
<< "Found writable cdrom, using alternative path: " << CD
174 string StartDir
= SafeGetCWD();
175 if (chdir(CD
.c_str()) != 0)
176 return _error
->Errno("chdir",_("Unable to change to %s"),CD
.c_str());
178 DIR *D
= opendir(".");
180 return _error
->Errno("opendir",_("Unable to read %s"),CD
.c_str());
182 /* Run over the directory, we assume that the reader order will never
183 change as the media is read-only. In theory if the kernel did
184 some sort of wacked caching this might not be true.. */
186 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
189 if (strcmp(Dir
->d_name
,".") == 0 ||
190 strcmp(Dir
->d_name
,"..") == 0)
195 sprintf(S
,"%lu",(unsigned long)Dir
->d_ino
);
200 if (stat(Dir
->d_name
,&Buf
) != 0)
202 sprintf(S
,"%lu",(unsigned long)Buf
.st_mtime
);
206 Hash
.Add(Dir
->d_name
);
209 if (chdir(StartDir
.c_str()) != 0)
210 return _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
213 // Some stats from the fsys
214 if (_config
->FindB("Debug::identcdrom",false) == false)
217 if (statvfs(CD
.c_str(),&Buf
) != 0)
218 return _error
->Errno("statfs",_("Failed to stat the cdrom"));
220 // We use a kilobyte block size to advoid overflow
223 sprintf(S
,"%lu",(long)(Buf
.f_blocks
*(Buf
.f_bsize
/1024)));
225 sprintf(S
,"%lu %lu",(long)(Buf
.f_blocks
*(Buf
.f_bsize
/1024)),
226 (long)(Buf
.f_bfree
*(Buf
.f_bsize
/1024)));
229 sprintf(S
,"-%u",Version
);
232 sprintf(S
,"-%u.debug",Version
);
234 Res
= Hash
.Result().Value() + S
;
239 // FindMountPointForDevice - Find mountpoint for the given device /*{{{*/
240 string
FindMountPointForDevice(const char *devnode
)
246 // this is the order that mount uses as well
247 const char *mount
[] = { "/etc/mtab",
251 for (i
=0; mount
[i
] != NULL
; i
++) {
252 if (FileExists(mount
[i
])) {
253 FILE *f
=fopen(mount
[i
], "r");
254 while ( fgets(buf
, sizeof(buf
), f
) != NULL
) {
255 if (strncmp(buf
, devnode
, strlen(devnode
)) == 0) {
256 if(TokSplitString(' ', buf
, out
, 10))
257 return string(out
[1]);