This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch master
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=1166bbb5f099580a0c1c26d0e29b1ae3fe11ad0f

commit 1166bbb5f099580a0c1c26d0e29b1ae3fe11ad0f
Author: Guillem Jover <[email protected]>
AuthorDate: Sat Jan 13 14:46:46 2018 +0100

    libdpkg: Switch db-fsys to use the new file_slurp() function
    
    This reduces code duplication.
---
 debian/changelog          |  1 +
 lib/dpkg/db-fsys-digest.c | 49 +++++++++++++----------------------------------
 lib/dpkg/db-fsys-files.c  | 44 +++++++++++++-----------------------------
 3 files changed, 27 insertions(+), 67 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 12f817c00..5ad7d759b 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -196,6 +196,7 @@ dpkg (1.19.1) UNRELEASED; urgency=medium
     - libdpkg: Change dpkg_error to track errno values.
     - libdpkg: Add new varbuf_new() and varbuf_free() functions.
     - libdpkg: Add new file_slurp() function.
+    - libdpkg: Switch db-fsys to use the new file_slurp() function.
   * Build system:
     - Set distribution tarball format to ustar, instead of default v7 format.
     - Mark PO4A and POD2MAN as precious variables.
diff --git a/lib/dpkg/db-fsys-digest.c b/lib/dpkg/db-fsys-digest.c
index 05137c2f2..cc032f14a 100644
--- a/lib/dpkg/db-fsys-digest.c
+++ b/lib/dpkg/db-fsys-digest.c
@@ -80,13 +80,14 @@ write_filehash_except(struct pkginfo *pkg, struct pkgbin 
*pkgbin,
 }
 
 static void
-parse_filehash_buffer(char *buf, char *buf_end,
+parse_filehash_buffer(struct varbuf *buf,
                       struct pkginfo *pkg, struct pkgbin *pkgbin)
 {
        char *thisline, *nextline;
        const char *pkgname = pkg_name(pkg, pnaw_nonambig);
+       const char *buf_end = buf->buf + buf->used;
 
-       for (thisline = buf; thisline < buf_end; thisline = nextline) {
+       for (thisline = buf->buf; thisline < buf_end; thisline = nextline) {
                struct filenamenode *namenode;
                char *endline, *hash_end, *filename;
 
@@ -124,7 +125,7 @@ parse_filehash_buffer(char *buf, char *buf_end,
                      thisline, filename);
 
                /* Add the file to the list. */
-               namenode = findnamenode(filename, fnn_nocopy);
+               namenode = findnamenode(filename, 0);
                namenode->newhash = thisline;
        }
 }
@@ -132,43 +133,19 @@ parse_filehash_buffer(char *buf, char *buf_end,
 void
 parse_filehash(struct pkginfo *pkg, struct pkgbin *pkgbin)
 {
-       static int fd;
        const char *hashfile;
-       struct stat st;
+       struct varbuf buf = VARBUF_INIT;
+       struct dpkg_error err = DPKG_ERROR_INIT;
 
        hashfile = pkg_infodb_get_file(pkg, pkgbin, HASHFILE);
 
-       fd = open(hashfile, O_RDONLY);
-       if (fd < 0) {
-               if (errno == ENOENT)
-                       return;
+       if (file_slurp(hashfile, &buf, &err) < 0 && err.syserrno != ENOENT)
+               dpkg_error_print(&err,
+                                _("loading control file '%s' for package 
'%s'"),
+                                HASHFILE, pkg_name(pkg, pnaw_nonambig));
 
-               ohshite(_("cannot open control file '%s' for package '%s'"),
-                       HASHFILE, pkg_name(pkg, pnaw_nonambig));
-       }
-
-       if (fstat(fd, &st) < 0)
-               ohshite(_("cannot stat control file '%s' for package '%s'"),
-                       HASHFILE, pkg_name(pkg, pnaw_nonambig));
-
-       if (!S_ISREG(st.st_mode))
-               ohshit(_("control file '%s' for package '%s' is not a regular 
file"),
-                      HASHFILE, pkg_name(pkg, pnaw_nonambig));
-
-       if (st.st_size > 0) {
-               char *buf, *buf_end;
-
-               buf = nfmalloc(st.st_size);
-               buf_end = buf + st.st_size;
-
-               if (fd_read(fd, buf, st.st_size) < 0)
-                       ohshite(_("cannot read control file '%s' for package 
'%s'"),
-                               HASHFILE, pkg_name(pkg, pnaw_nonambig));
-
-               parse_filehash_buffer(buf, buf_end, pkg, pkgbin);
-       }
+       if (buf.used > 0)
+               parse_filehash_buffer(&buf, pkg, pkgbin);
 
-       if (close(fd))
-               ohshite(_("cannot close control file '%s' for package '%s'"),
-                       HASHFILE, pkg_name(pkg, pnaw_nonambig));
+       varbuf_destroy(&buf);
 }
diff --git a/lib/dpkg/db-fsys-files.c b/lib/dpkg/db-fsys-files.c
index ff2ee7063..534109329 100644
--- a/lib/dpkg/db-fsys-files.c
+++ b/lib/dpkg/db-fsys-files.c
@@ -77,11 +77,11 @@ static enum pkg_filesdb_load_status saidread = 
PKG_FILESDB_LOAD_NONE;
 void
 ensure_packagefiles_available(struct pkginfo *pkg)
 {
-  static int fd;
   const char *filelistfile;
   struct fileinlist **lendp;
-  struct stat stat_buf;
-  char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
+  char *loaded_list_end, *thisline, *nextline, *ptr;
+  struct varbuf buf = VARBUF_INIT;
+  struct dpkg_error err = DPKG_ERROR_INIT;
 
   if (pkg->files_list_valid)
     return;
@@ -99,12 +99,11 @@ ensure_packagefiles_available(struct pkginfo *pkg)
 
   onerr_abort++;
 
-  fd= open(filelistfile,O_RDONLY);
+  if (file_slurp(filelistfile, &buf, &err) < 0) {
+    if (err.syserrno != ENOENT)
+      dpkg_error_print(&err, _("loading files list file for package '%s'"),
+                       pkg_name(pkg, pnaw_nonambig));
 
-  if (fd==-1) {
-    if (errno != ENOENT)
-      ohshite(_("unable to open files list file for package '%.250s'"),
-              pkg_name(pkg, pnaw_nonambig));
     onerr_abort--;
     if (pkg->status != PKG_STAT_CONFIGFILES &&
         dpkg_version_is_informative(&pkg->configversion)) {
@@ -117,26 +116,11 @@ ensure_packagefiles_available(struct pkginfo *pkg)
     return;
   }
 
-  push_cleanup(cu_closefd, ehflag_bombout, 1, &fd);
-
-  if (fstat(fd, &stat_buf))
-    ohshite(_("unable to stat files list file for package '%.250s'"),
-            pkg_name(pkg, pnaw_nonambig));
-
-  if (!S_ISREG(stat_buf.st_mode))
-    ohshit(_("files list for package '%.250s' is not a regular file"),
-           pkg_name(pkg, pnaw_nonambig));
-
-  if (stat_buf.st_size) {
-    loaded_list = nfmalloc(stat_buf.st_size);
-    loaded_list_end = loaded_list + stat_buf.st_size;
-
-    if (fd_read(fd, loaded_list, stat_buf.st_size) < 0)
-      ohshite(_("reading files list for package '%.250s'"),
-              pkg_name(pkg, pnaw_nonambig));
+  if (buf.used) {
+    loaded_list_end = buf.buf + buf.used;
 
     lendp = &pkg->files;
-    thisline = loaded_list;
+    thisline = buf.buf;
     while (thisline < loaded_list_end) {
       struct filenamenode *namenode;
 
@@ -154,15 +138,13 @@ ensure_packagefiles_available(struct pkginfo *pkg)
                pkg_name(pkg, pnaw_nonambig));
       *ptr = '\0';
 
-      namenode = findnamenode(thisline, fnn_nocopy);
+      namenode = findnamenode(thisline, 0);
       lendp = pkg_files_add_file(pkg, namenode, lendp);
       thisline = nextline;
     }
   }
-  pop_cleanup(ehflag_normaltidy); /* fd = open() */
-  if (close(fd))
-    ohshite(_("error closing files list file for package '%.250s'"),
-            pkg_name(pkg, pnaw_nonambig));
+
+  varbuf_destroy(&buf);
 
   onerr_abort--;
 

-- 
Dpkg.Org's dpkg

Reply via email to