apt @VERSION@
fileutl.h
00001 // -*- mode: cpp; mode: fold -*-
00002 // Description                                                          /*{{{*/
00003 // $Id: fileutl.h,v 1.26 2001/05/07 05:06:52 jgg Exp $
00004 /* ######################################################################
00005    
00006    File Utilities
00007    
00008    CopyFile - Buffered copy of a single file
00009    GetLock - dpkg compatible lock file manipulation (fcntl)
00010    FileExists - Returns true if the file exists
00011    SafeGetCWD - Returns the CWD in a string with overrun protection 
00012    
00013    The file class is a handy abstraction for various functions+classes
00014    that need to accept filenames.
00015    
00016    This source is placed in the Public Domain, do with it what you will
00017    It was originally written by Jason Gunthorpe.
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 /* Define this for python-apt */
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 long Size,bool AllowEof)
00053    {
00054       unsigned long long Jnk;
00055       if (AllowEof)
00056          return Read(To,Size,&Jnk);
00057       return Read(To,Size);
00058    }   
00059    bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0);
00060    bool Write(const void *From,unsigned long long Size);
00061    bool Seek(unsigned long long To);
00062    bool Skip(unsigned long long To);
00063    bool Truncate(unsigned long long To);
00064    unsigned long long Tell();
00065    unsigned long long Size();
00066    unsigned long long FileSize();
00067 
00068    /* You want to use 'unsigned long long' if you are talking about a file
00069       to be able to support large files (>2 or >4 GB) properly.
00070       This shouldn't happen all to often for the indexes, but deb's might be…
00071       And as the auto-conversation converts a 'unsigned long *' to a 'bool'
00072       instead of 'unsigned long long *' we need to provide this explicitely -
00073       otherwise applications magically start to fail… */
00074    __deprecated bool Read(void *To,unsigned long long Size,unsigned long *Actual)
00075    {
00076         unsigned long long R;
00077         bool const T = Read(To, Size, &R);
00078         *Actual = R;
00079         return T;
00080    }
00081 
00082    bool Open(string FileName,OpenMode Mode,unsigned long Perms = 0666);
00083    bool OpenDescriptor(int Fd, OpenMode Mode, bool AutoClose=false);
00084    bool Close();
00085    bool Sync();
00086    
00087    // Simple manipulators
00088    inline int Fd() {return iFd;};
00089    inline void Fd(int fd) {iFd = fd;};
00090    inline gzFile gzFd() {return gz;};
00091    inline bool IsOpen() {return iFd >= 0;};
00092    inline bool Failed() {return (Flags & Fail) == Fail;};
00093    inline void EraseOnFailure() {Flags |= DelOnFail;};
00094    inline void OpFail() {Flags |= Fail;};
00095    inline bool Eof() {return (Flags & HitEof) == HitEof;};
00096    inline string &Name() {return FileName;};
00097    
00098    FileFd(string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1), 
00099             Flags(0), gz(NULL)
00100    {
00101       Open(FileName,Mode,Perms);
00102    };
00103    FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose), gz(NULL) {};
00104    FileFd(int Fd,bool) : iFd(Fd), Flags(0), gz(NULL) {};
00105    virtual ~FileFd();
00106 };
00107 
00108 bool RunScripts(const char *Cnf);
00109 bool CopyFile(FileFd &From,FileFd &To);
00110 int GetLock(string File,bool Errors = true);
00111 bool FileExists(string File);
00112 bool RealFileExists(string File);
00113 bool DirectoryExists(string const &Path) __attrib_const;
00114 bool CreateDirectory(string const &Parent, string const &Path);
00115 time_t GetModificationTime(string const &Path);
00116 
00123 bool CreateAPTDirectoryIfNeeded(string const &Parent, string const &Path);
00124 
00125 std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
00126                                         bool const &SortList, bool const &AllowNoExt=false);
00127 std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext,
00128                                         bool const &SortList);
00129 string SafeGetCWD();
00130 void SetCloseExec(int Fd,bool Close);
00131 void SetNonBlock(int Fd,bool Block);
00132 bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
00133 pid_t ExecFork();
00134 bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
00135 
00136 // File string manipulators
00137 string flNotDir(string File);
00138 string flNotFile(string File);
00139 string flNoLink(string File);
00140 string flExtension(string File);
00141 string flCombine(string Dir,string File);
00142 
00143 #endif