]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
* apt-pkg/contrib/weakptr.h:
[apt.git] / apt-pkg / contrib / fileutl.cc
index eabaadf908ee7f29c637d87cb88b855d96c056df..db6057ea3ff6a975cf8a2555d38e499dcdc40406 100644 (file)
@@ -191,7 +191,7 @@ int GetLock(string File,bool Errors)
                                                                        /*}}}*/
 // FileExists - Check if a file exists                                 /*{{{*/
 // ---------------------------------------------------------------------
-/* */
+/* Beware: Directories are also files! */
 bool FileExists(string File)
 {
    struct stat Buf;
@@ -200,6 +200,17 @@ bool FileExists(string File)
    return true;
 }
                                                                        /*}}}*/
+// RealFileExists - Check if a file exists and if it is really a file  /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool RealFileExists(string File)
+{
+   struct stat Buf;
+   if (stat(File.c_str(),&Buf) != 0)
+      return false;
+   return ((Buf.st_mode & S_IFREG) != 0);
+}
+                                                                       /*}}}*/
 // DirectoryExists - Check if a directory exists and is really one     /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -304,6 +315,13 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> c
    }
 
    std::vector<string> List;
+
+   if (DirectoryExists(Dir.c_str()) == false)
+   {
+      _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str());
+      return List;
+   }
+
    Configuration::MatchAgainstConfig SilentIgnore("Dir::Ignore-Files-Silently");
    DIR *D = opendir(Dir.c_str());
    if (D == 0) 
@@ -910,18 +928,48 @@ unsigned long FileFd::Tell()
    return Res;
 }
                                                                        /*}}}*/
-// FileFd::Size - Return the size of the file                          /*{{{*/
+// FileFd::FileSize - Return the size of the file                      /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-unsigned long FileFd::Size()
+unsigned long FileFd::FileSize()
 {
-   //TODO: For gz, do we need the actual file size here or the uncompressed length?
    struct stat Buf;
+
    if (fstat(iFd,&Buf) != 0)
       return _error->Errno("fstat","Unable to determine the file size");
    return Buf.st_size;
 }
                                                                        /*}}}*/
+// FileFd::Size - Return the size of the content in the file           /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+unsigned long FileFd::Size()
+{
+   unsigned long size = FileSize();
+
+   // only check gzsize if we are actually a gzip file, just checking for
+   // "gz" is not sufficient as uncompressed files will be opened with
+   // gzopen in "direct" mode as well
+   if (gz && !gzdirect(gz) && size > 0)
+   {
+       /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do
+       * this ourselves; the original (uncompressed) file size is the last 32
+       * bits of the file */
+       off_t orig_pos = lseek(iFd, 0, SEEK_CUR);
+       if (lseek(iFd, -4, SEEK_END) < 0)
+          return _error->Errno("lseek","Unable to seek to end of gzipped file");
+       if (read(iFd, &size, 4) != 4)
+          return _error->Errno("read","Unable to read original size of gzipped file");
+       size &= 0xFFFFFFFF;
+
+       if (lseek(iFd, orig_pos, SEEK_SET) < 0)
+          return _error->Errno("lseek","Unable to seek in gzipped file");
+       return size;
+   }
+
+   return size;
+}
+                                                                       /*}}}*/
 // FileFd::Close - Close the file if the close flag is set             /*{{{*/
 // ---------------------------------------------------------------------
 /* */