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