cdromutl.cc 5.8 KB

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