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