]> git.saurik.com Git - apt.git/commitdiff
simplify code some more to make reddit happy
authorDavid Kalnischkies <david@kalnischkies.de>
Fri, 14 Feb 2014 15:33:26 +0000 (16:33 +0100)
committerDavid Kalnischkies <david@kalnischkies.de>
Fri, 14 Feb 2014 18:14:45 +0000 (19:14 +0100)
Commit 6008b79adf1d7ea5607fab87a355d664c8725026 should have been guarded
by "Git-Dch: Ignore", but it wasn't and I only noticed it with the Close
message via deity thinking "hehe, I wonder if someone is gonna notice".
Looks like someone did: hats off to reddit user itisOmegakai!
Good to know that what I do isn't only monitored by goverments. :)

As there is another instance of basically the same code we just factor
out the code a bit and reuse, so its even cleaner and not only simpler.

Reported-By: scan-build
apt-pkg/contrib/fileutl.cc

index 71ac9c73f5d6b4d535c79e2db5380c386b674221..53628406445cdf42a5c911c0faf498e46ad87b94 100644 (file)
@@ -1588,18 +1588,15 @@ unsigned long long FileFd::Tell()
    return Res;
 }
                                                                        /*}}}*/
-// FileFd::FileSize - Return the size of the file                      /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-unsigned long long FileFd::FileSize()
+static bool StatFileFd(char const * const msg, int const iFd, std::string const &FileName, struct stat &Buf, FileFdPrivate * const d) /*{{{*/
 {
-   struct stat Buf;
-
    bool ispipe = (d != NULL && d->pipe == true);
    if (ispipe == false)
    {
       if (fstat(iFd,&Buf) != 0)
-        return FileFdErrno("fstat","Unable to determine the file size");
+        // higher-level code will generate more meaningful messages,
+        // even translated this would be meaningless for users
+        return _error->Errno("fstat", "Unable to determine %s for fd %i", msg, iFd);
       ispipe = S_ISFIFO(Buf.st_mode);
    }
 
@@ -1611,12 +1608,35 @@ unsigned long long FileFd::FileSize()
       if (d != NULL)
         d->pipe = true;
       if (stat(FileName.c_str(), &Buf) != 0)
-        return FileFdErrno("stat","Unable to determine the file size");
+        return _error->Errno("fstat", "Unable to determine %s for file %s", msg, FileName.c_str());
+   }
+   return true;
+}
+                                                                       /*}}}*/
+// FileFd::FileSize - Return the size of the file                      /*{{{*/
+unsigned long long FileFd::FileSize()
+{
+   struct stat Buf;
+   if (StatFileFd("file size", iFd, FileName, Buf, d) == false)
+   {
+      Flags |= Fail;
+      return 0;
    }
-
    return Buf.st_size;
 }
                                                                        /*}}}*/
+// FileFd::ModificationTime - Return the time of last touch            /*{{{*/
+time_t FileFd::ModificationTime()
+{
+   struct stat Buf;
+   if (StatFileFd("modification time", iFd, FileName, Buf, d) == false)
+   {
+      Flags |= Fail;
+      return 0;
+   }
+   return Buf.st_mtime;
+}
+                                                                       /*}}}*/
 // FileFd::Size - Return the size of the content in the file           /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -1688,35 +1708,6 @@ unsigned long long FileFd::Size()
    return size;
 }
                                                                        /*}}}*/
-// FileFd::ModificationTime - Return the time of last touch            /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-time_t FileFd::ModificationTime()
-{
-   struct stat Buf;
-   if ((d == NULL || d->pipe == false) && fstat(iFd,&Buf) != 0)
-   {
-      FileFdErrno("fstat","Unable to determine the modification time of file %s", FileName.c_str());
-      return 0;
-   }
-
-   // for compressor pipes st_size is undefined and at 'best' zero
-   if ((d != NULL && d->pipe == true) || S_ISFIFO(Buf.st_mode))
-   {
-      // we set it here, too, as we get the info here for free
-      // in theory the Open-methods should take care of it already
-      if (d != NULL)
-        d->pipe = true;
-      if (stat(FileName.c_str(), &Buf) != 0)
-      {
-        FileFdErrno("fstat","Unable to determine the modification time of file %s", FileName.c_str());
-        return 0;
-      }
-   }
-
-   return Buf.st_mtime;
-}
-                                                                       /*}}}*/
 // FileFd::Close - Close the file if the close flag is set             /*{{{*/
 // ---------------------------------------------------------------------
 /* */