fileutl.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: fileutl.h,v 1.3 1999/10/24 06:53:12 jgg Exp $
  4. /* ######################################################################
  5. File Utilities
  6. CopyFile - Buffered copy of a single file
  7. GetLock - dpkg compatible lock file manipulation (fcntl)
  8. FileExists - Returns true if the file exists
  9. SafeGetCWD - Returns the CWD in a string with overrun protection
  10. The file class is a handy abstraction for various functions+classes
  11. that need to accept filenames.
  12. This source is placed in the Public Domain, do with it what you will
  13. It was originally written by Jason Gunthorpe.
  14. ##################################################################### */
  15. /*}}}*/
  16. #ifndef PKGLIB_FILEUTL_H
  17. #define PKGLIB_FILEUTL_H
  18. #ifdef __GNUG__
  19. #pragma interface "dsync/fileutl.h"
  20. #endif
  21. #include <string>
  22. using namespace std;
  23. class FileFd
  24. {
  25. protected:
  26. int iFd;
  27. enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
  28. HitEof = (1<<3)};
  29. unsigned long Flags;
  30. string FileName;
  31. public:
  32. enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny};
  33. bool Read(void *To,unsigned long Size,bool AllowEof = false);
  34. bool Write(const void *From,unsigned long Size);
  35. bool Seek(unsigned long To);
  36. bool Skip(unsigned long To);
  37. bool Truncate(unsigned long To);
  38. unsigned long Tell();
  39. unsigned long Size();
  40. bool Open(string FileName,OpenMode Mode,unsigned long Perms = 0666);
  41. bool Close();
  42. // Simple manipulators
  43. inline int Fd() {return iFd;};
  44. inline void Fd(int fd) {iFd = fd;};
  45. inline bool IsOpen() {return iFd >= 0;};
  46. inline bool Failed() {return (Flags & Fail) == Fail;};
  47. inline void EraseOnFailure() {Flags |= DelOnFail;};
  48. inline void OpFail() {Flags |= Fail;};
  49. inline bool Eof() {return (Flags & HitEof) == HitEof;};
  50. inline string &Name() {return FileName;};
  51. FileFd(string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1),
  52. Flags(0)
  53. {
  54. Open(FileName,Mode,Perms);
  55. };
  56. FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose) {};
  57. FileFd(int Fd,bool) : iFd(Fd), Flags(0) {};
  58. virtual ~FileFd();
  59. };
  60. bool CopyFile(FileFd &From,FileFd &To);
  61. int GetLock(string File,bool Errors = true);
  62. bool FileExists(string File);
  63. string SafeGetCWD();
  64. void SetCloseExec(int Fd,bool Close);
  65. void SetNonBlock(int Fd,bool Block);
  66. bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
  67. int ExecFork();
  68. bool ExecWait(int Pid,const char *Name,bool Reap = false);
  69. // File string manipulators
  70. string flNotDir(string File);
  71. string flNotFile(string File);
  72. string flNoLink(string File);
  73. #endif