fileutl.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: fileutl.cc,v 1.7 1998/08/26 04:52:26 jgg Exp $
  4. /* ######################################################################
  5. File Utilities
  6. CopyFile - Buffered copy of a single file
  7. GetLock - dpkg compatible lock file manipulation (fcntl)
  8. This source is placed in the Public Domain, do with it what you will
  9. It was originally written by Jason Gunthorpe.
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #ifdef __GNUG__
  14. #pragma implementation "apt-pkg/fileutl.h"
  15. #endif
  16. #include <apt-pkg/fileutl.h>
  17. #include <apt-pkg/error.h>
  18. #include <unistd.h>
  19. #include <sys/stat.h>
  20. #include <sys/fcntl.h>
  21. #include <sys/types.h>
  22. /*}}}*/
  23. // CopyFile - Buffered copy of a file /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* The caller is expected to set things so that failure causes erasure */
  26. bool CopyFile(FileFd From,FileFd To)
  27. {
  28. if (From.IsOpen() == false || To.IsOpen() == false)
  29. return false;
  30. // Buffered copy between fds
  31. unsigned char *Buf = new unsigned char[64000];
  32. long Size;
  33. while ((Size = read(From.Fd(),Buf,64000)) > 0)
  34. {
  35. if (To.Write(Buf,Size) == false)
  36. {
  37. delete [] Buf;
  38. return false;
  39. }
  40. }
  41. delete [] Buf;
  42. return true;
  43. }
  44. /*}}}*/
  45. // GetLock - Gets a lock file /*{{{*/
  46. // ---------------------------------------------------------------------
  47. /* This will create an empty file of the given name and lock it. Once this
  48. is done all other calls to GetLock in any other process will fail with
  49. -1. The return result is the fd of the file, the call should call
  50. close at some time. */
  51. int GetLock(string File,bool Errors)
  52. {
  53. int FD = open(File.c_str(),O_RDWR | O_CREAT | O_TRUNC,0640);
  54. if (FD < 0)
  55. {
  56. if (Errors == true)
  57. _error->Errno("open","Could not open lock file %s",File.c_str());
  58. return -1;
  59. }
  60. // Aquire a write lock
  61. struct flock fl;
  62. fl.l_type= F_WRLCK;
  63. fl.l_whence= SEEK_SET;
  64. fl.l_start= 0;
  65. fl.l_len= 1;
  66. if (fcntl(FD,F_SETLK,&fl) == -1)
  67. {
  68. if (Errors == true)
  69. _error->Errno("open","Could not get lock %s",File.c_str());
  70. close(FD);
  71. return -1;
  72. }
  73. return FD;
  74. }
  75. /*}}}*/
  76. // FileExists - Check if a file exists /*{{{*/
  77. // ---------------------------------------------------------------------
  78. /* */
  79. bool FileExists(string File)
  80. {
  81. struct stat Buf;
  82. if (stat(File.c_str(),&Buf) != 0)
  83. return false;
  84. return true;
  85. }
  86. /*}}}*/
  87. // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
  88. // ---------------------------------------------------------------------
  89. /* We return / on failure. */
  90. string SafeGetCWD()
  91. {
  92. // Stash the current dir.
  93. char S[300];
  94. S[0] = 0;
  95. if (getcwd(S,sizeof(S)) == 0)
  96. return "/";
  97. return S;
  98. }
  99. /*}}}*/
  100. // flNotDir - Strip the directory from the filename /*{{{*/
  101. // ---------------------------------------------------------------------
  102. /* */
  103. string flNotDir(string File)
  104. {
  105. string::size_type Res = File.rfind('/');
  106. if (Res == string::npos)
  107. return File;
  108. Res++;
  109. return string(File,Res,Res - File.length());
  110. }
  111. /*}}}*/
  112. // FileFd::FileFd - Open a file /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* The most commonly used open mode combinations are given with Mode */
  115. FileFd::FileFd(string FileName,OpenMode Mode, unsigned long Perms)
  116. {
  117. Flags = AutoClose;
  118. switch (Mode)
  119. {
  120. case ReadOnly:
  121. iFd = open(FileName.c_str(),O_RDONLY);
  122. break;
  123. case WriteEmpty:
  124. unlink(FileName.c_str());
  125. iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_EXCL,Perms);
  126. break;
  127. case WriteExists:
  128. iFd = open(FileName.c_str(),O_RDWR);
  129. break;
  130. // Dont use this in public directories
  131. case LockEmpty:
  132. iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_TRUNC,Perms);
  133. break;
  134. }
  135. if (iFd < 0)
  136. _error->Errno("open","Could not open file %s",FileName.c_str());
  137. else
  138. this->FileName = FileName;
  139. }
  140. /*}}}*/
  141. // FileFd::~File - Closes the file /*{{{*/
  142. // ---------------------------------------------------------------------
  143. /* If the proper modes are selected then we close the Fd and possibly
  144. unlink the file on error. */
  145. FileFd::~FileFd()
  146. {
  147. Close();
  148. }
  149. /*}}}*/
  150. // FileFd::Read - Read a bit of the file /*{{{*/
  151. // ---------------------------------------------------------------------
  152. /* */
  153. bool FileFd::Read(void *To,unsigned long Size)
  154. {
  155. if (read(iFd,To,Size) != (signed)Size)
  156. {
  157. Flags |= Fail;
  158. return _error->Errno("read","Read error");
  159. }
  160. return true;
  161. }
  162. /*}}}*/
  163. // FileFd::Write - Write to the file /*{{{*/
  164. // ---------------------------------------------------------------------
  165. /* */
  166. bool FileFd::Write(void *From,unsigned long Size)
  167. {
  168. if (write(iFd,From,Size) != (signed)Size)
  169. {
  170. Flags |= Fail;
  171. return _error->Errno("write","Write error");
  172. }
  173. return true;
  174. }
  175. /*}}}*/
  176. // FileFd::Seek - Seek in the file /*{{{*/
  177. // ---------------------------------------------------------------------
  178. /* */
  179. bool FileFd::Seek(unsigned long To)
  180. {
  181. if (lseek(iFd,To,SEEK_SET) != (signed)To)
  182. {
  183. Flags |= Fail;
  184. return _error->Error("Unable to seek to %u",To);
  185. }
  186. return true;
  187. }
  188. /*}}}*/
  189. // FileFd::Size - Return the size of the file /*{{{*/
  190. // ---------------------------------------------------------------------
  191. /* */
  192. unsigned long FileFd::Size()
  193. {
  194. struct stat Buf;
  195. if (fstat(iFd,&Buf) != 0)
  196. return _error->Errno("fstat","Unable to determine the file size");
  197. return Buf.st_size;
  198. }
  199. /*}}}*/
  200. // FileFd::Close - Close the file if the close flag is set /*{{{*/
  201. // ---------------------------------------------------------------------
  202. /* */
  203. bool FileFd::Close()
  204. {
  205. bool Res = true;
  206. if ((Flags & AutoClose) == AutoClose)
  207. if (iFd >= 0 && close(iFd) != 0)
  208. Res &= _error->Errno("close","Problem closing the file");
  209. iFd = -1;
  210. if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
  211. FileName.empty() == false)
  212. if (unlink(FileName.c_str()) != 0)
  213. Res &= _error->Warning("unlnk","Problem unlinking the file");
  214. return Res;
  215. }
  216. /*}}}*/