]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cdromutl.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cdromutl.cc,v 1.3 1999/04/03 01:05:24 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 /*{{{*/
14 #pragma implementation "apt-pkg/cdromutl.h"
16 #include <apt-pkg/cdromutl.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/md5.h>
19 #include <apt-pkg/fileutl.h>
20 #include <apt-pkg/configuration.h>
23 #include <sys/errno.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)
71 return _error
->Errno("fork","Failed to fork");
76 // Make all the fds /dev/null
77 for (int I
= 0; I
!= 10; I
++)
79 for (int I
= 0; I
!= 3; I
++)
80 dup2(open("/dev/null",O_RDWR
),I
);
82 if (_config
->Exists("Acquire::cdrom::"+Path
+"::UMount") == true)
84 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::UMount").c_str()) != 0)
92 Args
[1] = Path
.c_str();
94 execvp(Args
[0],(char **)Args
);
101 while (waitpid(Child
,&Status
,0) != Child
)
105 return _error
->Errno("waitpid","Couldn't wait for subprocess");
108 // Check for an error code.
109 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
114 // MountCdrom - Mount a cdrom /*{{{*/
115 // ---------------------------------------------------------------------
116 /* We fork mount and drop all messages */
117 bool MountCdrom(string Path
)
119 if (IsMounted(Path
) == true)
124 return _error
->Errno("fork","Failed to fork");
129 // Make all the fds /dev/null
130 for (int I
= 0; I
!= 10; I
++)
132 for (int I
= 0; I
!= 3; I
++)
133 dup2(open("/dev/null",O_RDWR
),I
);
135 if (_config
->Exists("Acquire::cdrom::"+Path
+"::Mount") == true)
137 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::Mount").c_str()) != 0)
143 const char *Args
[10];
145 Args
[1] = Path
.c_str();
147 execvp(Args
[0],(char **)Args
);
154 while (waitpid(Child
,&Status
,0) != Child
)
158 return _error
->Errno("waitpid","Couldn't wait for subprocess");
161 // Check for an error code.
162 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
167 // IdentCdrom - Generate a unique string for this CD /*{{{*/
168 // ---------------------------------------------------------------------
169 /* We convert everything we hash into a string, this prevents byte size/order
170 from effecting the outcome. */
171 bool IdentCdrom(string CD
,string
&Res
)
175 string StartDir
= SafeGetCWD();
176 if (chdir(CD
.c_str()) != 0)
177 return _error
->Errno("chdir","Unable to change to %s",CD
.c_str());
179 DIR *D
= opendir(".");
181 return _error
->Errno("opendir","Unable to read %s",CD
.c_str());
183 /* Run over the directory, we assume that the reader order will never
184 change as the media is read-only. In theory if the kernel did
185 some sort of wacked caching this might not be true.. */
187 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
190 if (strcmp(Dir
->d_name
,".") == 0 ||
191 strcmp(Dir
->d_name
,"..") == 0)
194 sprintf(S
,"%lu",Dir
->d_ino
);
196 Hash
.Add(Dir
->d_name
);
199 chdir(StartDir
.c_str());
202 // Some stats from the fsys
204 if (statfs(CD
.c_str(),&Buf
) != 0)
205 return _error
->Errno("statfs","Failed to stat the cdrom");
207 // We use a kilobyte block size to advoid overflow
208 sprintf(S
,"%u %u",Buf
.f_blocks
*(Buf
.f_bsize
/1024),
209 Buf
.f_bfree
*(Buf
.f_bsize
/1024));
212 Res
= Hash
.Result().Value();