LCOV - code coverage report
Current view: top level - lib/dpkg - db-ctrl-upgrade.c (source / functions) Hit Total Coverage
Test: dpkg 1.21.11 C code coverage Lines: 0 115 0.0 %
Date: 2022-12-03 00:40:01 Functions: 0 8 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 46 0.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_end_str(&oldname);
      85                 :          0 :         varbuf_snapshot(&oldname, &oldname_state);
      86                 :            : 
      87                 :          0 :         varbuf_add_buf(&newname, oldname.buf, oldname.used);
      88                 :          0 :         varbuf_end_str(&newname);
      89                 :          0 :         varbuf_snapshot(&newname, &newname_state);
      90                 :            : 
      91                 :          0 :         db_dir = opendir(pkg_infodb_get_dir());
      92         [ #  # ]:          0 :         if (!db_dir)
      93                 :          0 :                 ohshite(_("cannot read info directory"));
      94                 :            : 
      95                 :          0 :         push_cleanup(cu_closedir, ~0, 1, (void *)db_dir);
      96         [ #  # ]:          0 :         while ((db_de = readdir(db_dir)) != NULL) {
      97                 :            :                 const char *filetype, *dot;
      98                 :            :                 struct pkginfo *pkg;
      99                 :            :                 struct pkgset *set;
     100                 :            : 
     101                 :            :                 /* Ignore dotfiles, including ‘.’ and ‘..’. */
     102         [ #  # ]:          0 :                 if (db_de->d_name[0] == '.')
     103                 :          0 :                         continue;
     104                 :            : 
     105                 :            :                 /* Ignore anything odd. */
     106                 :          0 :                 dot = strrchr(db_de->d_name, '.');
     107         [ #  # ]:          0 :                 if (dot == NULL)
     108                 :          0 :                         continue;
     109                 :            : 
     110                 :          0 :                 varbuf_reset(&pkgname);
     111                 :          0 :                 varbuf_add_buf(&pkgname, db_de->d_name, dot - db_de->d_name);
     112                 :          0 :                 varbuf_end_str(&pkgname);
     113                 :            : 
     114                 :            :                  /* Skip files already converted. */
     115         [ #  # ]:          0 :                 if (strchr(pkgname.buf, ':'))
     116                 :          0 :                         continue;
     117                 :            : 
     118                 :          0 :                 set = pkg_hash_find_set(pkgname.buf);
     119         [ #  # ]:          0 :                 for (pkg = &set->pkg; pkg; pkg = pkg->arch_next)
     120         [ #  # ]:          0 :                         if (pkg->status != PKG_STAT_NOTINSTALLED)
     121                 :          0 :                                 break;
     122         [ #  # ]:          0 :                 if (!pkg) {
     123                 :          0 :                         warning(_("info file %s/%s not associated to any package"),
     124                 :          0 :                                 pkg_infodb_get_dir(), db_de->d_name);
     125                 :          0 :                         continue;
     126                 :            :                 }
     127                 :            : 
     128                 :            :                 /* Does it need to be upgraded? */
     129         [ #  # ]:          0 :                 if (pkg->installed.multiarch != PKG_MULTIARCH_SAME)
     130                 :          0 :                         continue;
     131                 :            : 
     132                 :            :                 /* Skip past the full stop. */
     133                 :          0 :                 filetype = dot + 1;
     134                 :            : 
     135                 :          0 :                 varbuf_rollback(&oldname_state);
     136                 :          0 :                 varbuf_add_str(&oldname, db_de->d_name);
     137                 :          0 :                 varbuf_end_str(&oldname);
     138                 :            : 
     139                 :          0 :                 varbuf_rollback(&newname_state);
     140                 :          0 :                 varbuf_add_pkgbin_name(&newname, pkg, &pkg->installed, pnaw_always);
     141                 :          0 :                 varbuf_add_char(&newname, '.');
     142                 :          0 :                 varbuf_add_str(&newname, filetype);
     143                 :          0 :                 varbuf_end_str(&newname);
     144                 :            : 
     145   [ #  #  #  # ]:          0 :                 if (link(oldname.buf, newname.buf) && errno != EEXIST)
     146                 :          0 :                         ohshite(_("error creating hard link '%.255s'"),
     147                 :            :                                 newname.buf);
     148                 :          0 :                 rename_head = rename_node_new(oldname.buf, newname.buf, rename_head);
     149                 :            :         }
     150                 :          0 :         pop_cleanup(ehflag_normaltidy); /* closedir */
     151                 :            : 
     152                 :          0 :         varbuf_destroy(&pkgname);
     153                 :          0 :         varbuf_destroy(&newname);
     154                 :          0 :         varbuf_destroy(&oldname);
     155                 :          0 : }
     156                 :            : 
     157                 :            : static void
     158                 :          0 : cu_abort_db_upgrade(int argc, void **argv)
     159                 :            : {
     160                 :          0 :         struct atomic_file *file = argv[0];
     161                 :            :         struct rename_node *next;
     162                 :            : 
     163                 :            :         /* Restore the old files if needed and drop the newly created files. */
     164         [ #  # ]:          0 :         while (rename_head) {
     165                 :          0 :                 next = rename_head->next;
     166   [ #  #  #  # ]:          0 :                 if (link(rename_head->new, rename_head->old) && errno != EEXIST)
     167                 :          0 :                         ohshite(_("error creating hard link '%.255s'"),
     168                 :          0 :                                 rename_head->old);
     169         [ #  # ]:          0 :                 if (unlink(rename_head->new))
     170                 :          0 :                         ohshite(_("cannot remove '%.250s'"), rename_head->new);
     171                 :          0 :                 rename_node_free(rename_head);
     172                 :          0 :                 rename_head = next;
     173                 :            :         }
     174   [ #  #  #  # ]:          0 :         if (unlink(file->name_new) && errno != ENOENT)
     175                 :          0 :                 ohshite(_("cannot remove '%.250s'"), file->name_new);
     176                 :            : 
     177                 :          0 :         atomic_file_free(file);
     178                 :          0 : }
     179                 :            : 
     180                 :            : static void
     181                 :          0 : pkg_infodb_write_format(struct atomic_file *file, int version)
     182                 :            : {
     183         [ #  # ]:          0 :         if (fprintf(file->fp, "%d\n", version) < 0)
     184                 :          0 :                 ohshite(_("error while writing '%s'"), file->name_new);
     185                 :            : 
     186                 :          0 :         atomic_file_sync(file);
     187                 :          0 :         atomic_file_close(file);
     188                 :          0 :         dir_sync_path_parent(file->name);
     189                 :            : 
     190                 :          0 :         pkg_infodb_set_format(version);
     191                 :          0 : }
     192                 :            : 
     193                 :            : static void
     194                 :          0 : pkg_infodb_unlink_monoarch_files(void)
     195                 :            : {
     196                 :            :         struct rename_node *next;
     197                 :            : 
     198         [ #  # ]:          0 :         while (rename_head) {
     199                 :          0 :                 next = rename_head->next;
     200         [ #  # ]:          0 :                 if (unlink(rename_head->old))
     201                 :          0 :                         ohshite(_("cannot remove '%.250s'"), rename_head->old);
     202                 :          0 :                 rename_node_free(rename_head);
     203                 :          0 :                 rename_head = next;
     204                 :            :         }
     205                 :          0 : }
     206                 :            : 
     207                 :            : static void
     208                 :          0 : pkg_infodb_upgrade_to_multiarch(void)
     209                 :            : {
     210                 :            :         struct atomic_file *db_file;
     211                 :            :         char *db_format_file;
     212                 :            : 
     213                 :          0 :         db_format_file = dpkg_db_get_path(INFODIR "/format");
     214                 :          0 :         db_file = atomic_file_new(db_format_file, ATOMIC_FILE_MKPATH);
     215                 :          0 :         atomic_file_open(db_file);
     216                 :            : 
     217                 :          0 :         push_cleanup(cu_abort_db_upgrade, ehflag_bombout, 1, db_file);
     218                 :            : 
     219                 :          0 :         pkg_infodb_link_multiarch_files();
     220                 :          0 :         pkg_infodb_write_format(db_file, 1);
     221                 :            : 
     222                 :          0 :         pkg_infodb_unlink_monoarch_files();
     223                 :          0 :         atomic_file_commit(db_file);
     224                 :          0 :         dir_sync_path(pkg_infodb_get_dir());
     225                 :            : 
     226                 :          0 :         pop_cleanup(ehflag_normaltidy);
     227                 :            : 
     228                 :          0 :         atomic_file_free(db_file);
     229                 :          0 :         free(db_format_file);
     230                 :          0 : }
     231                 :            : 
     232                 :            : /**
     233                 :            :  * Upgrade the infodb if there's the need and possibility.
     234                 :            :  *
     235                 :            :  * Currently this implies, that the modstatdb was opened for writing and:
     236                 :            :  * - previous upgrade has not been completed; or
     237                 :            :  * - current format is not the latest one.
     238                 :            :  */
     239                 :            : void
     240                 :          0 : pkg_infodb_upgrade(void)
     241                 :            : {
     242                 :            :         enum pkg_infodb_format db_format;
     243                 :            : 
     244                 :            :         /* Make sure to always read and verify the format version. */
     245                 :          0 :         db_format = pkg_infodb_get_format();
     246                 :            : 
     247         [ #  # ]:          0 :         if (modstatdb_get_status() < msdbrw_write)
     248                 :          0 :                 return;
     249                 :            : 
     250   [ #  #  #  # ]:          0 :         if (db_format < PKG_INFODB_FORMAT_MULTIARCH ||
     251                 :          0 :             pkg_infodb_is_upgrading())
     252                 :          0 :                 pkg_infodb_upgrade_to_multiarch();
     253                 :            : }

Generated by: LCOV version 1.16