]>
Commit | Line | Data |
---|---|---|
d669751b AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
ddc1d8d0 | 3 | // $Id: cdromutl.cc,v 1.8 1999/07/26 17:46:08 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 /*{{{*/ | |
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> | |
6c907975 | 20 | #include <apt-pkg/configuration.h> |
d669751b AL |
21 | |
22 | #include <sys/wait.h> | |
23 | #include <sys/errno.h> | |
24 | #include <sys/vfs.h> | |
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. */ | |
38 | bool 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) | |
53 | return _error->Errno("stat","Unable to stat the mount point %s",Path.c_str()); | |
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. */ | |
64 | bool UnmountCdrom(string Path) | |
65 | { | |
66 | if (IsMounted(Path) == false) | |
4df0b629 AL |
67 | return true; |
68 | ||
54676e1a | 69 | int Child = ExecFork(); |
d669751b AL |
70 | |
71 | // The child | |
72 | if (Child == 0) | |
73 | { | |
74 | // Make all the fds /dev/null | |
d669751b AL |
75 | for (int I = 0; I != 3; I++) |
76 | dup2(open("/dev/null",O_RDWR),I); | |
77 | ||
6c907975 AL |
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 | } | |
d669751b AL |
93 | } |
94 | ||
95 | // Wait for mount | |
ddc1d8d0 | 96 | return ExecWait(Child,"mount",true); |
d669751b AL |
97 | } |
98 | /*}}}*/ | |
99 | // MountCdrom - Mount a cdrom /*{{{*/ | |
100 | // --------------------------------------------------------------------- | |
101 | /* We fork mount and drop all messages */ | |
102 | bool MountCdrom(string Path) | |
103 | { | |
6c907975 AL |
104 | if (IsMounted(Path) == true) |
105 | return true; | |
106 | ||
54676e1a | 107 | int Child = ExecFork(); |
d669751b AL |
108 | |
109 | // The child | |
110 | if (Child == 0) | |
111 | { | |
112 | // Make all the fds /dev/null | |
d669751b AL |
113 | for (int I = 0; I != 3; I++) |
114 | dup2(open("/dev/null",O_RDWR),I); | |
115 | ||
6c907975 AL |
116 | if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true) |
117 | { | |
118 | if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0) | |
119 | _exit(100); | |
120 | _exit(0); | |
121 | } | |
122 | else | |
123 | { | |
124 | const char *Args[10]; | |
125 | Args[0] = "mount"; | |
126 | Args[1] = Path.c_str(); | |
127 | Args[2] = 0; | |
128 | execvp(Args[0],(char **)Args); | |
129 | _exit(100); | |
130 | } | |
d669751b AL |
131 | } |
132 | ||
133 | // Wait for mount | |
ddc1d8d0 | 134 | return ExecWait(Child,"mount",true); |
d669751b AL |
135 | } |
136 | /*}}}*/ | |
137 | // IdentCdrom - Generate a unique string for this CD /*{{{*/ | |
138 | // --------------------------------------------------------------------- | |
139 | /* We convert everything we hash into a string, this prevents byte size/order | |
140 | from effecting the outcome. */ | |
34fc0421 | 141 | bool IdentCdrom(string CD,string &Res,unsigned int Version) |
d669751b AL |
142 | { |
143 | MD5Summation Hash; | |
144 | ||
145 | string StartDir = SafeGetCWD(); | |
146 | if (chdir(CD.c_str()) != 0) | |
147 | return _error->Errno("chdir","Unable to change to %s",CD.c_str()); | |
148 | ||
149 | DIR *D = opendir("."); | |
150 | if (D == 0) | |
151 | return _error->Errno("opendir","Unable to read %s",CD.c_str()); | |
152 | ||
4df0b629 AL |
153 | /* Run over the directory, we assume that the reader order will never |
154 | change as the media is read-only. In theory if the kernel did | |
155 | some sort of wacked caching this might not be true.. */ | |
d669751b AL |
156 | char S[300]; |
157 | for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D)) | |
158 | { | |
159 | // Skip some files.. | |
160 | if (strcmp(Dir->d_name,".") == 0 || | |
161 | strcmp(Dir->d_name,"..") == 0) | |
162 | continue; | |
34fc0421 AL |
163 | |
164 | if (Version <= 1) | |
165 | { | |
166 | sprintf(S,"%lu",Dir->d_ino); | |
167 | } | |
168 | else | |
169 | { | |
170 | struct stat Buf; | |
171 | if (stat(Dir->d_name,&Buf) != 0) | |
172 | continue; | |
173 | sprintf(S,"%lu",Buf.st_mtime); | |
174 | } | |
175 | ||
d669751b AL |
176 | Hash.Add(S); |
177 | Hash.Add(Dir->d_name); | |
178 | }; | |
179 | ||
180 | chdir(StartDir.c_str()); | |
181 | closedir(D); | |
182 | ||
183 | // Some stats from the fsys | |
fbdccabb AL |
184 | if (_config->FindB("Debug::identcdrom",false) == false) |
185 | { | |
186 | struct statfs Buf; | |
187 | if (statfs(CD.c_str(),&Buf) != 0) | |
188 | return _error->Errno("statfs","Failed to stat the cdrom"); | |
189 | ||
190 | // We use a kilobyte block size to advoid overflow | |
191 | sprintf(S,"%lu %lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)), | |
192 | (long)(Buf.f_bfree*(Buf.f_bsize/1024))); | |
193 | Hash.Add(S); | |
194 | sprintf(S,"-%u",Version); | |
195 | } | |
196 | else | |
197 | sprintf(S,"-%u.debug",Version); | |
d669751b | 198 | |
34fc0421 | 199 | Res = Hash.Result().Value() + S; |
d669751b AL |
200 | return true; |
201 | } | |
202 | /*}}}*/ |