]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/fileutl.cc
6c6441ef67efafff38ac73a310e985d032e0384c
   1 // -*- mode: cpp; mode: fold -*- 
   3 // $Id: fileutl.cc,v 1.2 1998/07/04 05:57:41 jgg Exp $ 
   4 /* ###################################################################### 
   8    CopyFile - Buffered copy of a single file 
   9    GetLock - dpkg compatible lock file manipulation (fcntl) 
  11    This source is placed in the Public Domain, do with it what you will 
  12    It was originally written by Jason Gunthorpe. 
  14    ##################################################################### */ 
  16 // Include Files                                                        /*{{{*/ 
  17 #include <pkglib/fileutl.h> 
  18 #include <pkglib/error.h> 
  22 #include <sys/fcntl.h> 
  23 #include <sys/types.h> 
  26 // CopyFile - Buffered copy of a file                                   /*{{{*/ 
  27 // --------------------------------------------------------------------- 
  28 /* The caller is expected to set things so that failure causes erasure */ 
  29 bool CopyFile(File From
,File To
) 
  31    if (From
.IsOpen() == false || To
.IsOpen() == false) 
  34    // Buffered copy between fds 
  35    unsigned char *Buf 
= new unsigned char[64000]; 
  37    while ((Size 
= read(From
.Fd(),Buf
,64000)) > 0) 
  39       if (To
.Write(Buf
,Size
) == false) 
  50 // GetLock - Gets a lock file                                           /*{{{*/ 
  51 // --------------------------------------------------------------------- 
  52 /* This will create an empty file of the given name and lock it. Once this 
  53    is done all other calls to GetLock in any other process will fail with 
  54    -1. The return result is the fd of the file, the call should call 
  55    close at some time. */ 
  56 int GetLock(string File
,bool Errors
) 
  58    int FD 
= open(File
.c_str(),O_RDWR 
| O_CREAT 
| O_TRUNC
,0640); 
  62          _error
->Errno("open","Could not open lock file %s",File
.c_str()); 
  66    // Aquire a write lock 
  69    fl
.l_whence
= SEEK_SET
; 
  72    if (fcntl(FD
,F_SETLK
,&fl
) == -1) 
  75          _error
->Errno("open","Could not get lock %s",File
.c_str()); 
  83 // FileExists - Check if a file exists                                  /*{{{*/ 
  84 // --------------------------------------------------------------------- 
  86 bool FileExists(string File
) 
  89    if (stat(File
.c_str(),&Buf
) != 0) 
  94 // SafeGetCWD - This is a safer getcwd that returns a dynamic string    /*{{{*/ 
  95 // --------------------------------------------------------------------- 
  96 /* We return / on failure. */ 
  99    // Stash the current dir. 
 102    if (getcwd(S
,sizeof(S
)) == 0) 
 108 // File::File - Open a file                                             /*{{{*/ 
 109 // --------------------------------------------------------------------- 
 110 /* The most commonly used open mode combinations are given with Mode */ 
 111 File::File(string FileName
,OpenMode Mode
, unsigned long Perms
) 
 117       iFd 
= open(FileName
.c_str(),O_RDONLY
); 
 121       unlink(FileName
.c_str()); 
 122       iFd 
= open(FileName
.c_str(),O_RDWR 
| O_CREAT 
| O_EXCL
,Perms
); 
 126       iFd 
= open(FileName
.c_str(),O_RDWR
); 
 131       _error
->Errno("open","Could not open file %s",FileName
.c_str()); 
 133       this->FileName 
= FileName
; 
 136 // File::~File - Closes the file                                        /*{{{*/ 
 137 // --------------------------------------------------------------------- 
 138 /* If the proper modes are selected then we close the Fd and possibly 
 139    unlink the file on error. */ 
 145 // File::Read - Read a bit of the file                                  /*{{{*/ 
 146 // --------------------------------------------------------------------- 
 148 bool File::Read(void *To
,unsigned long Size
) 
 150    if (read(iFd
,To
,Size
) != (signed)Size
) 
 153       return _error
->Errno("read","Read error"); 
 159 // File::Write - Write to the file                                      /*{{{*/ 
 160 // --------------------------------------------------------------------- 
 162 bool File::Write(void *From
,unsigned long Size
) 
 164    if (write(iFd
,From
,Size
) != (signed)Size
) 
 167       return _error
->Errno("write","Write error"); 
 173 // File::Seek - Seek in the file                                        /*{{{*/ 
 174 // --------------------------------------------------------------------- 
 176 bool File::Seek(unsigned long To
) 
 178    if (lseek(iFd
,To
,SEEK_SET
) != (signed)To
) 
 181       return _error
->Error("Unable to seek to %u",To
); 
 187 // File::Size - Return the size of the file                             /*{{{*/ 
 188 // --------------------------------------------------------------------- 
 190 unsigned long File::Size() 
 193    if (fstat(iFd
,&Buf
) != 0) 
 194       return _error
->Errno("fstat","Unable to determine the file size"); 
 198 // File::Close - Close the file if the close flag is set                /*{{{*/ 
 199 // --------------------------------------------------------------------- 
 204    if ((Flags 
& AutoClose
) == AutoClose
) 
 206          Res 
&= _error
->Errno("close","Problem closing the file"); 
 208    if ((Flags 
& Fail
) == Fail 
&& (Flags 
& DelOnFail
) == DelOnFail 
&& 
 209        FileName
.empty() == false) 
 210       if (unlink(FileName
.c_str()) != 0) 
 211          Res 
&= _error
->Warning("unlnk","Problem unlinking the file");