]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/cdromutl.cc
Test acquire failover
[apt.git] / apt-pkg / contrib / cdromutl.cc
CommitLineData
d669751b
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: cdromutl.cc,v 1.1 1998/11/29 01:19:27 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
21#include <sys/wait.h>
22#include <sys/errno.h>
23#include <sys/vfs.h>
24#include <dirent.h>
25#include <fcntl.h>
26#include <unistd.h>
27#include <stdio.h>
28 /*}}}*/
29
30// UnmountCdrom - Unmount a cdrom /*{{{*/
31// ---------------------------------------------------------------------
32/* Forking umount works much better than the umount syscall which can
33 leave /etc/mtab inconsitant. We drop all messages this produces. */
34bool UnmountCdrom(string Path)
35{
36 int Child = fork();
37 if (Child < -1)
38 return _error->Errno("fork","Failed to fork");
39
40 // The child
41 if (Child == 0)
42 {
43 // Make all the fds /dev/null
44 for (int I = 0; I != 10; I++)
45 close(I);
46 for (int I = 0; I != 3; I++)
47 dup2(open("/dev/null",O_RDWR),I);
48
49 const char *Args[10];
50 Args[0] = "umount";
51 Args[1] = Path.c_str();
52 Args[2] = 0;
53 execvp(Args[0],(char **)Args);
54 exit(100);
55 }
56
57 // Wait for mount
58 int Status = 0;
59 while (waitpid(Child,&Status,0) != Child)
60 {
61 if (errno == EINTR)
62 continue;
63 return _error->Errno("waitpid","Couldn't wait for subprocess");
64 }
65
66 // Check for an error code.
67 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
68 return false;
69 return true;
70}
71 /*}}}*/
72// MountCdrom - Mount a cdrom /*{{{*/
73// ---------------------------------------------------------------------
74/* We fork mount and drop all messages */
75bool MountCdrom(string Path)
76{
77 int Child = fork();
78 if (Child < -1)
79 return _error->Errno("fork","Failed to fork");
80
81 // The child
82 if (Child == 0)
83 {
84 // Make all the fds /dev/null
85 for (int I = 0; I != 10; I++)
86 close(I);
87 for (int I = 0; I != 3; I++)
88 dup2(open("/dev/null",O_RDWR),I);
89
90 const char *Args[10];
91 Args[0] = "mount";
92 Args[1] = Path.c_str();
93 Args[2] = 0;
94 execvp(Args[0],(char **)Args);
95 exit(100);
96 }
97
98 // Wait for mount
99 int Status = 0;
100 while (waitpid(Child,&Status,0) != Child)
101 {
102 if (errno == EINTR)
103 continue;
104 return _error->Errno("waitpid","Couldn't wait for subprocess");
105 }
106
107 // Check for an error code.
108 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
109 return false;
110 return true;
111}
112 /*}}}*/
113// IdentCdrom - Generate a unique string for this CD /*{{{*/
114// ---------------------------------------------------------------------
115/* We convert everything we hash into a string, this prevents byte size/order
116 from effecting the outcome. */
117bool IdentCdrom(string CD,string &Res)
118{
119 MD5Summation Hash;
120
121 string StartDir = SafeGetCWD();
122 if (chdir(CD.c_str()) != 0)
123 return _error->Errno("chdir","Unable to change to %s",CD.c_str());
124
125 DIR *D = opendir(".");
126 if (D == 0)
127 return _error->Errno("opendir","Unable to read %s",CD.c_str());
128
129 // Run over the directory
130 char S[300];
131 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
132 {
133 // Skip some files..
134 if (strcmp(Dir->d_name,".") == 0 ||
135 strcmp(Dir->d_name,"..") == 0)
136 continue;
137
138 sprintf(S,"%lu",Dir->d_ino);
139 Hash.Add(S);
140 Hash.Add(Dir->d_name);
141 };
142
143 chdir(StartDir.c_str());
144 closedir(D);
145
146 // Some stats from the fsys
147 struct statfs Buf;
148 if (statfs(CD.c_str(),&Buf) != 0)
149 return _error->Errno("statfs","Failed to stat the cdrom");
150
151 sprintf(S,"%u %u",Buf.f_blocks,Buf.f_bfree);
152 Hash.Add(S);
153
154 Res = Hash.Result().Value();
155 return true;
156}
157 /*}}}*/