]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/cdromutl.cc
merged -r1875..1886 from lp:~donkult/apt/sid
[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
AL
66 return true;
67
54676e1a 68 int Child = ExecFork();
d669751b
AL
69
70 // The child
71 if (Child == 0)
72 {
73 // Make all the fds /dev/null
d669751b
AL
74 for (int I = 0; I != 3; I++)
75 dup2(open("/dev/null",O_RDWR),I);
76
6c907975
AL
77 if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true)
78 {
79 if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0)
80 _exit(100);
81 _exit(0);
82 }
83 else
84 {
85 const char *Args[10];
86 Args[0] = "umount";
87 Args[1] = Path.c_str();
88 Args[2] = 0;
89 execvp(Args[0],(char **)Args);
90 _exit(100);
91 }
d669751b
AL
92 }
93
94 // Wait for mount
b2e465d6 95 return ExecWait(Child,"umount",true);
d669751b
AL
96}
97 /*}}}*/
98// MountCdrom - Mount a cdrom /*{{{*/
99// ---------------------------------------------------------------------
100/* We fork mount and drop all messages */
a6418a4b 101bool MountCdrom(string Path, string DeviceName)
d669751b 102{
6c907975
AL
103 if (IsMounted(Path) == true)
104 return true;
105
54676e1a 106 int Child = ExecFork();
d669751b
AL
107
108 // The child
109 if (Child == 0)
110 {
111 // Make all the fds /dev/null
d669751b
AL
112 for (int I = 0; I != 3; I++)
113 dup2(open("/dev/null",O_RDWR),I);
114
6c907975
AL
115 if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true)
116 {
117 if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0)
118 _exit(100);
119 _exit(0);
120 }
121 else
122 {
123 const char *Args[10];
124 Args[0] = "mount";
a6418a4b
MV
125 if (DeviceName == "")
126 {
127 Args[1] = Path.c_str();
128 Args[2] = 0;
129 } else {
130 Args[1] = DeviceName.c_str();
131 Args[2] = Path.c_str();
132 Args[3] = 0;
133 }
6c907975
AL
134 execvp(Args[0],(char **)Args);
135 _exit(100);
136 }
d669751b
AL
137 }
138
139 // Wait for mount
ddc1d8d0 140 return ExecWait(Child,"mount",true);
d669751b
AL
141}
142 /*}}}*/
143// IdentCdrom - Generate a unique string for this CD /*{{{*/
144// ---------------------------------------------------------------------
145/* We convert everything we hash into a string, this prevents byte size/order
146 from effecting the outcome. */
34fc0421 147bool IdentCdrom(string CD,string &Res,unsigned int Version)
d669751b
AL
148{
149 MD5Summation Hash;
150
151 string StartDir = SafeGetCWD();
152 if (chdir(CD.c_str()) != 0)
b2e465d6 153 return _error->Errno("chdir",_("Unable to change to %s"),CD.c_str());
d669751b
AL
154
155 DIR *D = opendir(".");
156 if (D == 0)
b2e465d6 157 return _error->Errno("opendir",_("Unable to read %s"),CD.c_str());
d669751b 158
4df0b629
AL
159 /* Run over the directory, we assume that the reader order will never
160 change as the media is read-only. In theory if the kernel did
161 some sort of wacked caching this might not be true.. */
d669751b
AL
162 char S[300];
163 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
164 {
165 // Skip some files..
166 if (strcmp(Dir->d_name,".") == 0 ||
167 strcmp(Dir->d_name,"..") == 0)
168 continue;
34fc0421
AL
169
170 if (Version <= 1)
171 {
0f297e46 172 sprintf(S,"%lu",(unsigned long)Dir->d_ino);
34fc0421
AL
173 }
174 else
175 {
176 struct stat Buf;
177 if (stat(Dir->d_name,&Buf) != 0)
178 continue;
1ae93c94 179 sprintf(S,"%lu",(unsigned long)Buf.st_mtime);
34fc0421
AL
180 }
181
d669751b
AL
182 Hash.Add(S);
183 Hash.Add(Dir->d_name);
184 };
185
9b5d79ec
MV
186 if (chdir(StartDir.c_str()) != 0)
187 return _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str());
d669751b
AL
188 closedir(D);
189
190 // Some stats from the fsys
fbdccabb
AL
191 if (_config->FindB("Debug::identcdrom",false) == false)
192 {
101030ab
AL
193 struct statvfs Buf;
194 if (statvfs(CD.c_str(),&Buf) != 0)
b2e465d6 195 return _error->Errno("statfs",_("Failed to stat the cdrom"));
fbdccabb
AL
196
197 // We use a kilobyte block size to advoid overflow
198 sprintf(S,"%lu %lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)),
199 (long)(Buf.f_bfree*(Buf.f_bsize/1024)));
200 Hash.Add(S);
201 sprintf(S,"-%u",Version);
202 }
203 else
204 sprintf(S,"-%u.debug",Version);
d669751b 205
34fc0421 206 Res = Hash.Result().Value() + S;
d669751b
AL
207 return true;
208}
209 /*}}}*/