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 #include <apt-pkg/aptconfiguration.h>
00026 
00027 #include <string>
00028 #include <vector>
00029 
00030 #include <zlib.h>
00031 
00032 #ifndef APT_8_CLEANER_HEADERS
00033 using std::string;
00034 #endif
00035 
00036 /* Define this for python-apt */
00037 #define APT_HAS_GZIP 1
00038 
00039 class FileFdPrivate;
00040 class FileFd
00041 {
00042    protected:
00043    int iFd;
00044  
00045    enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
00046                     HitEof = (1<<3), Replace = (1<<4), Compressed = (1<<5) };
00047    unsigned long Flags;
00048    std::string FileName;
00049    std::string TemporaryFileName;
00050 
00051    public:
00052    enum OpenMode {
00053         ReadOnly = (1 << 0),
00054         WriteOnly = (1 << 1),
00055         ReadWrite = ReadOnly | WriteOnly,
00056 
00057         Create = (1 << 2),
00058         Exclusive = (1 << 3),
00059         Atomic = Exclusive | (1 << 4),
00060         Empty = (1 << 5),
00061 
00062         WriteEmpty = ReadWrite | Create | Empty,
00063         WriteExists = ReadWrite,
00064         WriteAny = ReadWrite | Create,
00065         WriteTemp = ReadWrite | Create | Exclusive,
00066         ReadOnlyGzip,
00067         WriteAtomic = ReadWrite | Create | Atomic
00068    };
00069    enum CompressMode { Auto = 'A', None = 'N', Extension = 'E', Gzip = 'G', Bzip2 = 'B', Lzma = 'L', Xz = 'X' };
00070    
00071    inline bool Read(void *To,unsigned long long Size,bool AllowEof)
00072    {
00073       unsigned long long Jnk;
00074       if (AllowEof)
00075          return Read(To,Size,&Jnk);
00076       return Read(To,Size);
00077    }   
00078    bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0);
00079    char* ReadLine(char *To, unsigned long long const Size);
00080    bool Write(const void *From,unsigned long long Size);
00081    bool Seek(unsigned long long To);
00082    bool Skip(unsigned long long To);
00083    bool Truncate(unsigned long long To);
00084    unsigned long long Tell();
00085    unsigned long long Size();
00086    unsigned long long FileSize();
00087    time_t ModificationTime();
00088 
00089    /* You want to use 'unsigned long long' if you are talking about a file
00090       to be able to support large files (>2 or >4 GB) properly.
00091       This shouldn't happen all to often for the indexes, but deb's might be…
00092       And as the auto-conversation converts a 'unsigned long *' to a 'bool'
00093       instead of 'unsigned long long *' we need to provide this explicitely -
00094       otherwise applications magically start to fail… */
00095    __deprecated bool Read(void *To,unsigned long long Size,unsigned long *Actual)
00096    {
00097         unsigned long long R;
00098         bool const T = Read(To, Size, &R);
00099         *Actual = R;
00100         return T;
00101    }
00102 
00103    bool Open(std::string FileName,unsigned int const Mode,CompressMode Compress,unsigned long const Perms = 0666);
00104    bool Open(std::string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor,unsigned long const Perms = 0666);
00105    inline bool Open(std::string const &FileName,unsigned int const Mode, unsigned long const Perms = 0666) {
00106       return Open(FileName, Mode, None, Perms);
00107    };
00108    bool OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose=false);
00109    bool OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose=false);
00110    inline bool OpenDescriptor(int Fd, unsigned int const Mode, bool AutoClose=false) {
00111       return OpenDescriptor(Fd, Mode, None, AutoClose);
00112    };
00113    bool Close();
00114    bool Sync();
00115    
00116    // Simple manipulators
00117    inline int Fd() {return iFd;};
00118    inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);};
00119    __deprecated gzFile gzFd();
00120 
00121    inline bool IsOpen() {return iFd >= 0;};
00122    inline bool Failed() {return (Flags & Fail) == Fail;};
00123    inline void EraseOnFailure() {Flags |= DelOnFail;};
00124    inline void OpFail() {Flags |= Fail;};
00125    inline bool Eof() {return (Flags & HitEof) == HitEof;};
00126    inline bool IsCompressed() {return (Flags & Compressed) == Compressed;};
00127    inline std::string &Name() {return FileName;};
00128    
00129    FileFd(std::string FileName,unsigned int const Mode,unsigned long Perms = 0666) : iFd(-1), Flags(0), d(NULL)
00130    {
00131       Open(FileName,Mode, None, Perms);
00132    };
00133    FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long Perms = 0666) : iFd(-1), Flags(0), d(NULL)
00134    {
00135       Open(FileName,Mode, Compress, Perms);
00136    };
00137    FileFd() : iFd(-1), Flags(AutoClose), d(NULL) {};
00138    FileFd(int const Fd, unsigned int const Mode = ReadWrite, CompressMode Compress = None) : iFd(-1), Flags(0), d(NULL)
00139    {
00140       OpenDescriptor(Fd, Mode, Compress);
00141    };
00142    FileFd(int const Fd, bool const AutoClose) : iFd(-1), Flags(0), d(NULL)
00143    {
00144       OpenDescriptor(Fd, ReadWrite, None, AutoClose);
00145    };
00146    virtual ~FileFd();
00147 
00148    private:
00149    FileFdPrivate* d;
00150    bool OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor);
00151 };
00152 
00153 bool RunScripts(const char *Cnf);
00154 bool CopyFile(FileFd &From,FileFd &To);
00155 int GetLock(std::string File,bool Errors = true);
00156 bool FileExists(std::string File);
00157 bool RealFileExists(std::string File);
00158 bool DirectoryExists(std::string const &Path) __attrib_const;
00159 bool CreateDirectory(std::string const &Parent, std::string const &Path);
00160 time_t GetModificationTime(std::string const &Path);
00161 
00168 bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
00169 
00170 std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
00171                                         bool const &SortList, bool const &AllowNoExt=false);
00172 std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
00173                                         bool const &SortList);
00174 std::string SafeGetCWD();
00175 void SetCloseExec(int Fd,bool Close);
00176 void SetNonBlock(int Fd,bool Block);
00177 bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
00178 pid_t ExecFork();
00179 bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
00180 
00181 // File string manipulators
00182 std::string flNotDir(std::string File);
00183 std::string flNotFile(std::string File);
00184 std::string flNoLink(std::string File);
00185 std::string flExtension(std::string File);
00186 std::string flCombine(std::string Dir,std::string File);
00187 
00188 #endif