LCOV - code coverage report
Current view: top level - lib/dpkg - ar.c (source / functions) Hit Total Coverage
Test: dpkg 1.21.11 C code coverage Lines: 84 104 80.8 %
Date: 2022-12-03 00:40:01 Functions: 13 13 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 33 58 56.9 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * libdpkg - Debian packaging suite library routines
       3                 :            :  * ar.c - primitives for ar handling
       4                 :            :  *
       5                 :            :  * Copyright © 2010 Guillem Jover <guillem@debian.org>
       6                 :            :  *
       7                 :            :  * This is free software; you can redistribute it and/or modify
       8                 :            :  * it under the terms of the GNU General Public License as published by
       9                 :            :  * the Free Software Foundation; either version 2 of the License, or
      10                 :            :  * (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      19                 :            :  */
      20                 :            : 
      21                 :            : #include <config.h>
      22                 :            : #include <compat.h>
      23                 :            : 
      24                 :            : #include <sys/types.h>
      25                 :            : #include <sys/stat.h>
      26                 :            : 
      27                 :            : #include <time.h>
      28                 :            : #include <fcntl.h>
      29                 :            : #include <stdint.h>
      30                 :            : #include <stdlib.h>
      31                 :            : #include <string.h>
      32                 :            : #include <unistd.h>
      33                 :            : 
      34                 :            : #include <dpkg/i18n.h>
      35                 :            : #include <dpkg/dpkg.h>
      36                 :            : #include <dpkg/fdio.h>
      37                 :            : #include <dpkg/buffer.h>
      38                 :            : #include <dpkg/ar.h>
      39                 :            : 
      40                 :            : struct dpkg_ar *
      41                 :         77 : dpkg_ar_fdopen(const char *filename, int fd)
      42                 :            : {
      43                 :            :         struct dpkg_ar *ar;
      44                 :            :         struct stat st;
      45                 :            : 
      46         [ -  + ]:         77 :         if (fstat(fd, &st) != 0)
      47                 :          0 :                 ohshite(_("failed to fstat archive"));
      48                 :            : 
      49                 :         77 :         ar = m_malloc(sizeof(*ar));
      50                 :         77 :         ar->name = filename;
      51                 :         77 :         ar->mode = st.st_mode;
      52                 :         77 :         ar->size = st.st_size;
      53                 :         77 :         ar->time = st.st_mtime;
      54                 :         77 :         ar->fd = fd;
      55                 :            : 
      56                 :         77 :         return ar;
      57                 :            : }
      58                 :            : 
      59                 :            : struct dpkg_ar *
      60                 :         57 : dpkg_ar_open(const char *filename)
      61                 :            : {
      62                 :            :         int fd;
      63                 :            : 
      64         [ -  + ]:         57 :         if (strcmp(filename, "-") == 0)
      65                 :          0 :                 fd = STDIN_FILENO;
      66                 :            :         else
      67                 :         57 :                 fd = open(filename, O_RDONLY);
      68         [ -  + ]:         57 :         if (fd < 0)
      69                 :          0 :                 ohshite(_("failed to read archive '%.255s'"), filename);
      70                 :            : 
      71                 :         57 :         return dpkg_ar_fdopen(filename, fd);
      72                 :            : }
      73                 :            : 
      74                 :            : struct dpkg_ar *
      75                 :         20 : dpkg_ar_create(const char *filename, mode_t mode)
      76                 :            : {
      77                 :            :         int fd;
      78                 :            : 
      79                 :         20 :         fd = creat(filename, mode);
      80         [ -  + ]:         20 :         if (fd < 0)
      81                 :          0 :                 ohshite(_("unable to create '%.255s'"), filename);
      82                 :            : 
      83                 :         20 :         return dpkg_ar_fdopen(filename, fd);
      84                 :            : }
      85                 :            : 
      86                 :            : void
      87                 :         20 : dpkg_ar_set_mtime(struct dpkg_ar *ar, intmax_t mtime)
      88                 :            : {
      89                 :         20 :         ar->time = mtime;
      90                 :         20 : }
      91                 :            : 
      92                 :            : void
      93                 :         63 : dpkg_ar_close(struct dpkg_ar *ar)
      94                 :            : {
      95         [ -  + ]:         63 :         if (close(ar->fd))
      96                 :          0 :                 ohshite(_("unable to close file '%s'"), ar->name);
      97                 :         63 :         free(ar);
      98                 :         63 : }
      99                 :            : 
     100                 :            : static void
     101                 :         46 : dpkg_ar_member_init(struct dpkg_ar *ar, struct dpkg_ar_member *member,
     102                 :            :                     const char *name, off_t size)
     103                 :            : {
     104                 :         46 :         member->name = name;
     105                 :         46 :         member->size = size;
     106                 :         46 :         member->time = ar->time;
     107                 :         46 :         member->mode = 0100644;
     108                 :         46 :         member->uid = 0;
     109                 :         46 :         member->gid = 0;
     110                 :         46 : }
     111                 :            : 
     112                 :            : void
     113                 :        102 : dpkg_ar_normalize_name(struct dpkg_ar_hdr *arh)
     114                 :            : {
     115                 :        102 :         char *name = arh->ar_name;
     116                 :            :         int i;
     117                 :            : 
     118                 :            :         /* Remove trailing spaces from the member name. */
     119   [ +  -  +  + ]:        544 :         for (i = sizeof(arh->ar_name) - 1; i >= 0 && name[i] == ' '; i--)
     120                 :        442 :                 name[i] = '\0';
     121                 :            : 
     122                 :            :         /* Remove optional slash terminator (on GNU-style archives). */
     123   [ +  -  +  + ]:        102 :         if (i >= 0 && name[i] == '/')
     124                 :         65 :                 name[i] = '\0';
     125                 :        102 : }
     126                 :            : 
     127                 :            : off_t
     128                 :         98 : dpkg_ar_member_get_size(struct dpkg_ar *ar, struct dpkg_ar_hdr *arh)
     129                 :            : {
     130                 :         98 :         const char *str = arh->ar_size;
     131                 :         98 :         int len = sizeof(arh->ar_size);
     132                 :         98 :         off_t size = 0;
     133                 :            : 
     134   [ +  -  -  + ]:         98 :         while (len && *str == ' ')
     135                 :          0 :                 str++, len--;
     136                 :            : 
     137         [ +  - ]:        396 :         while (len--) {
     138         [ +  + ]:        396 :                 if (*str == ' ')
     139                 :         98 :                         break;
     140   [ +  -  -  + ]:        298 :                 if (*str < '0' || *str > '9')
     141                 :          0 :                         ohshit(_("invalid character '%c' in archive '%.250s' "
     142                 :            :                                  "member '%.16s' size"),
     143                 :          0 :                                *str, ar->name, arh->ar_name);
     144                 :            : 
     145                 :        298 :                 size *= 10;
     146                 :        298 :                 size += *str++ - '0';
     147                 :            :         }
     148                 :            : 
     149                 :         98 :         return size;
     150                 :            : }
     151                 :            : 
     152                 :            : bool
     153                 :        101 : dpkg_ar_member_is_illegal(struct dpkg_ar_hdr *arh)
     154                 :            : {
     155                 :        101 :         return memcmp(arh->ar_fmag, DPKG_AR_FMAG, sizeof(arh->ar_fmag)) != 0;
     156                 :            : }
     157                 :            : 
     158                 :            : void
     159                 :         19 : dpkg_ar_put_magic(struct dpkg_ar *ar)
     160                 :            : {
     161         [ -  + ]:         19 :         if (fd_write(ar->fd, DPKG_AR_MAGIC, strlen(DPKG_AR_MAGIC)) < 0)
     162                 :          0 :                 ohshite(_("unable to write file '%s'"), ar->name);
     163                 :         19 : }
     164                 :            : 
     165                 :            : void
     166                 :         46 : dpkg_ar_member_put_header(struct dpkg_ar *ar, struct dpkg_ar_member *member)
     167                 :            : {
     168                 :            :         char header[sizeof(struct dpkg_ar_hdr) + 1];
     169                 :            :         int n;
     170                 :            : 
     171         [ -  + ]:         46 :         if (strlen(member->name) > 15)
     172                 :          0 :                 ohshit(_("ar member name '%s' length too long"), member->name);
     173         [ -  + ]:         46 :         if (member->size > 9999999999L)
     174                 :          0 :                 ohshit(_("ar member size %jd too large"), (intmax_t)member->size);
     175         [ -  + ]:         46 :         if (member->time > 999999999999L)
     176                 :          0 :                 ohshit(_("ar member time %jd too large"), (intmax_t)member->time);
     177                 :            : 
     178                 :         46 :         n = snprintf(header, sizeof(struct dpkg_ar_hdr) + 1,
     179                 :            :                      "%-16s%-12jd%-6lu%-6lu%-8lo%-10jd`\n",
     180                 :         46 :                      member->name, (intmax_t)member->time,
     181                 :         46 :                      (unsigned long)member->uid, (unsigned long)member->gid,
     182                 :         46 :                      (unsigned long)member->mode, (intmax_t)member->size);
     183         [ -  + ]:         46 :         if (n != sizeof(struct dpkg_ar_hdr))
     184                 :          0 :                 ohshit(_("generated corrupt ar header for '%s'"), ar->name);
     185                 :            : 
     186         [ -  + ]:         46 :         if (fd_write(ar->fd, header, n) < 0)
     187                 :          0 :                 ohshite(_("unable to write file '%s'"), ar->name);
     188                 :         46 : }
     189                 :            : 
     190                 :            : void
     191                 :         19 : dpkg_ar_member_put_mem(struct dpkg_ar *ar,
     192                 :            :                        const char *name, const void *data, size_t size)
     193                 :            : {
     194                 :            :         struct dpkg_ar_member member;
     195                 :            : 
     196                 :         19 :         dpkg_ar_member_init(ar, &member, name, size);
     197                 :         19 :         dpkg_ar_member_put_header(ar, &member);
     198                 :            : 
     199                 :            :         /* Copy data contents. */
     200         [ -  + ]:         19 :         if (fd_write(ar->fd, data, size) < 0)
     201                 :          0 :                 ohshite(_("unable to write file '%s'"), ar->name);
     202                 :            : 
     203         [ +  + ]:         19 :         if (size & 1)
     204         [ -  + ]:          9 :                 if (fd_write(ar->fd, "\n", 1) < 0)
     205                 :          0 :                         ohshite(_("unable to write file '%s'"), ar->name);
     206                 :         19 : }
     207                 :            : 
     208                 :            : void
     209                 :         27 : dpkg_ar_member_put_file(struct dpkg_ar *ar,
     210                 :            :                         const char *name, int fd, off_t size)
     211                 :            : {
     212                 :            :         struct dpkg_error err;
     213                 :            :         struct dpkg_ar_member member;
     214                 :            : 
     215         [ +  + ]:         27 :         if (size <= 0) {
     216                 :            :                 struct stat st;
     217                 :            : 
     218         [ -  + ]:         17 :                 if (fstat(fd, &st))
     219                 :          0 :                         ohshite(_("failed to fstat ar member file (%s)"), name);
     220                 :         17 :                 size = st.st_size;
     221                 :            :         }
     222                 :            : 
     223                 :         27 :         dpkg_ar_member_init(ar, &member, name, size);
     224                 :         27 :         dpkg_ar_member_put_header(ar, &member);
     225                 :            : 
     226                 :            :         /* Copy data contents. */
     227         [ -  + ]:         27 :         if (fd_fd_copy(fd, ar->fd, size, &err) < 0)
     228                 :          0 :                 ohshit(_("cannot append ar member file (%s) to '%s': %s"),
     229                 :            :                        name, ar->name, err.str);
     230                 :            : 
     231         [ -  + ]:         27 :         if (size & 1)
     232         [ #  # ]:          0 :                 if (fd_write(ar->fd, "\n", 1) < 0)
     233                 :          0 :                         ohshite(_("unable to write file '%s'"), ar->name);
     234                 :         27 : }

Generated by: LCOV version 1.16