fileutl.cc 5.7 KB

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