]>
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>
22 #include <sys/errno.h>
23 #include <sys/statvfs.h>
31 // IsMounted - Returns true if the mount point is mounted /*{{{*/
32 // ---------------------------------------------------------------------
33 /* This is a simple algorithm that should always work, we stat the mount point
34 and the '..' file in the mount point and see if they are on the same device.
35 By definition if they are the same then it is not mounted. This should
36 account for symlinked mount points as well. */
37 bool IsMounted(string
&Path
)
39 if (Path
.empty() == true)
42 // Need that trailing slash for directories
43 if (Path
[Path
.length() - 1] != '/')
46 /* First we check if the path is actualy mounted, we do this by
47 stating the path and the previous directory (carefull of links!)
48 and comparing their device fields. */
50 if (stat(Path
.c_str(),&Buf
) != 0 ||
51 stat((Path
+ "../").c_str(),&Buf2
) != 0)
52 return _error
->Errno("stat",_("Unable to stat the mount point %s"),Path
.c_str());
54 if (Buf
.st_dev
== Buf2
.st_dev
)
59 // UnmountCdrom - Unmount a cdrom /*{{{*/
60 // ---------------------------------------------------------------------
61 /* Forking umount works much better than the umount syscall which can
62 leave /etc/mtab inconsitant. We drop all messages this produces. */
63 bool UnmountCdrom(string Path
)
65 if (IsMounted(Path
) == false)
68 int Child
= ExecFork();
73 // Make all the fds /dev/null
74 for (int I
= 0; I
!= 3; I
++)
75 dup2(open("/dev/null",O_RDWR
),I
);
77 if (_config
->Exists("Acquire::cdrom::"+Path
+"::UMount") == true)
79 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::UMount").c_str()) != 0)
87 Args
[1] = Path
.c_str();
89 execvp(Args
[0],(char **)Args
);
95 return ExecWait(Child
,"umount",true);
98 // MountCdrom - Mount a cdrom /*{{{*/
99 // ---------------------------------------------------------------------
100 /* We fork mount and drop all messages */
101 bool MountCdrom(string Path
, string DeviceName
)
103 if (IsMounted(Path
) == true)
106 int Child
= ExecFork();
111 // Make all the fds /dev/null
112 for (int I
= 0; I
!= 3; I
++)
113 dup2(open("/dev/null",O_RDWR
),I
);
115 if (_config
->Exists("Acquire::cdrom::"+Path
+"::Mount") == true)
117 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::Mount").c_str()) != 0)
123 const char *Args
[10];
125 if (DeviceName
== "")
127 Args
[1] = Path
.c_str();
130 Args
[1] = DeviceName
.c_str();
131 Args
[2] = Path
.c_str();
134 execvp(Args
[0],(char **)Args
);
140 return ExecWait(Child
,"mount",true);
143 // IdentCdrom - Generate a unique string for this CD /*{{{*/
144 // ---------------------------------------------------------------------
145 /* We convert everything we hash into a string, this prevents byte size/order
146 from effecting the outcome. */
147 bool IdentCdrom(string CD
,string
&Res
,unsigned int Version
)
151 string StartDir
= SafeGetCWD();
152 if (chdir(CD
.c_str()) != 0)
153 return _error
->Errno("chdir",_("Unable to change to %s"),CD
.c_str());
155 DIR *D
= opendir(".");
157 return _error
->Errno("opendir",_("Unable to read %s"),CD
.c_str());
159 /* Run over the directory, we assume that the reader order will never
160 change as the media is read-only. In theory if the kernel did
161 some sort of wacked caching this might not be true.. */
163 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
166 if (strcmp(Dir
->d_name
,".") == 0 ||
167 strcmp(Dir
->d_name
,"..") == 0)
172 sprintf(S
,"%lu",(unsigned long)Dir
->d_ino
);
177 if (stat(Dir
->d_name
,&Buf
) != 0)
179 sprintf(S
,"%lu",(unsigned long)Buf
.st_mtime
);
183 Hash
.Add(Dir
->d_name
);
186 if (chdir(StartDir
.c_str()) != 0)
187 return _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
190 // Some stats from the fsys
191 if (_config
->FindB("Debug::identcdrom",false) == false)
194 if (statvfs(CD
.c_str(),&Buf
) != 0)
195 return _error
->Errno("statfs",_("Failed to stat the cdrom"));
197 // We use a kilobyte block size to advoid overflow
198 sprintf(S
,"%lu %lu",(long)(Buf
.f_blocks
*(Buf
.f_bsize
/1024)),
199 (long)(Buf
.f_bfree
*(Buf
.f_bsize
/1024)));
201 sprintf(S
,"-%u",Version
);
204 sprintf(S
,"-%u.debug",Version
);
206 Res
= Hash
.Result().Value() + S
;