cdromutl.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cdromutl.cc,v 1.3 1999/04/03 01:05:24 jgg Exp $
  4. /* ######################################################################
  5. CDROM Utilities - Some functions to manipulate CDROM mounts.
  6. These are here for the cdrom method and apt-cdrom.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #ifdef __GNUG__
  11. #pragma implementation "apt-pkg/cdromutl.h"
  12. #endif
  13. #include <apt-pkg/cdromutl.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/md5.h>
  16. #include <apt-pkg/fileutl.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <sys/wait.h>
  19. #include <sys/errno.h>
  20. #include <sys/vfs.h>
  21. #include <dirent.h>
  22. #include <fcntl.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <stdio.h>
  26. /*}}}*/
  27. // IsMounted - Returns true if the mount point is mounted /*{{{*/
  28. // ---------------------------------------------------------------------
  29. /* This is a simple algorithm that should always work, we stat the mount point
  30. and the '..' file in the mount point and see if they are on the same device.
  31. By definition if they are the same then it is not mounted. This should
  32. account for symlinked mount points as well. */
  33. bool IsMounted(string &Path)
  34. {
  35. if (Path.empty() == true)
  36. return false;
  37. // Need that trailing slash for directories
  38. if (Path[Path.length() - 1] != '/')
  39. Path += '/';
  40. /* First we check if the path is actualy mounted, we do this by
  41. stating the path and the previous directory (carefull of links!)
  42. and comparing their device fields. */
  43. struct stat Buf,Buf2;
  44. if (stat(Path.c_str(),&Buf) != 0 ||
  45. stat((Path + "../").c_str(),&Buf2) != 0)
  46. return _error->Errno("stat","Unable to stat the mount point %s",Path.c_str());
  47. if (Buf.st_dev == Buf2.st_dev)
  48. return false;
  49. return true;
  50. }
  51. /*}}}*/
  52. // UnmountCdrom - Unmount a cdrom /*{{{*/
  53. // ---------------------------------------------------------------------
  54. /* Forking umount works much better than the umount syscall which can
  55. leave /etc/mtab inconsitant. We drop all messages this produces. */
  56. bool UnmountCdrom(string Path)
  57. {
  58. if (IsMounted(Path) == false)
  59. return true;
  60. int Child = fork();
  61. if (Child < -1)
  62. return _error->Errno("fork","Failed to fork");
  63. // The child
  64. if (Child == 0)
  65. {
  66. // Make all the fds /dev/null
  67. for (int I = 0; I != 10; I++)
  68. close(I);
  69. for (int I = 0; I != 3; I++)
  70. dup2(open("/dev/null",O_RDWR),I);
  71. if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true)
  72. {
  73. if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0)
  74. _exit(100);
  75. _exit(0);
  76. }
  77. else
  78. {
  79. const char *Args[10];
  80. Args[0] = "umount";
  81. Args[1] = Path.c_str();
  82. Args[2] = 0;
  83. execvp(Args[0],(char **)Args);
  84. _exit(100);
  85. }
  86. }
  87. // Wait for mount
  88. int Status = 0;
  89. while (waitpid(Child,&Status,0) != Child)
  90. {
  91. if (errno == EINTR)
  92. continue;
  93. return _error->Errno("waitpid","Couldn't wait for subprocess");
  94. }
  95. // Check for an error code.
  96. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  97. return false;
  98. return true;
  99. }
  100. /*}}}*/
  101. // MountCdrom - Mount a cdrom /*{{{*/
  102. // ---------------------------------------------------------------------
  103. /* We fork mount and drop all messages */
  104. bool MountCdrom(string Path)
  105. {
  106. if (IsMounted(Path) == true)
  107. return true;
  108. int Child = fork();
  109. if (Child < -1)
  110. return _error->Errno("fork","Failed to fork");
  111. // The child
  112. if (Child == 0)
  113. {
  114. // Make all the fds /dev/null
  115. for (int I = 0; I != 10; I++)
  116. close(I);
  117. for (int I = 0; I != 3; I++)
  118. dup2(open("/dev/null",O_RDWR),I);
  119. if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true)
  120. {
  121. if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0)
  122. _exit(100);
  123. _exit(0);
  124. }
  125. else
  126. {
  127. const char *Args[10];
  128. Args[0] = "mount";
  129. Args[1] = Path.c_str();
  130. Args[2] = 0;
  131. execvp(Args[0],(char **)Args);
  132. _exit(100);
  133. }
  134. }
  135. // Wait for mount
  136. int Status = 0;
  137. while (waitpid(Child,&Status,0) != Child)
  138. {
  139. if (errno == EINTR)
  140. continue;
  141. return _error->Errno("waitpid","Couldn't wait for subprocess");
  142. }
  143. // Check for an error code.
  144. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  145. return false;
  146. return true;
  147. }
  148. /*}}}*/
  149. // IdentCdrom - Generate a unique string for this CD /*{{{*/
  150. // ---------------------------------------------------------------------
  151. /* We convert everything we hash into a string, this prevents byte size/order
  152. from effecting the outcome. */
  153. bool IdentCdrom(string CD,string &Res)
  154. {
  155. MD5Summation Hash;
  156. string StartDir = SafeGetCWD();
  157. if (chdir(CD.c_str()) != 0)
  158. return _error->Errno("chdir","Unable to change to %s",CD.c_str());
  159. DIR *D = opendir(".");
  160. if (D == 0)
  161. return _error->Errno("opendir","Unable to read %s",CD.c_str());
  162. /* Run over the directory, we assume that the reader order will never
  163. change as the media is read-only. In theory if the kernel did
  164. some sort of wacked caching this might not be true.. */
  165. char S[300];
  166. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  167. {
  168. // Skip some files..
  169. if (strcmp(Dir->d_name,".") == 0 ||
  170. strcmp(Dir->d_name,"..") == 0)
  171. continue;
  172. sprintf(S,"%lu",Dir->d_ino);
  173. Hash.Add(S);
  174. Hash.Add(Dir->d_name);
  175. };
  176. chdir(StartDir.c_str());
  177. closedir(D);
  178. // Some stats from the fsys
  179. struct statfs Buf;
  180. if (statfs(CD.c_str(),&Buf) != 0)
  181. return _error->Errno("statfs","Failed to stat the cdrom");
  182. // We use a kilobyte block size to advoid overflow
  183. sprintf(S,"%u %u",Buf.f_blocks*(Buf.f_bsize/1024),
  184. Buf.f_bfree*(Buf.f_bsize/1024));
  185. Hash.Add(S);
  186. Res = Hash.Result().Value();
  187. return true;
  188. }
  189. /*}}}*/