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 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    unsigned long FileSize();
00067    bool Open(string FileName,OpenMode Mode,unsigned long Perms = 0666);
00068    bool OpenDescriptor(int Fd, OpenMode Mode, bool AutoClose=false);
00069    bool Close();
00070    bool Sync();
00071    
00072    // Simple manipulators
00073    inline int Fd() {return iFd;};
00074    inline void Fd(int fd) {iFd = fd;};
00075    inline gzFile gzFd() {return gz;};
00076    inline bool IsOpen() {return iFd >= 0;};
00077    inline bool Failed() {return (Flags & Fail) == Fail;};
00078    inline void EraseOnFailure() {Flags |= DelOnFail;};
00079    inline void OpFail() {Flags |= Fail;};
00080    inline bool Eof() {return (Flags & HitEof) == HitEof;};
00081    inline string &Name() {return FileName;};
00082    
00083    FileFd(string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1), 
00084             Flags(0), gz(NULL)
00085    {
00086       Open(FileName,Mode,Perms);
00087    };
00088    FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose), gz(NULL) {};
00089    FileFd(int Fd,bool) : iFd(Fd), Flags(0), gz(NULL) {};
00090    virtual ~FileFd();
00091 };
00092 
00093 bool RunScripts(const char *Cnf);
00094 bool CopyFile(FileFd &From,FileFd &To);
00095 int GetLock(string File,bool Errors = true);
00096 bool FileExists(string File);
00097 bool RealFileExists(string File);
00098 bool DirectoryExists(string const &Path) __attrib_const;
00099 bool CreateDirectory(string const &Parent, string const &Path);
00100 
00107 bool CreateAPTDirectoryIfNeeded(string const &Parent, string const &Path);
00108 
00109 std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
00110                                         bool const &SortList, bool const &AllowNoExt=false);
00111 std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext,
00112                                         bool const &SortList);
00113 string SafeGetCWD();
00114 void SetCloseExec(int Fd,bool Close);
00115 void SetNonBlock(int Fd,bool Block);
00116 bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
00117 pid_t ExecFork();
00118 bool ExecWait(pid_t Pid,const char *Name,bool Reap = false);
00119 
00120 // File string manipulators
00121 string flNotDir(string File);
00122 string flNotFile(string File);
00123 string flNoLink(string File);
00124 string flExtension(string File);
00125 string flCombine(string Dir,string File);
00126 
00127 #endif