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 class FileFd
00035 {
00036    protected:
00037    int iFd;
00038  
00039    enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2),
00040                     HitEof = (1<<3), Replace = (1<<4) };
00041    unsigned long Flags;
00042    std::string FileName;
00043    std::string TemporaryFileName;
00044    gzFile gz;
00045 
00046    public:
00047    enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny,WriteTemp,ReadOnlyGzip,
00048                   WriteAtomic};
00049    
00050    inline bool Read(void *To,unsigned long long Size,bool AllowEof)
00051    {
00052       unsigned long long Jnk;
00053       if (AllowEof)
00054          return Read(To,Size,&Jnk);
00055       return Read(To,Size);
00056    }   
00057    bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0);
00058    bool Write(const void *From,unsigned long long Size);
00059    bool Seek(unsigned long long To);
00060    bool Skip(unsigned long long To);
00061    bool Truncate(unsigned long long To);
00062    unsigned long long Tell();
00063    unsigned long long Size();
00064    unsigned long long FileSize();
00065 
00066    /* You want to use 'unsigned long long' if you are talking about a file
00067       to be able to support large files (>2 or >4 GB) properly.
00068       This shouldn't happen all to often for the indexes, but deb's might be…
00069       And as the auto-conversation converts a 'unsigned long *' to a 'bool'
00070       instead of 'unsigned long long *' we need to provide this explicitely -
00071       otherwise applications magically start to fail… */
00072    __deprecated bool Read(void *To,unsigned long long Size,unsigned long *Actual)
00073    {
00074         unsigned long long R;
00075         bool const T = Read(To, Size, &R);
00076         *Actual = R;
00077         return T;
00078    }
00079 
00080    bool Open(std::string FileName,OpenMode Mode,unsigned long Perms = 0666);
00081    bool OpenDescriptor(int Fd, OpenMode Mode, bool AutoClose=false);
00082    bool Close();
00083    bool Sync();
00084    
00085    // Simple manipulators
00086    inline int Fd() {return iFd;};
00087    inline void Fd(int fd) {iFd = fd;};
00088    inline gzFile gzFd() {return gz;};
00089    inline bool IsOpen() {return iFd >= 0;};
00090    inline bool Failed() {return (Flags & Fail) == Fail;};
00091    inline void EraseOnFailure() {Flags |= DelOnFail;};
00092    inline void OpFail() {Flags |= Fail;};
00093    inline bool Eof() {return (Flags & HitEof) == HitEof;};
00094    inline std::string &Name() {return FileName;};
00095    
00096    FileFd(std::string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1), 
00097             Flags(0), gz(NULL)
00098    {
00099       Open(FileName,Mode,Perms);
00100    };
00101    FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose), gz(NULL) {};
00102    FileFd(int Fd,bool) : iFd(Fd), Flags(0), gz(NULL) {};
00103    virtual ~FileFd();
00104 };
00105 
00106 bool RunScripts(const char *Cnf);
00107 bool CopyFile(FileFd &From,FileFd &To);
00108 int GetLock(std::string File,bool Errors = true);
00109 bool FileExists(std::string File);
00110 bool RealFileExists(std::string File);
00111 bool DirectoryExists(std::string const &Path) __attrib_const;
00112 bool CreateDirectory(std::string const &Parent, std::string const &Path);
00113 time_t GetModificationTime(std::string const &Path);
00114 
00121 bool CreateAPTDirectoryIfNeeded(std::string const &Parent, std::string const &Path);
00122 
00123 std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::string const &Ext,
00124                                         bool const &SortList, bool const &AllowNoExt=false);
00125 std::vector<std::string> GetListOfFilesInDir(std::string const &Dir, std::vector<std::string> const &Ext,
00126                                         bool const &SortList);
00127 std::string SafeGetCWD();
00128 void SetCloseExec(int Fd,bool Close);
00129 void SetNonBlock(int Fd,bool Block);
00130 bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
00131 pid_t ExecFork();
00132 bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
00133 
00134 // File string manipulators
00135 std::string flNotDir(std::string File);
00136 std::string flNotFile(std::string File);
00137 std::string flNoLink(std::string File);
00138 std::string flExtension(std::string File);
00139 std::string flCombine(std::string Dir,std::string File);
00140 
00141 #endif