]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cdromutl.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cdromutl.cc,v 1.1 1998/11/29 01:19:27 jgg Exp $
4 /* ######################################################################
6 CDROM Utilities - Some functions to manipulate CDROM mounts.
8 These are here for the cdrom method and apt-cdrom.
10 ##################################################################### */
12 // Include Files /*{{{*/
14 #pragma implementation "apt-pkg/cdromutl.h"
16 #include <apt-pkg/cdromutl.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/md5.h>
19 #include <apt-pkg/fileutl.h>
22 #include <sys/errno.h>
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. */
34 bool UnmountCdrom(string Path
)
38 return _error
->Errno("fork","Failed to fork");
43 // Make all the fds /dev/null
44 for (int I
= 0; I
!= 10; I
++)
46 for (int I
= 0; I
!= 3; I
++)
47 dup2(open("/dev/null",O_RDWR
),I
);
51 Args
[1] = Path
.c_str();
53 execvp(Args
[0],(char **)Args
);
59 while (waitpid(Child
,&Status
,0) != Child
)
63 return _error
->Errno("waitpid","Couldn't wait for subprocess");
66 // Check for an error code.
67 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
72 // MountCdrom - Mount a cdrom /*{{{*/
73 // ---------------------------------------------------------------------
74 /* We fork mount and drop all messages */
75 bool MountCdrom(string Path
)
79 return _error
->Errno("fork","Failed to fork");
84 // Make all the fds /dev/null
85 for (int I
= 0; I
!= 10; I
++)
87 for (int I
= 0; I
!= 3; I
++)
88 dup2(open("/dev/null",O_RDWR
),I
);
92 Args
[1] = Path
.c_str();
94 execvp(Args
[0],(char **)Args
);
100 while (waitpid(Child
,&Status
,0) != Child
)
104 return _error
->Errno("waitpid","Couldn't wait for subprocess");
107 // Check for an error code.
108 if (WIFEXITED(Status
) == 0 || WEXITSTATUS(Status
) != 0)
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. */
117 bool IdentCdrom(string CD
,string
&Res
)
121 string StartDir
= SafeGetCWD();
122 if (chdir(CD
.c_str()) != 0)
123 return _error
->Errno("chdir","Unable to change to %s",CD
.c_str());
125 DIR *D
= opendir(".");
127 return _error
->Errno("opendir","Unable to read %s",CD
.c_str());
129 // Run over the directory
131 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
134 if (strcmp(Dir
->d_name
,".") == 0 ||
135 strcmp(Dir
->d_name
,"..") == 0)
138 sprintf(S
,"%lu",Dir
->d_ino
);
140 Hash
.Add(Dir
->d_name
);
143 chdir(StartDir
.c_str());
146 // Some stats from the fsys
148 if (statfs(CD
.c_str(),&Buf
) != 0)
149 return _error
->Errno("statfs","Failed to stat the cdrom");
151 sprintf(S
,"%u %u",Buf
.f_blocks
,Buf
.f_bfree
);
154 Res
= Hash
.Result().Value();