00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef PKGLIB_FILEUTL_H
00022 #define PKGLIB_FILEUTL_H
00023
00024 #include <apt-pkg/macros.h>
00025
00026 #include <string>
00027 #include <vector>
00028
00029 #include <zlib.h>
00030
00031
00032 #define APT_HAS_GZIP 1
00033
00034 using std::string;
00035
00036 class FileFd
00037 {
00038 protected:
00039 int iFd;
00040
00041 enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
00042 HitEof = (1<<3), Replace = (1<<4) };
00043 unsigned long Flags;
00044 string FileName;
00045 string TemporaryFileName;
00046 gzFile gz;
00047
00048 public:
00049 enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny,WriteTemp,ReadOnlyGzip,
00050 WriteAtomic};
00051
00052 inline bool Read(void *To,unsigned long Size,bool AllowEof)
00053 {
00054 unsigned long Jnk;
00055 if (AllowEof)
00056 return Read(To,Size,&Jnk);
00057 return Read(To,Size);
00058 }
00059 bool Read(void *To,unsigned long Size,unsigned long *Actual = 0);
00060 bool Write(const void *From,unsigned long Size);
00061 bool Seek(unsigned long To);
00062 bool Skip(unsigned long To);
00063 bool Truncate(unsigned long To);
00064 unsigned long Tell();
00065 unsigned long Size();
00066 bool Open(string FileName,OpenMode Mode,unsigned long Perms = 0666);
00067 bool OpenDescriptor(int Fd, OpenMode Mode, bool AutoClose=false);
00068 bool Close();
00069 bool Sync();
00070
00071
00072 inline int Fd() {return iFd;};
00073 inline void Fd(int fd) {iFd = fd;};
00074 inline bool IsOpen() {return iFd >= 0;};
00075 inline bool Failed() {return (Flags & Fail) == Fail;};
00076 inline void EraseOnFailure() {Flags |= DelOnFail;};
00077 inline void OpFail() {Flags |= Fail;};
00078 inline bool Eof() {return (Flags & HitEof) == HitEof;};
00079 inline string &Name() {return FileName;};
00080
00081 FileFd(string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1),
00082 Flags(0), gz(NULL)
00083 {
00084 Open(FileName,Mode,Perms);
00085 };
00086 FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose), gz(NULL) {};
00087 FileFd(int Fd,bool) : iFd(Fd), Flags(0), gz(NULL) {};
00088 virtual ~FileFd();
00089 };
00090
00091 bool RunScripts(const char *Cnf);
00092 bool CopyFile(FileFd &From,FileFd &To);
00093 int GetLock(string File,bool Errors = true);
00094 bool FileExists(string File);
00095 bool DirectoryExists(string const &Path) __attrib_const;
00096 bool CreateDirectory(string const &Parent, string const &Path);
00097
00104 bool CreateAPTDirectoryIfNeeded(string const &Parent, string const &Path);
00105
00106 std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
00107 bool const &SortList, bool const &AllowNoExt=false);
00108 std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext,
00109 bool const &SortList);
00110 string SafeGetCWD();
00111 void SetCloseExec(int Fd,bool Close);
00112 void SetNonBlock(int Fd,bool Block);
00113 bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
00114 pid_t ExecFork();
00115 bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
00116
00117
00118 string flNotDir(string File);
00119 string flNotFile(string File);
00120 string flNoLink(string File);
00121 string flExtension(string File);
00122 string flCombine(string Dir,string File);
00123
00124 #endif