apt  1.5
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 static Write(int Fd, const void *From, unsigned long long Size);
00082    bool Seek(unsigned long long To);
00083    bool Skip(unsigned long long To);
00084    bool Truncate(unsigned long long To);
00085    unsigned long long Tell();
00086    unsigned long long Size();
00087    unsigned long long FileSize();
00088    time_t ModificationTime();
00089 
00090    /* You want to use 'unsigned long long' if you are talking about a file
00091       to be able to support large files (>2 or >4 GB) properly.
00092       This shouldn't happen all to often for the indexes, but deb's might be…
00093       And as the auto-conversation converts a 'unsigned long *' to a 'bool'
00094       instead of 'unsigned long long *' we need to provide this explicitely -
00095       otherwise applications magically start to fail… */
00096    __deprecated bool Read(void *To,unsigned long long Size,unsigned long *Actual)
00097    {
00098         unsigned long long R;
00099         bool const T = Read(To, Size, &R);
00100         *Actual = R;
00101         return T;
00102    }
00103 
00104    bool Open(std::string FileName,unsigned int const Mode,CompressMode Compress,unsigned long const Perms = 0666);
00105    bool Open(std::string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor,unsigned long const Perms = 0666);
00106    inline bool Open(std::string const &FileName,unsigned int const Mode, unsigned long const Perms = 0666) {
00107       return Open(FileName, Mode, None, Perms);
00108    };
00109    bool OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose=false);
00110    bool OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose=false);
00111    inline bool OpenDescriptor(int Fd, unsigned int const Mode, bool AutoClose=false) {
00112       return OpenDescriptor(Fd, Mode, None, AutoClose);
00113    };
00114    bool Close();
00115    bool Sync();
00116    
00117    // Simple manipulators
00118    inline int Fd() {return iFd;};
00119    inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);};
00120    __deprecated gzFile gzFd();
00121 
00122    inline bool IsOpen() {return iFd >= 0;};
00123    inline bool Failed() {return (Flags & Fail) == Fail;};
00124    inline void EraseOnFailure() {Flags |= DelOnFail;};
00125    inline void OpFail() {Flags |= Fail;};
00126    inline bool Eof() {return (Flags & HitEof) == HitEof;};
00127    inline bool IsCompressed() {return (Flags & Compressed) == Compressed;};
00128    inline std::string &Name() {return FileName;};
00129    
00130    FileFd(std::string FileName,unsigned int const Mode,unsigned long Perms = 0666) : iFd(-1), Flags(0), d(NULL)
00131    {
00132       Open(FileName,Mode, None, Perms);
00133    };
00134    FileFd(std::string FileName,unsigned int const Mode, CompressMode Compress, unsigned long Perms = 0666) : iFd(-1), Flags(0), d(NULL)
00135    {
00136       Open(FileName,Mode, Compress, Perms);
00137    };
00138    FileFd() : iFd(-1), Flags(AutoClose), d(NULL) {};
00139    FileFd(int const Fd, unsigned int const Mode = ReadWrite, CompressMode Compress = None) : iFd(-1), Flags(0), d(NULL)
00140    {
00141       OpenDescriptor(Fd, Mode, Compress);
00142    };
00143    FileFd(int const Fd, bool const AutoClose) : iFd(-1), Flags(0), d(NULL)
00144    {
00145       OpenDescriptor(Fd, ReadWrite, None, AutoClose);
00146    };
00147    virtual ~FileFd();
00148 
00149    private:
00150    FileFdPrivate* d;
00151    bool OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor);
00152 };
00153 
00154 bool RunScripts(const char *Cnf);
00155 bool CopyFile(FileFd &From,FileFd &To);
00156 int GetLock(std::string File,bool Errors = true);
00157 bool FileExists(std::string File);
00158 bool RealFileExists(std::string File);
00159 bool DirectoryExists(std::string const &Path) __attrib_const;
00160 bool CreateDirectory(std::string const &Parent, std::string const &Path);
00161 time_t GetModificationTime(std::string const &Path);
00162 
00169 bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
00170 
00171 std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
00172                                         bool const &SortList, bool const &AllowNoExt=false);
00173 std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
00174                                         bool const &SortList);
00175 std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, bool SortList);
00176 std::string SafeGetCWD();
00177 void SetCloseExec(int Fd,bool Close);
00178 void SetNonBlock(int Fd,bool Block);
00179 bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
00180 pid_t ExecFork();
00181 bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
00182 
00183 // File string manipulators
00184 std::string flNotDir(std::string File);
00185 std::string flNotFile(std::string File);
00186 std::string flNoLink(std::string File);
00187 std::string flExtension(std::string File);
00188 std::string flCombine(std::string Dir,std::string File);
00189 
00190 #endif