cdromutl.cc 7.7 KB

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