cdromutl.cc 5.7 KB

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