LCOV - code coverage report
Current view: top level - lib/dpkg - db-ctrl-upgrade.c (source / functions) Coverage Total Hit
Test: dpkg 1.22.7-3-g89f48 C code coverage Lines: 0.0 % 109 0
Test Date: 2024-07-17 02:53:43 Functions: 0.0 % 8 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0.0 % 46 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * libdpkg - Debian packaging suite library routines
       3                 :             :  * db-ctrl-upgrade.c - package control information database format upgrade
       4                 :             :  *
       5                 :             :  * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
       6                 :             :  * Copyright © 2011-2014 Guillem Jover <guillem@debian.org>
       7                 :             :  * Copyright © 2011 Linaro Limited
       8                 :             :  * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
       9                 :             :  *
      10                 :             :  * This is free software; you can redistribute it and/or modify
      11                 :             :  * it under the terms of the GNU General Public License as published by
      12                 :             :  * the Free Software Foundation; either version 2 of the License, or
      13                 :             :  * (at your option) any later version.
      14                 :             :  *
      15                 :             :  * This is distributed in the hope that it will be useful,
      16                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      17                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18                 :             :  * GNU General Public License for more details.
      19                 :             :  *
      20                 :             :  * You should have received a copy of the GNU General Public License
      21                 :             :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      22                 :             :  */
      23                 :             : 
      24                 :             : #include <config.h>
      25                 :             : #include <compat.h>
      26                 :             : 
      27                 :             : #include <sys/types.h>
      28                 :             : #include <sys/stat.h>
      29                 :             : 
      30                 :             : #include <errno.h>
      31                 :             : #include <stdlib.h>
      32                 :             : #include <unistd.h>
      33                 :             : 
      34                 :             : #include <dpkg/i18n.h>
      35                 :             : #include <dpkg/dpkg.h>
      36                 :             : #include <dpkg/dpkg-db.h>
      37                 :             : #include <dpkg/fsys.h>
      38                 :             : #include <dpkg/db-ctrl.h>
      39                 :             : #include <dpkg/path.h>
      40                 :             : #include <dpkg/dir.h>
      41                 :             : 
      42                 :             : struct rename_node {
      43                 :             :         struct rename_node *next;
      44                 :             :         char *old;
      45                 :             :         char *new;
      46                 :             : };
      47                 :             : 
      48                 :             : /* Global variables. */
      49                 :             : static struct rename_node *rename_head = NULL;
      50                 :             : 
      51                 :             : static struct rename_node *
      52                 :           0 : rename_node_new(const char *old, const char *new, struct rename_node *next)
      53                 :             : {
      54                 :             :         struct rename_node *node;
      55                 :             : 
      56                 :           0 :         node = m_malloc(sizeof(*node));
      57                 :           0 :         node->next = next;
      58                 :           0 :         node->old = m_strdup(old);
      59                 :           0 :         node->new = m_strdup(new);
      60                 :             : 
      61                 :           0 :         return node;
      62                 :             : }
      63                 :             : 
      64                 :             : static void
      65                 :           0 : rename_node_free(struct rename_node *node)
      66                 :             : {
      67                 :           0 :         free(node->old);
      68                 :           0 :         free(node->new);
      69                 :           0 :         free(node);
      70                 :           0 : }
      71                 :             : 
      72                 :             : static void
      73                 :           0 : pkg_infodb_link_multiarch_files(void)
      74                 :             : {
      75                 :             :         DIR *db_dir;
      76                 :             :         struct dirent *db_de;
      77                 :           0 :         struct varbuf pkgname = VARBUF_INIT;
      78                 :           0 :         struct varbuf oldname = VARBUF_INIT;
      79                 :           0 :         struct varbuf newname = VARBUF_INIT;
      80                 :             :         struct varbuf_state oldname_state;
      81                 :             :         struct varbuf_state newname_state;
      82                 :             : 
      83                 :           0 :         varbuf_add_dir(&oldname, pkg_infodb_get_dir());
      84                 :           0 :         varbuf_snapshot(&oldname, &oldname_state);
      85                 :             : 
      86                 :           0 :         varbuf_set_varbuf(&newname, &oldname);
      87                 :           0 :         varbuf_snapshot(&newname, &newname_state);
      88                 :             : 
      89                 :           0 :         db_dir = opendir(pkg_infodb_get_dir());
      90         [ #  # ]:           0 :         if (!db_dir)
      91                 :           0 :                 ohshite(_("cannot read info directory"));
      92                 :             : 
      93                 :           0 :         push_cleanup(cu_closedir, ~0, 1, (void *)db_dir);
      94         [ #  # ]:           0 :         while ((db_de = readdir(db_dir)) != NULL) {
      95                 :             :                 const char *filetype, *dot;
      96                 :             :                 struct pkginfo *pkg;
      97                 :             :                 struct pkgset *set;
      98                 :             : 
      99                 :             :                 /* Ignore dotfiles, including ‘.’ and ‘..’. */
     100         [ #  # ]:           0 :                 if (db_de->d_name[0] == '.')
     101                 :           0 :                         continue;
     102                 :             : 
     103                 :             :                 /* Ignore anything odd. */
     104                 :           0 :                 dot = strrchr(db_de->d_name, '.');
     105         [ #  # ]:           0 :                 if (dot == NULL)
     106                 :           0 :                         continue;
     107                 :             : 
     108                 :           0 :                 varbuf_set_buf(&pkgname, db_de->d_name, dot - db_de->d_name);
     109                 :             : 
     110                 :             :                  /* Skip files already converted. */
     111         [ #  # ]:           0 :                 if (strchr(pkgname.buf, ':'))
     112                 :           0 :                         continue;
     113                 :             : 
     114                 :           0 :                 set = pkg_hash_find_set(pkgname.buf);
     115         [ #  # ]:           0 :                 for (pkg = &set->pkg; pkg; pkg = pkg->arch_next)
     116         [ #  # ]:           0 :                         if (pkg->status != PKG_STAT_NOTINSTALLED)
     117                 :           0 :                                 break;
     118         [ #  # ]:           0 :                 if (!pkg) {
     119                 :           0 :                         warning(_("info file %s/%s not associated to any package"),
     120                 :           0 :                                 pkg_infodb_get_dir(), db_de->d_name);
     121                 :           0 :                         continue;
     122                 :             :                 }
     123                 :             : 
     124                 :             :                 /* Does it need to be upgraded? */
     125         [ #  # ]:           0 :                 if (pkg->installed.multiarch != PKG_MULTIARCH_SAME)
     126                 :           0 :                         continue;
     127                 :             : 
     128                 :             :                 /* Skip past the full stop. */
     129                 :           0 :                 filetype = dot + 1;
     130                 :             : 
     131                 :           0 :                 varbuf_rollback(&oldname_state);
     132                 :           0 :                 varbuf_add_str(&oldname, db_de->d_name);
     133                 :             : 
     134                 :           0 :                 varbuf_rollback(&newname_state);
     135                 :           0 :                 varbuf_add_pkgbin_name(&newname, pkg, &pkg->installed, pnaw_always);
     136                 :           0 :                 varbuf_add_char(&newname, '.');
     137                 :           0 :                 varbuf_add_str(&newname, filetype);
     138                 :             : 
     139   [ #  #  #  # ]:           0 :                 if (link(oldname.buf, newname.buf) && errno != EEXIST)
     140                 :           0 :                         ohshite(_("error creating hard link '%.255s'"),
     141                 :             :                                 newname.buf);
     142                 :           0 :                 rename_head = rename_node_new(oldname.buf, newname.buf, rename_head);
     143                 :             :         }
     144                 :           0 :         pop_cleanup(ehflag_normaltidy); /* closedir */
     145                 :             : 
     146                 :           0 :         varbuf_destroy(&pkgname);
     147                 :           0 :         varbuf_destroy(&newname);
     148                 :           0 :         varbuf_destroy(&oldname);
     149                 :           0 : }
     150                 :             : 
     151                 :             : static void
     152                 :           0 : cu_abort_db_upgrade(int argc, void **argv)
     153                 :             : {
     154                 :           0 :         struct atomic_file *file = argv[0];
     155                 :             :         struct rename_node *next;
     156                 :             : 
     157                 :             :         /* Restore the old files if needed and drop the newly created files. */
     158         [ #  # ]:           0 :         while (rename_head) {
     159                 :           0 :                 next = rename_head->next;
     160   [ #  #  #  # ]:           0 :                 if (link(rename_head->new, rename_head->old) && errno != EEXIST)
     161                 :           0 :                         ohshite(_("error creating hard link '%.255s'"),
     162                 :           0 :                                 rename_head->old);
     163         [ #  # ]:           0 :                 if (unlink(rename_head->new))
     164                 :           0 :                         ohshite(_("cannot remove '%.250s'"), rename_head->new);
     165                 :           0 :                 rename_node_free(rename_head);
     166                 :           0 :                 rename_head = next;
     167                 :             :         }
     168   [ #  #  #  # ]:           0 :         if (unlink(file->name_new) && errno != ENOENT)
     169                 :           0 :                 ohshite(_("cannot remove '%.250s'"), file->name_new);
     170                 :             : 
     171                 :           0 :         atomic_file_free(file);
     172                 :           0 : }
     173                 :             : 
     174                 :             : static void
     175                 :           0 : pkg_infodb_write_format(struct atomic_file *file, int version)
     176                 :             : {
     177         [ #  # ]:           0 :         if (fprintf(file->fp, "%d\n", version) < 0)
     178                 :           0 :                 ohshite(_("error while writing '%s'"), file->name_new);
     179                 :             : 
     180                 :           0 :         atomic_file_sync(file);
     181                 :           0 :         atomic_file_close(file);
     182                 :           0 :         dir_sync_path_parent(file->name);
     183                 :             : 
     184                 :           0 :         pkg_infodb_set_format(version);
     185                 :           0 : }
     186                 :             : 
     187                 :             : static void
     188                 :           0 : pkg_infodb_unlink_monoarch_files(void)
     189                 :             : {
     190                 :             :         struct rename_node *next;
     191                 :             : 
     192         [ #  # ]:           0 :         while (rename_head) {
     193                 :           0 :                 next = rename_head->next;
     194         [ #  # ]:           0 :                 if (unlink(rename_head->old))
     195                 :           0 :                         ohshite(_("cannot remove '%.250s'"), rename_head->old);
     196                 :           0 :                 rename_node_free(rename_head);
     197                 :           0 :                 rename_head = next;
     198                 :             :         }
     199                 :           0 : }
     200                 :             : 
     201                 :             : static void
     202                 :           0 : pkg_infodb_upgrade_to_multiarch(void)
     203                 :             : {
     204                 :             :         struct atomic_file *db_file;
     205                 :             :         char *db_format_file;
     206                 :             : 
     207                 :           0 :         db_format_file = dpkg_db_get_path(INFODIR "/format");
     208                 :           0 :         db_file = atomic_file_new(db_format_file, ATOMIC_FILE_MKPATH);
     209                 :           0 :         atomic_file_open(db_file);
     210                 :             : 
     211                 :           0 :         push_cleanup(cu_abort_db_upgrade, ehflag_bombout, 1, db_file);
     212                 :             : 
     213                 :           0 :         pkg_infodb_link_multiarch_files();
     214                 :           0 :         pkg_infodb_write_format(db_file, 1);
     215                 :             : 
     216                 :           0 :         pkg_infodb_unlink_monoarch_files();
     217                 :           0 :         atomic_file_commit(db_file);
     218                 :           0 :         dir_sync_path(pkg_infodb_get_dir());
     219                 :             : 
     220                 :           0 :         pop_cleanup(ehflag_normaltidy);
     221                 :             : 
     222                 :           0 :         atomic_file_free(db_file);
     223                 :           0 :         free(db_format_file);
     224                 :           0 : }
     225                 :             : 
     226                 :             : /**
     227                 :             :  * Upgrade the infodb if there's the need and possibility.
     228                 :             :  *
     229                 :             :  * Currently this implies, that the modstatdb was opened for writing and:
     230                 :             :  * - previous upgrade has not been completed; or
     231                 :             :  * - current format is not the latest one.
     232                 :             :  */
     233                 :             : void
     234                 :           0 : pkg_infodb_upgrade(void)
     235                 :             : {
     236                 :             :         enum pkg_infodb_format db_format;
     237                 :             : 
     238                 :             :         /* Make sure to always read and verify the format version. */
     239                 :           0 :         db_format = pkg_infodb_get_format();
     240                 :             : 
     241         [ #  # ]:           0 :         if (modstatdb_get_status() < msdbrw_write)
     242                 :           0 :                 return;
     243                 :             : 
     244   [ #  #  #  # ]:           0 :         if (db_format < PKG_INFODB_FORMAT_MULTIARCH ||
     245                 :           0 :             pkg_infodb_is_upgrading())
     246                 :           0 :                 pkg_infodb_upgrade_to_multiarch();
     247                 :             : }
        

Generated by: LCOV version 2.0-1