cdromutl.cc 5.6 KB

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