cdromutl.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <apt-pkg/strutl.h>
  16. #include <apti18n.h>
  17. #include <sys/wait.h>
  18. #include <sys/errno.h>
  19. #include <sys/statvfs.h>
  20. #include <dirent.h>
  21. #include <fcntl.h>
  22. #include <sys/stat.h>
  23. #include <unistd.h>
  24. #include <stdio.h>
  25. /*}}}*/
  26. // IsMounted - Returns true if the mount point is mounted /*{{{*/
  27. // ---------------------------------------------------------------------
  28. /* This is a simple algorithm that should always work, we stat the mount point
  29. and the '..' file in the mount point and see if they are on the same device.
  30. By definition if they are the same then it is not mounted. This should
  31. account for symlinked mount points as well. */
  32. bool IsMounted(string &Path)
  33. {
  34. if (Path.empty() == true)
  35. return false;
  36. // Need that trailing slash for directories
  37. if (Path[Path.length() - 1] != '/')
  38. Path += '/';
  39. /* First we check if the path is actualy mounted, we do this by
  40. stating the path and the previous directory (carefull of links!)
  41. and comparing their device fields. */
  42. struct stat Buf,Buf2;
  43. if (stat(Path.c_str(),&Buf) != 0 ||
  44. stat((Path + "../").c_str(),&Buf2) != 0)
  45. return _error->Errno("stat",_("Unable to stat the mount point %s"),Path.c_str());
  46. if (Buf.st_dev == Buf2.st_dev)
  47. return false;
  48. return true;
  49. }
  50. /*}}}*/
  51. // UnmountCdrom - Unmount a cdrom /*{{{*/
  52. // ---------------------------------------------------------------------
  53. /* Forking umount works much better than the umount syscall which can
  54. leave /etc/mtab inconsitant. We drop all messages this produces. */
  55. bool UnmountCdrom(string Path)
  56. {
  57. if (IsMounted(Path) == false)
  58. return true;
  59. for (int i=0;i<3;i++)
  60. {
  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. // if it can not be umounted, give it a bit more time
  85. // this can happen when auto-mount magic or fs/cdrom prober attack
  86. if (ExecWait(Child,"umount",true) == true)
  87. return true;
  88. sleep(1);
  89. }
  90. return false;
  91. }
  92. /*}}}*/
  93. // MountCdrom - Mount a cdrom /*{{{*/
  94. // ---------------------------------------------------------------------
  95. /* We fork mount and drop all messages */
  96. bool MountCdrom(string Path, string DeviceName)
  97. {
  98. if (IsMounted(Path) == true)
  99. return true;
  100. int Child = ExecFork();
  101. // The child
  102. if (Child == 0)
  103. {
  104. // Make all the fds /dev/null
  105. for (int I = 0; I != 3; I++)
  106. dup2(open("/dev/null",O_RDWR),I);
  107. if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true)
  108. {
  109. if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0)
  110. _exit(100);
  111. _exit(0);
  112. }
  113. else
  114. {
  115. const char *Args[10];
  116. Args[0] = "mount";
  117. if (DeviceName == "")
  118. {
  119. Args[1] = Path.c_str();
  120. Args[2] = 0;
  121. } else {
  122. Args[1] = DeviceName.c_str();
  123. Args[2] = Path.c_str();
  124. Args[3] = 0;
  125. }
  126. execvp(Args[0],(char **)Args);
  127. _exit(100);
  128. }
  129. }
  130. // Wait for mount
  131. return ExecWait(Child,"mount",true);
  132. }
  133. /*}}}*/
  134. // IdentCdrom - Generate a unique string for this CD /*{{{*/
  135. // ---------------------------------------------------------------------
  136. /* We convert everything we hash into a string, this prevents byte size/order
  137. from effecting the outcome. */
  138. bool IdentCdrom(string CD,string &Res,unsigned int Version)
  139. {
  140. MD5Summation Hash;
  141. bool writable_media = false;
  142. // if we are on a writable medium (like a usb-stick) that is just
  143. // used like a cdrom don't use "." as it will constantly change,
  144. // use .disk instead
  145. if (access(CD.c_str(), W_OK) == 0 && DirectoryExists(CD+string("/.disk")))
  146. {
  147. writable_media = true;
  148. CD = CD.append("/.disk");
  149. if (_config->FindB("Debug::aptcdrom",false) == true)
  150. std::clog << "Found writable cdrom, using alternative path: " << CD
  151. << std::endl;
  152. }
  153. string StartDir = SafeGetCWD();
  154. if (chdir(CD.c_str()) != 0)
  155. return _error->Errno("chdir",_("Unable to change to %s"),CD.c_str());
  156. DIR *D = opendir(".");
  157. if (D == 0)
  158. return _error->Errno("opendir",_("Unable to read %s"),CD.c_str());
  159. /* Run over the directory, we assume that the reader order will never
  160. change as the media is read-only. In theory if the kernel did
  161. some sort of wacked caching this might not be true.. */
  162. char S[300];
  163. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  164. {
  165. // Skip some files..
  166. if (strcmp(Dir->d_name,".") == 0 ||
  167. strcmp(Dir->d_name,"..") == 0)
  168. continue;
  169. if (Version <= 1)
  170. {
  171. sprintf(S,"%lu",(unsigned long)Dir->d_ino);
  172. }
  173. else
  174. {
  175. struct stat Buf;
  176. if (stat(Dir->d_name,&Buf) != 0)
  177. continue;
  178. sprintf(S,"%lu",(unsigned long)Buf.st_mtime);
  179. }
  180. Hash.Add(S);
  181. Hash.Add(Dir->d_name);
  182. };
  183. if (chdir(StartDir.c_str()) != 0)
  184. return _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str());
  185. closedir(D);
  186. // Some stats from the fsys
  187. if (_config->FindB("Debug::identcdrom",false) == false)
  188. {
  189. struct statvfs Buf;
  190. if (statvfs(CD.c_str(),&Buf) != 0)
  191. return _error->Errno("statfs",_("Failed to stat the cdrom"));
  192. // We use a kilobyte block size to advoid overflow
  193. if (writable_media)
  194. {
  195. sprintf(S,"%lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)));
  196. } else {
  197. sprintf(S,"%lu %lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)),
  198. (long)(Buf.f_bfree*(Buf.f_bsize/1024)));
  199. }
  200. Hash.Add(S);
  201. sprintf(S,"-%u",Version);
  202. }
  203. else
  204. sprintf(S,"-%u.debug",Version);
  205. Res = Hash.Result().Value() + S;
  206. return true;
  207. }
  208. /*}}}*/
  209. // FindMountPointForDevice - Find mountpoint for the given device /*{{{*/
  210. char* FindMountPointForDevice(const char *devnode)
  211. {
  212. char buf[255];
  213. char *out[10];
  214. int i=0;
  215. // this is the order that mount uses as well
  216. const char *mount[] = { "/etc/mtab",
  217. "/proc/mount",
  218. NULL };
  219. for (i=0; mount[i] != NULL; i++) {
  220. if (FileExists(mount[i])) {
  221. FILE *f=fopen(mount[i], "r");
  222. while ( fgets(buf, sizeof(buf), f) != NULL) {
  223. if (strncmp(buf, devnode, strlen(devnode)) == 0) {
  224. if(TokSplitString(' ', buf, out, 10))
  225. return strdup(out[1]);
  226. }
  227. }
  228. fclose(f);
  229. }
  230. }
  231. return NULL;
  232. }