]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/cdromutl.cc
apt-pkg/contrib/cdromutl.{cc,h}: return string for mountpath; apt-pkg/cdrom.cc: use...
[apt.git] / apt-pkg / contrib / cdromutl.cc
CommitLineData
d669751b
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b2e465d6 3// $Id: cdromutl.cc,v 1.12 2001/02/20 07:03:17 jgg Exp $
d669751b
AL
4/* ######################################################################
5
6 CDROM Utilities - Some functions to manipulate CDROM mounts.
7
8 These are here for the cdrom method and apt-cdrom.
9
10 ##################################################################### */
11 /*}}}*/
12// Include Files /*{{{*/
d669751b
AL
13#include <apt-pkg/cdromutl.h>
14#include <apt-pkg/error.h>
15#include <apt-pkg/md5.h>
16#include <apt-pkg/fileutl.h>
6c907975 17#include <apt-pkg/configuration.h>
ef381816 18#include <apt-pkg/strutl.h>
d669751b 19
b2e465d6
AL
20#include <apti18n.h>
21
d669751b
AL
22#include <sys/wait.h>
23#include <sys/errno.h>
101030ab 24#include <sys/statvfs.h>
d669751b
AL
25#include <dirent.h>
26#include <fcntl.h>
4df0b629 27#include <sys/stat.h>
d669751b
AL
28#include <unistd.h>
29#include <stdio.h>
30 /*}}}*/
31
6c907975 32// IsMounted - Returns true if the mount point is mounted /*{{{*/
d669751b 33// ---------------------------------------------------------------------
6c907975
AL
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. */
38bool IsMounted(string &Path)
d669751b 39{
4df0b629
AL
40 if (Path.empty() == true)
41 return false;
42
43 // Need that trailing slash for directories
44 if (Path[Path.length() - 1] != '/')
45 Path += '/';
46
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. */
50 struct stat Buf,Buf2;
51 if (stat(Path.c_str(),&Buf) != 0 ||
52 stat((Path + "../").c_str(),&Buf2) != 0)
b2e465d6 53 return _error->Errno("stat",_("Unable to stat the mount point %s"),Path.c_str());
4df0b629
AL
54
55 if (Buf.st_dev == Buf2.st_dev)
6c907975
AL
56 return false;
57 return true;
58}
59 /*}}}*/
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. */
64bool UnmountCdrom(string Path)
65{
66 if (IsMounted(Path) == false)
4df0b629 67 return true;
d669751b 68
5ae004ce 69 for (int i=0;i<3;i++)
d669751b 70 {
5ae004ce
MV
71
72 int Child = ExecFork();
d669751b 73
5ae004ce
MV
74 // The child
75 if (Child == 0)
6c907975 76 {
5ae004ce
MV
77 // Make all the fds /dev/null
78 for (int I = 0; I != 3; I++)
79 dup2(open("/dev/null",O_RDWR),I);
80
81 if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true)
82 {
83 if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0)
84 _exit(100);
85 _exit(0);
86 }
87 else
88 {
89 const char *Args[10];
90 Args[0] = "umount";
91 Args[1] = Path.c_str();
92 Args[2] = 0;
93 execvp(Args[0],(char **)Args);
6c907975 94 _exit(100);
5ae004ce 95 }
6c907975 96 }
5ae004ce
MV
97
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)
101 return true;
102 sleep(1);
d669751b
AL
103 }
104
5ae004ce 105 return false;
d669751b
AL
106}
107 /*}}}*/
108// MountCdrom - Mount a cdrom /*{{{*/
109// ---------------------------------------------------------------------
110/* We fork mount and drop all messages */
a6418a4b 111bool MountCdrom(string Path, string DeviceName)
d669751b 112{
6c907975
AL
113 if (IsMounted(Path) == true)
114 return true;
115
54676e1a 116 int Child = ExecFork();
d669751b
AL
117
118 // The child
119 if (Child == 0)
120 {
121 // Make all the fds /dev/null
d669751b
AL
122 for (int I = 0; I != 3; I++)
123 dup2(open("/dev/null",O_RDWR),I);
124
6c907975
AL
125 if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true)
126 {
127 if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0)
128 _exit(100);
129 _exit(0);
130 }
131 else
132 {
133 const char *Args[10];
134 Args[0] = "mount";
a6418a4b
MV
135 if (DeviceName == "")
136 {
137 Args[1] = Path.c_str();
138 Args[2] = 0;
139 } else {
140 Args[1] = DeviceName.c_str();
141 Args[2] = Path.c_str();
142 Args[3] = 0;
143 }
6c907975
AL
144 execvp(Args[0],(char **)Args);
145 _exit(100);
146 }
d669751b
AL
147 }
148
149 // Wait for mount
ddc1d8d0 150 return ExecWait(Child,"mount",true);
d669751b
AL
151}
152 /*}}}*/
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. */
34fc0421 157bool IdentCdrom(string CD,string &Res,unsigned int Version)
d669751b
AL
158{
159 MD5Summation Hash;
4dc7b4a7 160 bool writable_media = false;
d669751b 161
2a001232
MV
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,
164 // use .disk instead
4dc7b4a7
MV
165 if (access(CD.c_str(), W_OK) == 0 && DirectoryExists(CD+string("/.disk")))
166 {
167 writable_media = true;
7970157f 168 CD = CD.append("/.disk");
2a001232 169 if (_config->FindB("Debug::aptcdrom",false) == true)
7970157f
MV
170 std::clog << "Found writable cdrom, using alternative path: " << CD
171 << std::endl;
2a001232
MV
172 }
173
d669751b
AL
174 string StartDir = SafeGetCWD();
175 if (chdir(CD.c_str()) != 0)
b2e465d6 176 return _error->Errno("chdir",_("Unable to change to %s"),CD.c_str());
d669751b
AL
177
178 DIR *D = opendir(".");
179 if (D == 0)
b2e465d6 180 return _error->Errno("opendir",_("Unable to read %s"),CD.c_str());
d669751b 181
4df0b629
AL
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.. */
d669751b
AL
185 char S[300];
186 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
187 {
188 // Skip some files..
189 if (strcmp(Dir->d_name,".") == 0 ||
190 strcmp(Dir->d_name,"..") == 0)
191 continue;
34fc0421
AL
192
193 if (Version <= 1)
194 {
0f297e46 195 sprintf(S,"%lu",(unsigned long)Dir->d_ino);
34fc0421
AL
196 }
197 else
198 {
199 struct stat Buf;
200 if (stat(Dir->d_name,&Buf) != 0)
201 continue;
1ae93c94 202 sprintf(S,"%lu",(unsigned long)Buf.st_mtime);
34fc0421
AL
203 }
204
d669751b
AL
205 Hash.Add(S);
206 Hash.Add(Dir->d_name);
207 };
208
9b5d79ec
MV
209 if (chdir(StartDir.c_str()) != 0)
210 return _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str());
d669751b
AL
211 closedir(D);
212
213 // Some stats from the fsys
fbdccabb
AL
214 if (_config->FindB("Debug::identcdrom",false) == false)
215 {
101030ab
AL
216 struct statvfs Buf;
217 if (statvfs(CD.c_str(),&Buf) != 0)
b2e465d6 218 return _error->Errno("statfs",_("Failed to stat the cdrom"));
4dc7b4a7 219
fbdccabb 220 // We use a kilobyte block size to advoid overflow
4dc7b4a7
MV
221 if (writable_media)
222 {
223 sprintf(S,"%lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)));
224 } else {
225 sprintf(S,"%lu %lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)),
226 (long)(Buf.f_bfree*(Buf.f_bsize/1024)));
227 }
fbdccabb
AL
228 Hash.Add(S);
229 sprintf(S,"-%u",Version);
230 }
231 else
232 sprintf(S,"-%u.debug",Version);
d669751b 233
34fc0421 234 Res = Hash.Result().Value() + S;
d669751b
AL
235 return true;
236}
237 /*}}}*/
ef381816
MV
238
239// FindMountPointForDevice - Find mountpoint for the given device /*{{{*/
02aa6f67 240string FindMountPointForDevice(const char *devnode)
ef381816
MV
241{
242 char buf[255];
243 char *out[10];
244 int i=0;
245
246 // this is the order that mount uses as well
247 const char *mount[] = { "/etc/mtab",
248 "/proc/mount",
249 NULL };
250
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))
02aa6f67 257 return string(out[1]);
ef381816
MV
258 }
259 }
260 fclose(f);
261 }
262 }
263
02aa6f67 264 return string();
ef381816
MV
265}
266
267