cdromutl.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/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. for (int i=0;i<3;i++)
  59. {
  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. // if it can not be umounted, give it a bit more time
  84. // this can happen when auto-mount magic or fs/cdrom prober attack
  85. if (ExecWait(Child,"umount",true) == true)
  86. return true;
  87. sleep(1);
  88. }
  89. return false;
  90. }
  91. /*}}}*/
  92. // MountCdrom - Mount a cdrom /*{{{*/
  93. // ---------------------------------------------------------------------
  94. /* We fork mount and drop all messages */
  95. bool MountCdrom(string Path, string DeviceName)
  96. {
  97. if (IsMounted(Path) == true)
  98. return true;
  99. int Child = ExecFork();
  100. // The child
  101. if (Child == 0)
  102. {
  103. // Make all the fds /dev/null
  104. for (int I = 0; I != 3; I++)
  105. dup2(open("/dev/null",O_RDWR),I);
  106. if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true)
  107. {
  108. if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0)
  109. _exit(100);
  110. _exit(0);
  111. }
  112. else
  113. {
  114. const char *Args[10];
  115. Args[0] = "mount";
  116. if (DeviceName == "")
  117. {
  118. Args[1] = Path.c_str();
  119. Args[2] = 0;
  120. } else {
  121. Args[1] = DeviceName.c_str();
  122. Args[2] = Path.c_str();
  123. Args[3] = 0;
  124. }
  125. execvp(Args[0],(char **)Args);
  126. _exit(100);
  127. }
  128. }
  129. // Wait for mount
  130. return ExecWait(Child,"mount",true);
  131. }
  132. /*}}}*/
  133. // IdentCdrom - Generate a unique string for this CD /*{{{*/
  134. // ---------------------------------------------------------------------
  135. /* We convert everything we hash into a string, this prevents byte size/order
  136. from effecting the outcome. */
  137. bool IdentCdrom(string CD,string &Res,unsigned int Version)
  138. {
  139. MD5Summation Hash;
  140. bool writable_media = false;
  141. // if we are on a writable medium (like a usb-stick) that is just
  142. // used like a cdrom don't use "." as it will constantly change,
  143. // use .disk instead
  144. if (access(CD.c_str(), W_OK) == 0 && DirectoryExists(CD+string("/.disk")))
  145. {
  146. writable_media = true;
  147. CD = CD.append("/.disk");
  148. if (_config->FindB("Debug::aptcdrom",false) == true)
  149. std::clog << "Found writable cdrom, using alternative path: " << CD
  150. << std::endl;
  151. }
  152. string StartDir = SafeGetCWD();
  153. if (chdir(CD.c_str()) != 0)
  154. return _error->Errno("chdir",_("Unable to change to %s"),CD.c_str());
  155. DIR *D = opendir(".");
  156. if (D == 0)
  157. return _error->Errno("opendir",_("Unable to read %s"),CD.c_str());
  158. /* Run over the directory, we assume that the reader order will never
  159. change as the media is read-only. In theory if the kernel did
  160. some sort of wacked caching this might not be true.. */
  161. char S[300];
  162. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  163. {
  164. // Skip some files..
  165. if (strcmp(Dir->d_name,".") == 0 ||
  166. strcmp(Dir->d_name,"..") == 0)
  167. continue;
  168. if (Version <= 1)
  169. {
  170. sprintf(S,"%lu",(unsigned long)Dir->d_ino);
  171. }
  172. else
  173. {
  174. struct stat Buf;
  175. if (stat(Dir->d_name,&Buf) != 0)
  176. continue;
  177. sprintf(S,"%lu",(unsigned long)Buf.st_mtime);
  178. }
  179. Hash.Add(S);
  180. Hash.Add(Dir->d_name);
  181. };
  182. if (chdir(StartDir.c_str()) != 0) {
  183. _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str());
  184. closedir(D);
  185. return false;
  186. }
  187. closedir(D);
  188. // Some stats from the fsys
  189. if (_config->FindB("Debug::identcdrom",false) == false)
  190. {
  191. struct statvfs Buf;
  192. if (statvfs(CD.c_str(),&Buf) != 0)
  193. return _error->Errno("statfs",_("Failed to stat the cdrom"));
  194. // We use a kilobyte block size to advoid overflow
  195. if (writable_media)
  196. {
  197. sprintf(S,"%lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)));
  198. } else {
  199. sprintf(S,"%lu %lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)),
  200. (long)(Buf.f_bfree*(Buf.f_bsize/1024)));
  201. }
  202. Hash.Add(S);
  203. sprintf(S,"-%u",Version);
  204. }
  205. else
  206. sprintf(S,"-%u.debug",Version);
  207. Res = Hash.Result().Value() + S;
  208. return true;
  209. }
  210. /*}}}*/
  211. // FindMountPointForDevice - Find mountpoint for the given device /*{{{*/
  212. string FindMountPointForDevice(const char *devnode)
  213. {
  214. char buf[255];
  215. char *out[10];
  216. int i=0;
  217. // this is the order that mount uses as well
  218. const char *mount[] = { "/etc/mtab",
  219. "/proc/mount",
  220. NULL };
  221. for (i=0; mount[i] != NULL; i++) {
  222. if (FileExists(mount[i])) {
  223. FILE *f=fopen(mount[i], "r");
  224. while ( fgets(buf, sizeof(buf), f) != NULL) {
  225. if (strncmp(buf, devnode, strlen(devnode)) == 0) {
  226. if(TokSplitString(' ', buf, out, 10))
  227. {
  228. fclose(f);
  229. // unescape the \0XXX chars in the path
  230. string mount_point = out[1];
  231. return DeEscapeString(mount_point);
  232. }
  233. }
  234. }
  235. fclose(f);
  236. }
  237. }
  238. return string();
  239. }
  240. /*}}}*/