]>
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 /*{{{*/
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>
25 #include <sys/errno.h>
26 #include <sys/statvfs.h>
34 // IsMounted - Returns true if the mount point is mounted /*{{{*/
35 // ---------------------------------------------------------------------
36 /* This is a simple algorithm that should always work, we stat the mount point
37 and the '..' file in the mount point and see if they are on the same device.
38 By definition if they are the same then it is not mounted. This should
39 account for symlinked mount points as well. */
40 bool IsMounted(string
&Path
)
42 if (Path
.empty() == true)
45 // Need that trailing slash for directories
46 if (Path
[Path
.length() - 1] != '/')
49 /* First we check if the path is actualy mounted, we do this by
50 stating the path and the previous directory (carefull of links!)
51 and comparing their device fields. */
53 if (stat(Path
.c_str(),&Buf
) != 0 ||
54 stat((Path
+ "../").c_str(),&Buf2
) != 0)
55 return _error
->Errno("stat",_("Unable to stat the mount point %s"),Path
.c_str());
57 if (Buf
.st_dev
== Buf2
.st_dev
)
62 // UnmountCdrom - Unmount a cdrom /*{{{*/
63 // ---------------------------------------------------------------------
64 /* Forking umount works much better than the umount syscall which can
65 leave /etc/mtab inconsitant. We drop all messages this produces. */
66 bool UnmountCdrom(string Path
)
68 if (IsMounted(Path
) == false)
71 int Child
= ExecFork();
76 // Make all the fds /dev/null
77 for (int I
= 0; I
!= 3; I
++)
78 dup2(open("/dev/null",O_RDWR
),I
);
80 if (_config
->Exists("Acquire::cdrom::"+Path
+"::UMount") == true)
82 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::UMount").c_str()) != 0)
90 Args
[1] = Path
.c_str();
92 execvp(Args
[0],(char **)Args
);
98 return ExecWait(Child
,"umount",true);
101 // MountCdrom - Mount a cdrom /*{{{*/
102 // ---------------------------------------------------------------------
103 /* We fork mount and drop all messages */
104 bool MountCdrom(string Path
)
106 if (IsMounted(Path
) == true)
109 int Child
= ExecFork();
114 // Make all the fds /dev/null
115 for (int I
= 0; I
!= 3; I
++)
116 dup2(open("/dev/null",O_RDWR
),I
);
118 if (_config
->Exists("Acquire::cdrom::"+Path
+"::Mount") == true)
120 if (system(_config
->Find("Acquire::cdrom::"+Path
+"::Mount").c_str()) != 0)
126 const char *Args
[10];
128 Args
[1] = Path
.c_str();
130 execvp(Args
[0],(char **)Args
);
136 return ExecWait(Child
,"mount",true);
139 // IdentCdrom - Generate a unique string for this CD /*{{{*/
140 // ---------------------------------------------------------------------
141 /* We convert everything we hash into a string, this prevents byte size/order
142 from effecting the outcome. */
143 bool IdentCdrom(string CD
,string
&Res
,unsigned int Version
)
147 string StartDir
= SafeGetCWD();
148 if (chdir(CD
.c_str()) != 0)
149 return _error
->Errno("chdir",_("Unable to change to %s"),CD
.c_str());
151 DIR *D
= opendir(".");
153 return _error
->Errno("opendir",_("Unable to read %s"),CD
.c_str());
155 /* Run over the directory, we assume that the reader order will never
156 change as the media is read-only. In theory if the kernel did
157 some sort of wacked caching this might not be true.. */
159 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
162 if (strcmp(Dir
->d_name
,".") == 0 ||
163 strcmp(Dir
->d_name
,"..") == 0)
168 sprintf(S
,"%lu",(unsigned long)Dir
->d_ino
);
173 if (stat(Dir
->d_name
,&Buf
) != 0)
175 sprintf(S
,"%lu",(unsigned long)Buf
.st_mtime
);
179 Hash
.Add(Dir
->d_name
);
182 chdir(StartDir
.c_str());
185 // Some stats from the fsys
186 if (_config
->FindB("Debug::identcdrom",false) == false)
189 if (statvfs(CD
.c_str(),&Buf
) != 0)
190 return _error
->Errno("statfs",_("Failed to stat the cdrom"));
192 // We use a kilobyte block size to advoid overflow
193 sprintf(S
,"%lu %lu",(long)(Buf
.f_blocks
*(Buf
.f_bsize
/1024)),
194 (long)(Buf
.f_bfree
*(Buf
.f_bsize
/1024)));
196 sprintf(S
,"-%u",Version
);
199 sprintf(S
,"-%u.debug",Version
);
201 Res
= Hash
.Result().Value() + S
;