]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cdromutl.cc
176cc2024c2c273deb624150498d4dce6e732e48
[apt.git] / apt-pkg / contrib / cdromutl.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cdromutl.cc,v 1.12 2001/02/20 07:03:17 jgg Exp $
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 /*{{{*/
13 #include<config.h>
14
15 #include <apt-pkg/cdromutl.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/md5.h>
18 #include <apt-pkg/fileutl.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/strutl.h>
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <iostream>
25 #include <string>
26 #include <sys/statvfs.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <stdio.h>
32
33 #include <apti18n.h>
34 /*}}}*/
35
36 using std::string;
37
38 // IsMounted - Returns true if the mount point is mounted /*{{{*/
39 // ---------------------------------------------------------------------
40 /* This is a simple algorithm that should always work, we stat the mount point
41 and the '..' file in the mount point and see if they are on the same device.
42 By definition if they are the same then it is not mounted. This should
43 account for symlinked mount points as well. */
44 bool IsMounted(string &Path)
45 {
46 if (Path.empty() == true)
47 return false;
48
49 // Need that trailing slash for directories
50 if (Path[Path.length() - 1] != '/')
51 Path += '/';
52
53 // if the path has a ".disk" directory we treat it as mounted
54 // this way even extracted copies of disks are recognized
55 if (DirectoryExists(Path + ".disk/") == true)
56 return true;
57
58 /* First we check if the path is actually mounted, we do this by
59 stating the path and the previous directory (careful of links!)
60 and comparing their device fields. */
61 struct stat Buf,Buf2;
62 if (stat(Path.c_str(),&Buf) != 0 ||
63 stat((Path + "../").c_str(),&Buf2) != 0)
64 return _error->Errno("stat",_("Unable to stat the mount point %s"),Path.c_str());
65
66 if (Buf.st_dev == Buf2.st_dev)
67 return false;
68 return true;
69 }
70 /*}}}*/
71 // UnmountCdrom - Unmount a cdrom /*{{{*/
72 // ---------------------------------------------------------------------
73 /* Forking umount works much better than the umount syscall which can
74 leave /etc/mtab inconsitant. We drop all messages this produces. */
75 bool UnmountCdrom(string Path)
76 {
77 if (IsMounted(Path) == false)
78 return true;
79
80 for (int i=0;i<3;i++)
81 {
82
83 int Child = ExecFork();
84
85 // The child
86 if (Child == 0)
87 {
88 // Make all the fds /dev/null
89 for (int I = 0; I != 3; I++)
90 dup2(open("/dev/null",O_RDWR),I);
91
92 if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true)
93 {
94 if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0)
95 _exit(100);
96 _exit(0);
97 }
98 else
99 {
100 const char *Args[10];
101 Args[0] = "umount";
102 Args[1] = Path.c_str();
103 Args[2] = 0;
104 execvp(Args[0],(char **)Args);
105 _exit(100);
106 }
107 }
108
109 // if it can not be umounted, give it a bit more time
110 // this can happen when auto-mount magic or fs/cdrom prober attack
111 if (ExecWait(Child,"umount",true) == true)
112 return true;
113 sleep(1);
114 }
115
116 return false;
117 }
118 /*}}}*/
119 // MountCdrom - Mount a cdrom /*{{{*/
120 // ---------------------------------------------------------------------
121 /* We fork mount and drop all messages */
122 bool MountCdrom(string Path, string DeviceName)
123 {
124 if (IsMounted(Path) == true)
125 return true;
126
127 int Child = ExecFork();
128
129 // The child
130 if (Child == 0)
131 {
132 // Make all the fds /dev/null
133 int null_fd = open("/dev/null",O_RDWR);
134 for (int I = 0; I != 3; I++)
135 dup2(null_fd, I);
136
137 if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true)
138 {
139 if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0)
140 _exit(100);
141 _exit(0);
142 }
143 else
144 {
145 const char *Args[10];
146 Args[0] = "mount";
147 if (DeviceName == "")
148 {
149 Args[1] = Path.c_str();
150 Args[2] = 0;
151 } else {
152 Args[1] = DeviceName.c_str();
153 Args[2] = Path.c_str();
154 Args[3] = 0;
155 }
156 execvp(Args[0],(char **)Args);
157 _exit(100);
158 }
159 }
160
161 // Wait for mount
162 return ExecWait(Child,"mount",true);
163 }
164 /*}}}*/
165 // IdentCdrom - Generate a unique string for this CD /*{{{*/
166 // ---------------------------------------------------------------------
167 /* We convert everything we hash into a string, this prevents byte size/order
168 from effecting the outcome. */
169 bool IdentCdrom(string CD,string &Res,unsigned int Version)
170 {
171 MD5Summation Hash;
172 bool writable_media = false;
173
174 // if we are on a writable medium (like a usb-stick) that is just
175 // used like a cdrom don't use "." as it will constantly change,
176 // use .disk instead
177 if (access(CD.c_str(), W_OK) == 0 && DirectoryExists(CD+string("/.disk")))
178 {
179 writable_media = true;
180 CD = CD.append("/.disk");
181 if (_config->FindB("Debug::aptcdrom",false) == true)
182 std::clog << "Found writable cdrom, using alternative path: " << CD
183 << std::endl;
184 }
185
186 string StartDir = SafeGetCWD();
187 if (chdir(CD.c_str()) != 0)
188 return _error->Errno("chdir",_("Unable to change to %s"),CD.c_str());
189
190 DIR *D = opendir(".");
191 if (D == 0)
192 return _error->Errno("opendir",_("Unable to read %s"),CD.c_str());
193
194 /* Run over the directory, we assume that the reader order will never
195 change as the media is read-only. In theory if the kernel did
196 some sort of wacked caching this might not be true.. */
197 char S[300];
198 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
199 {
200 // Skip some files..
201 if (strcmp(Dir->d_name,".") == 0 ||
202 strcmp(Dir->d_name,"..") == 0)
203 continue;
204
205 if (Version <= 1)
206 {
207 sprintf(S,"%lu",(unsigned long)Dir->d_ino);
208 }
209 else
210 {
211 struct stat Buf;
212 if (stat(Dir->d_name,&Buf) != 0)
213 continue;
214 sprintf(S,"%lu",(unsigned long)Buf.st_mtime);
215 }
216
217 Hash.Add(S);
218 Hash.Add(Dir->d_name);
219 };
220
221 if (chdir(StartDir.c_str()) != 0) {
222 _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str());
223 closedir(D);
224 return false;
225 }
226 closedir(D);
227
228 // Some stats from the fsys
229 if (_config->FindB("Debug::identcdrom",false) == false)
230 {
231 struct statvfs Buf;
232 if (statvfs(CD.c_str(),&Buf) != 0)
233 return _error->Errno("statfs",_("Failed to stat the cdrom"));
234
235 // We use a kilobyte block size to advoid overflow
236 if (writable_media)
237 {
238 sprintf(S,"%lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)));
239 } else {
240 sprintf(S,"%lu %lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)),
241 (long)(Buf.f_bfree*(Buf.f_bsize/1024)));
242 }
243 Hash.Add(S);
244 sprintf(S,"-%u",Version);
245 }
246 else
247 sprintf(S,"-%u.debug",Version);
248
249 Res = Hash.Result().Value() + S;
250 return true;
251 }
252 /*}}}*/
253
254 // FindMountPointForDevice - Find mountpoint for the given device /*{{{*/
255 string FindMountPointForDevice(const char *devnode)
256 {
257 char buf[255];
258 char *out[10];
259 int i=0;
260
261 // this is the order that mount uses as well
262 const char *mount[] = { "/etc/mtab",
263 "/proc/mount",
264 NULL };
265
266 for (i=0; mount[i] != NULL; i++) {
267 if (FileExists(mount[i])) {
268 FILE *f=fopen(mount[i], "r");
269 while ( fgets(buf, sizeof(buf), f) != NULL) {
270 if (strncmp(buf, devnode, strlen(devnode)) == 0) {
271 if(TokSplitString(' ', buf, out, 10))
272 {
273 fclose(f);
274 // unescape the \0XXX chars in the path
275 string mount_point = out[1];
276 return DeEscapeString(mount_point);
277 }
278 }
279 }
280 fclose(f);
281 }
282 }
283
284 return string();
285 }
286 /*}}}*/