]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cdromutl.cc
Added a depends option to apt-cache
[apt.git] / apt-pkg / contrib / cdromutl.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cdromutl.cc,v 1.7 1999/07/02 22:21:01 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 #ifdef __GNUG__
14 #pragma implementation "apt-pkg/cdromutl.h"
15 #endif
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>
21
22 #include <sys/wait.h>
23 #include <sys/errno.h>
24 #include <sys/vfs.h>
25 #include <dirent.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 /*}}}*/
31
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)
39 {
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)
53 return _error->Errno("stat","Unable to stat the mount point %s",Path.c_str());
54
55 if (Buf.st_dev == Buf2.st_dev)
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. */
64 bool UnmountCdrom(string Path)
65 {
66 if (IsMounted(Path) == false)
67 return true;
68
69 int Child = ExecFork();
70
71 // The child
72 if (Child == 0)
73 {
74 // Make all the fds /dev/null
75 for (int I = 0; I != 3; I++)
76 dup2(open("/dev/null",O_RDWR),I);
77
78 if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true)
79 {
80 if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0)
81 _exit(100);
82 _exit(0);
83 }
84 else
85 {
86 const char *Args[10];
87 Args[0] = "umount";
88 Args[1] = Path.c_str();
89 Args[2] = 0;
90 execvp(Args[0],(char **)Args);
91 _exit(100);
92 }
93 }
94
95 // Wait for mount
96 int Status = 0;
97 while (waitpid(Child,&Status,0) != Child)
98 {
99 if (errno == EINTR)
100 continue;
101 return _error->Errno("waitpid","Couldn't wait for subprocess");
102 }
103
104 // Check for an error code.
105 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
106 return false;
107 return true;
108 }
109 /*}}}*/
110 // MountCdrom - Mount a cdrom /*{{{*/
111 // ---------------------------------------------------------------------
112 /* We fork mount and drop all messages */
113 bool MountCdrom(string Path)
114 {
115 if (IsMounted(Path) == true)
116 return true;
117
118 int Child = ExecFork();
119
120 // The child
121 if (Child == 0)
122 {
123 // Make all the fds /dev/null
124 for (int I = 0; I != 3; I++)
125 dup2(open("/dev/null",O_RDWR),I);
126
127 if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true)
128 {
129 if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0)
130 _exit(100);
131 _exit(0);
132 }
133 else
134 {
135 const char *Args[10];
136 Args[0] = "mount";
137 Args[1] = Path.c_str();
138 Args[2] = 0;
139 execvp(Args[0],(char **)Args);
140 _exit(100);
141 }
142 }
143
144 // Wait for mount
145 int Status = 0;
146 while (waitpid(Child,&Status,0) != Child)
147 {
148 if (errno == EINTR)
149 continue;
150 return _error->Errno("waitpid","Couldn't wait for subprocess");
151 }
152
153 // Check for an error code.
154 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
155 return false;
156 return true;
157 }
158 /*}}}*/
159 // IdentCdrom - Generate a unique string for this CD /*{{{*/
160 // ---------------------------------------------------------------------
161 /* We convert everything we hash into a string, this prevents byte size/order
162 from effecting the outcome. */
163 bool IdentCdrom(string CD,string &Res,unsigned int Version)
164 {
165 MD5Summation Hash;
166
167 string StartDir = SafeGetCWD();
168 if (chdir(CD.c_str()) != 0)
169 return _error->Errno("chdir","Unable to change to %s",CD.c_str());
170
171 DIR *D = opendir(".");
172 if (D == 0)
173 return _error->Errno("opendir","Unable to read %s",CD.c_str());
174
175 /* Run over the directory, we assume that the reader order will never
176 change as the media is read-only. In theory if the kernel did
177 some sort of wacked caching this might not be true.. */
178 char S[300];
179 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
180 {
181 // Skip some files..
182 if (strcmp(Dir->d_name,".") == 0 ||
183 strcmp(Dir->d_name,"..") == 0)
184 continue;
185
186 if (Version <= 1)
187 {
188 sprintf(S,"%lu",Dir->d_ino);
189 }
190 else
191 {
192 struct stat Buf;
193 if (stat(Dir->d_name,&Buf) != 0)
194 continue;
195 sprintf(S,"%lu",Buf.st_mtime);
196 }
197
198 Hash.Add(S);
199 Hash.Add(Dir->d_name);
200 };
201
202 chdir(StartDir.c_str());
203 closedir(D);
204
205 // Some stats from the fsys
206 if (_config->FindB("Debug::identcdrom",false) == false)
207 {
208 struct statfs Buf;
209 if (statfs(CD.c_str(),&Buf) != 0)
210 return _error->Errno("statfs","Failed to stat the cdrom");
211
212 // We use a kilobyte block size to advoid overflow
213 sprintf(S,"%lu %lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)),
214 (long)(Buf.f_bfree*(Buf.f_bsize/1024)));
215 Hash.Add(S);
216 sprintf(S,"-%u",Version);
217 }
218 else
219 sprintf(S,"-%u.debug",Version);
220
221 Res = Hash.Result().Value() + S;
222 return true;
223 }
224 /*}}}*/