]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
Widened the mmap size
[apt.git] / apt-pkg / contrib / fileutl.cc
index b29b2e098dfa37fe86d6d16cfda1addaf6bac109..77e846117bc8778c87117bae05edbc0a455f6c13 100644 (file)
@@ -1,6 +1,6 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: fileutl.cc,v 1.6 1998/07/19 04:42:12 jgg Exp $
+// $Id: fileutl.cc,v 1.13 1998/10/26 07:11:49 jgg Exp $
 /* ######################################################################
    
    File Utilities
@@ -29,7 +29,7 @@
 // CopyFile - Buffered copy of a file                                  /*{{{*/
 // ---------------------------------------------------------------------
 /* The caller is expected to set things so that failure causes erasure */
-bool CopyFile(FileFd From,FileFd To)
+bool CopyFile(FileFd &From,FileFd &To)
 {
    if (From.IsOpen() == false || To.IsOpen() == false)
       return false;
@@ -107,6 +107,59 @@ string SafeGetCWD()
    return S;
 }
                                                                        /*}}}*/
+// flNotDir - Strip the directory from the filename                    /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+string flNotDir(string File)
+{
+   string::size_type Res = File.rfind('/');
+   if (Res == string::npos)
+      return File;
+   Res++;
+   return string(File,Res,Res - File.length());
+}
+                                                                       /*}}}*/
+// SetCloseExec - Set the close on exec flag                           /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void SetCloseExec(int Fd,bool Close)
+{   
+   if (fcntl(Fd,F_SETFD,(Close == false)?0:FD_CLOEXEC) != 0)
+   {
+      cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl;
+      exit(100);
+   }
+}
+                                                                       /*}}}*/
+// SetNonBlock - Set the nonblocking flag                              /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void SetNonBlock(int Fd,bool Block)
+{   
+   int Flags = fcntl(Fd,F_GETFL) & (~O_NONBLOCK);
+   if (fcntl(Fd,F_SETFL,Flags | ((Block == false)?0:O_NONBLOCK)) != 0)
+   {
+      cerr << "FATAL -> Could not set non-blocking flag " << strerror(errno) << endl;
+      exit(100);
+   }
+}
+                                                                       /*}}}*/
+// WaitFd - Wait for a FD to become readable                           /*{{{*/
+// ---------------------------------------------------------------------
+/* This waits for a FD to become readable using select. It is usefull for
+   applications making use of non-blocking sockets. */
+bool WaitFd(int Fd)
+{
+   fd_set Set;
+   FD_ZERO(&Set);
+   FD_SET(Fd,&Set);
+
+   if (select(Fd+1,&Set,0,0,0) <= 0)
+      return false;
+
+   return true;
+}
+                                                                       /*}}}*/
 
 // FileFd::FileFd - Open a file                                                /*{{{*/
 // ---------------------------------------------------------------------
@@ -128,6 +181,10 @@ FileFd::FileFd(string FileName,OpenMode Mode, unsigned long Perms)
       case WriteExists:
       iFd = open(FileName.c_str(),O_RDWR);
       break;
+
+      case WriteAny:
+      iFd = open(FileName.c_str(),O_RDWR | O_CREAT,Perms);
+      break;
       
       // Dont use this in public directories
       case LockEmpty:
@@ -138,7 +195,10 @@ FileFd::FileFd(string FileName,OpenMode Mode, unsigned long Perms)
    if (iFd < 0)
       _error->Errno("open","Could not open file %s",FileName.c_str());
    else
+   {
       this->FileName = FileName;
+      SetCloseExec(iFd,true);
+   }   
 }
                                                                        /*}}}*/
 // FileFd::~File - Closes the file                                     /*{{{*/