]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/cdromutl.cc
apt-pkg/cdrom.{cc,h}: add udev_enumerate_add_match_sysattr to the libudev class
[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>
d669751b 18
b2e465d6
AL
19#include <apti18n.h>
20
d669751b
AL
21#include <sys/wait.h>
22#include <sys/errno.h>
101030ab 23#include <sys/statvfs.h>
d669751b
AL
24#include <dirent.h>
25#include <fcntl.h>
4df0b629 26#include <sys/stat.h>
d669751b
AL
27#include <unistd.h>
28#include <stdio.h>
29 /*}}}*/
30
6c907975 31// IsMounted - Returns true if the mount point is mounted /*{{{*/
d669751b 32// ---------------------------------------------------------------------
6c907975
AL
33/* This is a simple algorithm that should always work, we stat the mount point
34 and the '..' file in the mount point and see if they are on the same device.
35 By definition if they are the same then it is not mounted. This should
36 account for symlinked mount points as well. */
37bool IsMounted(string &Path)
d669751b 38{
4df0b629
AL
39 if (Path.empty() == true)
40 return false;
41
42 // Need that trailing slash for directories
43 if (Path[Path.length() - 1] != '/')
44 Path += '/';
45
46 /* First we check if the path is actualy mounted, we do this by
47 stating the path and the previous directory (carefull of links!)
48 and comparing their device fields. */
49 struct stat Buf,Buf2;
50 if (stat(Path.c_str(),&Buf) != 0 ||
51 stat((Path + "../").c_str(),&Buf2) != 0)
b2e465d6 52 return _error->Errno("stat",_("Unable to stat the mount point %s"),Path.c_str());
4df0b629
AL
53
54 if (Buf.st_dev == Buf2.st_dev)
6c907975
AL
55 return false;
56 return true;
57}
58 /*}}}*/
59// UnmountCdrom - Unmount a cdrom /*{{{*/
60// ---------------------------------------------------------------------
61/* Forking umount works much better than the umount syscall which can
62 leave /etc/mtab inconsitant. We drop all messages this produces. */
63bool UnmountCdrom(string Path)
64{
65 if (IsMounted(Path) == false)
4df0b629 66 return true;
d669751b 67
5ae004ce 68 for (int i=0;i<3;i++)
d669751b 69 {
5ae004ce
MV
70
71 int Child = ExecFork();
d669751b 72
5ae004ce
MV
73 // The child
74 if (Child == 0)
6c907975 75 {
5ae004ce
MV
76 // Make all the fds /dev/null
77 for (int I = 0; I != 3; I++)
78 dup2(open("/dev/null",O_RDWR),I);
79
80 if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true)
81 {
82 if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0)
83 _exit(100);
84 _exit(0);
85 }
86 else
87 {
88 const char *Args[10];
89 Args[0] = "umount";
90 Args[1] = Path.c_str();
91 Args[2] = 0;
92 execvp(Args[0],(char **)Args);
6c907975 93 _exit(100);
5ae004ce 94 }
6c907975 95 }
5ae004ce
MV
96
97 // if it can not be umounted, give it a bit more time
98 // this can happen when auto-mount magic or fs/cdrom prober attack
99 if (ExecWait(Child,"umount",true) == true)
100 return true;
101 sleep(1);
d669751b
AL
102 }
103
5ae004ce 104 return false;
d669751b
AL
105}
106 /*}}}*/
107// MountCdrom - Mount a cdrom /*{{{*/
108// ---------------------------------------------------------------------
109/* We fork mount and drop all messages */
a6418a4b 110bool MountCdrom(string Path, string DeviceName)
d669751b 111{
6c907975
AL
112 if (IsMounted(Path) == true)
113 return true;
114
54676e1a 115 int Child = ExecFork();
d669751b
AL
116
117 // The child
118 if (Child == 0)
119 {
120 // Make all the fds /dev/null
d669751b
AL
121 for (int I = 0; I != 3; I++)
122 dup2(open("/dev/null",O_RDWR),I);
123
6c907975
AL
124 if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true)
125 {
126 if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0)
127 _exit(100);
128 _exit(0);
129 }
130 else
131 {
132 const char *Args[10];
133 Args[0] = "mount";
a6418a4b
MV
134 if (DeviceName == "")
135 {
136 Args[1] = Path.c_str();
137 Args[2] = 0;
138 } else {
139 Args[1] = DeviceName.c_str();
140 Args[2] = Path.c_str();
141 Args[3] = 0;
142 }
6c907975
AL
143 execvp(Args[0],(char **)Args);
144 _exit(100);
145 }
d669751b
AL
146 }
147
148 // Wait for mount
ddc1d8d0 149 return ExecWait(Child,"mount",true);
d669751b
AL
150}
151 /*}}}*/
152// IdentCdrom - Generate a unique string for this CD /*{{{*/
153// ---------------------------------------------------------------------
154/* We convert everything we hash into a string, this prevents byte size/order
155 from effecting the outcome. */
34fc0421 156bool IdentCdrom(string CD,string &Res,unsigned int Version)
d669751b
AL
157{
158 MD5Summation Hash;
4dc7b4a7 159 bool writable_media = false;
d669751b 160
2a001232
MV
161 // if we are on a writable medium (like a usb-stick) that is just
162 // used like a cdrom don't use "." as it will constantly change,
163 // use .disk instead
4dc7b4a7
MV
164 if (access(CD.c_str(), W_OK) == 0 && DirectoryExists(CD+string("/.disk")))
165 {
166 writable_media = true;
7970157f 167 CD = CD.append("/.disk");
2a001232 168 if (_config->FindB("Debug::aptcdrom",false) == true)
7970157f
MV
169 std::clog << "Found writable cdrom, using alternative path: " << CD
170 << std::endl;
2a001232
MV
171 }
172
d669751b
AL
173 string StartDir = SafeGetCWD();
174 if (chdir(CD.c_str()) != 0)
b2e465d6 175 return _error->Errno("chdir",_("Unable to change to %s"),CD.c_str());
d669751b
AL
176
177 DIR *D = opendir(".");
178 if (D == 0)
b2e465d6 179 return _error->Errno("opendir",_("Unable to read %s"),CD.c_str());
d669751b 180
4df0b629
AL
181 /* Run over the directory, we assume that the reader order will never
182 change as the media is read-only. In theory if the kernel did
183 some sort of wacked caching this might not be true.. */
d669751b
AL
184 char S[300];
185 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
186 {
187 // Skip some files..
188 if (strcmp(Dir->d_name,".") == 0 ||
189 strcmp(Dir->d_name,"..") == 0)
190 continue;
34fc0421
AL
191
192 if (Version <= 1)
193 {
0f297e46 194 sprintf(S,"%lu",(unsigned long)Dir->d_ino);
34fc0421
AL
195 }
196 else
197 {
198 struct stat Buf;
199 if (stat(Dir->d_name,&Buf) != 0)
200 continue;
1ae93c94 201 sprintf(S,"%lu",(unsigned long)Buf.st_mtime);
34fc0421
AL
202 }
203
d669751b
AL
204 Hash.Add(S);
205 Hash.Add(Dir->d_name);
206 };
207
9b5d79ec
MV
208 if (chdir(StartDir.c_str()) != 0)
209 return _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str());
d669751b
AL
210 closedir(D);
211
212 // Some stats from the fsys
fbdccabb
AL
213 if (_config->FindB("Debug::identcdrom",false) == false)
214 {
101030ab
AL
215 struct statvfs Buf;
216 if (statvfs(CD.c_str(),&Buf) != 0)
b2e465d6 217 return _error->Errno("statfs",_("Failed to stat the cdrom"));
4dc7b4a7 218
fbdccabb 219 // We use a kilobyte block size to advoid overflow
4dc7b4a7
MV
220 if (writable_media)
221 {
222 sprintf(S,"%lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)));
223 } else {
224 sprintf(S,"%lu %lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)),
225 (long)(Buf.f_bfree*(Buf.f_bsize/1024)));
226 }
fbdccabb
AL
227 Hash.Add(S);
228 sprintf(S,"-%u",Version);
229 }
230 else
231 sprintf(S,"-%u.debug",Version);
d669751b 232
34fc0421 233 Res = Hash.Result().Value() + S;
d669751b
AL
234 return true;
235}
236 /*}}}*/