LCOV - code coverage report
Current view: top level - lib/dpkg - ar.c (source / functions) Coverage Total Hit
Test: dpkg 1.22.7-3-g89f48 C code coverage Lines: 80.8 % 104 84
Test Date: 2024-07-17 02:53:43 Functions: 100.0 % 13 13
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 56.9 % 58 33

             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                 :          96 : dpkg_ar_fdopen(const char *filename, int fd)
      42                 :             : {
      43                 :             :         struct dpkg_ar *ar;
      44                 :             :         struct stat st;
      45                 :             : 
      46         [ -  + ]:          96 :         if (fstat(fd, &st) != 0)
      47                 :           0 :                 ohshite(_("failed to fstat archive"));
      48                 :             : 
      49                 :          96 :         ar = m_malloc(sizeof(*ar));
      50                 :          96 :         ar->name = filename;
      51                 :          96 :         ar->mode = st.st_mode;
      52                 :          96 :         ar->size = st.st_size;
      53                 :          96 :         ar->time = st.st_mtime;
      54                 :          96 :         ar->fd = fd;
      55                 :             : 
      56                 :          96 :         return ar;
      57                 :             : }
      58                 :             : 
      59                 :             : struct dpkg_ar *
      60                 :          68 : dpkg_ar_open(const char *filename)
      61                 :             : {
      62                 :             :         int fd;
      63                 :             : 
      64         [ -  + ]:          68 :         if (strcmp(filename, "-") == 0)
      65                 :           0 :                 fd = STDIN_FILENO;
      66                 :             :         else
      67                 :          68 :                 fd = open(filename, O_RDONLY);
      68         [ -  + ]:          68 :         if (fd < 0)
      69                 :           0 :                 ohshite(_("failed to read archive '%.255s'"), filename);
      70                 :             : 
      71                 :          68 :         return dpkg_ar_fdopen(filename, fd);
      72                 :             : }
      73                 :             : 
      74                 :             : struct dpkg_ar *
      75                 :          28 : dpkg_ar_create(const char *filename, mode_t mode)
      76                 :             : {
      77                 :             :         int fd;
      78                 :             : 
      79                 :          28 :         fd = creat(filename, mode);
      80         [ -  + ]:          28 :         if (fd < 0)
      81                 :           0 :                 ohshite(_("unable to create '%.255s'"), filename);
      82                 :             : 
      83                 :          28 :         return dpkg_ar_fdopen(filename, fd);
      84                 :             : }
      85                 :             : 
      86                 :             : void
      87                 :          28 : dpkg_ar_set_mtime(struct dpkg_ar *ar, intmax_t mtime)
      88                 :             : {
      89                 :          28 :         ar->time = mtime;
      90                 :          28 : }
      91                 :             : 
      92                 :             : void
      93                 :         119 : dpkg_ar_close(struct dpkg_ar *ar)
      94                 :             : {
      95         [ -  + ]:         119 :         if (close(ar->fd))
      96                 :           0 :                 ohshite(_("unable to close file '%s'"), ar->name);
      97                 :         119 :         free(ar);
      98                 :         119 : }
      99                 :             : 
     100                 :             : static void
     101                 :          70 : dpkg_ar_member_init(struct dpkg_ar *ar, struct dpkg_ar_member *member,
     102                 :             :                     const char *name, off_t size)
     103                 :             : {
     104                 :          70 :         member->name = name;
     105                 :          70 :         member->size = size;
     106                 :          70 :         member->time = ar->time;
     107                 :          70 :         member->mode = 0100644;
     108                 :          70 :         member->uid = 0;
     109                 :          70 :         member->gid = 0;
     110                 :          70 : }
     111                 :             : 
     112                 :             : void
     113                 :         128 : dpkg_ar_normalize_name(struct dpkg_ar_hdr *arh)
     114                 :             : {
     115                 :         128 :         char *name = arh->ar_name;
     116                 :             :         int i;
     117                 :             : 
     118                 :             :         /* Remove trailing spaces from the member name. */
     119   [ +  -  +  + ]:         723 :         for (i = sizeof(arh->ar_name) - 1; i >= 0 && name[i] == ' '; i--)
     120                 :         595 :                 name[i] = '\0';
     121                 :             : 
     122                 :             :         /* Remove optional slash terminator (on GNU-style archives). */
     123   [ +  -  +  + ]:         128 :         if (i >= 0 && name[i] == '/')
     124                 :           1 :                 name[i] = '\0';
     125                 :         128 : }
     126                 :             : 
     127                 :             : off_t
     128                 :         124 : dpkg_ar_member_get_size(struct dpkg_ar *ar, struct dpkg_ar_hdr *arh)
     129                 :             : {
     130                 :         124 :         const char *str = arh->ar_size;
     131                 :         124 :         int len = sizeof(arh->ar_size);
     132                 :         124 :         off_t size = 0;
     133                 :             : 
     134   [ +  -  -  + ]:         124 :         while (len && *str == ' ')
     135                 :           0 :                 str++, len--;
     136                 :             : 
     137         [ +  - ]:         527 :         while (len--) {
     138         [ +  + ]:         527 :                 if (*str == ' ')
     139                 :         124 :                         break;
     140   [ +  -  -  + ]:         403 :                 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                 :         403 :                 size *= 10;
     146                 :         403 :                 size += *str++ - '0';
     147                 :             :         }
     148                 :             : 
     149                 :         124 :         return size;
     150                 :             : }
     151                 :             : 
     152                 :             : bool
     153                 :         127 : dpkg_ar_member_is_illegal(struct dpkg_ar_hdr *arh)
     154                 :             : {
     155                 :         127 :         return memcmp(arh->ar_fmag, DPKG_AR_FMAG, sizeof(arh->ar_fmag)) != 0;
     156                 :             : }
     157                 :             : 
     158                 :             : void
     159                 :          27 : dpkg_ar_put_magic(struct dpkg_ar *ar)
     160                 :             : {
     161         [ -  + ]:          27 :         if (fd_write(ar->fd, DPKG_AR_MAGIC, strlen(DPKG_AR_MAGIC)) < 0)
     162                 :           0 :                 ohshite(_("unable to write file '%s'"), ar->name);
     163                 :          27 : }
     164                 :             : 
     165                 :             : void
     166                 :          70 : 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         [ -  + ]:          70 :         if (strlen(member->name) > 15)
     172                 :           0 :                 ohshit(_("ar member name '%s' length too long"), member->name);
     173         [ -  + ]:          70 :         if (member->size > 9999999999L)
     174                 :           0 :                 ohshit(_("ar member size %jd too large"), (intmax_t)member->size);
     175         [ -  + ]:          70 :         if (member->time > 999999999999L)
     176                 :           0 :                 ohshit(_("ar member time %jd too large"), (intmax_t)member->time);
     177                 :             : 
     178                 :          70 :         n = snprintf(header, sizeof(struct dpkg_ar_hdr) + 1,
     179                 :             :                      "%-16s%-12jd%-6lu%-6lu%-8lo%-10jd`\n",
     180                 :          70 :                      member->name, (intmax_t)member->time,
     181                 :          70 :                      (unsigned long)member->uid, (unsigned long)member->gid,
     182                 :          70 :                      (unsigned long)member->mode, (intmax_t)member->size);
     183         [ -  + ]:          70 :         if (n != sizeof(struct dpkg_ar_hdr))
     184                 :           0 :                 ohshit(_("generated corrupt ar header for '%s'"), ar->name);
     185                 :             : 
     186         [ -  + ]:          70 :         if (fd_write(ar->fd, header, n) < 0)
     187                 :           0 :                 ohshite(_("unable to write file '%s'"), ar->name);
     188                 :          70 : }
     189                 :             : 
     190                 :             : void
     191                 :          27 : 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                 :          27 :         dpkg_ar_member_init(ar, &member, name, size);
     197                 :          27 :         dpkg_ar_member_put_header(ar, &member);
     198                 :             : 
     199                 :             :         /* Copy data contents. */
     200         [ -  + ]:          27 :         if (fd_write(ar->fd, data, size) < 0)
     201                 :           0 :                 ohshite(_("unable to write file '%s'"), ar->name);
     202                 :             : 
     203         [ +  + ]:          27 :         if (size & 1)
     204         [ -  + ]:           9 :                 if (fd_write(ar->fd, "\n", 1) < 0)
     205                 :           0 :                         ohshite(_("unable to write file '%s'"), ar->name);
     206                 :          27 : }
     207                 :             : 
     208                 :             : void
     209                 :          43 : 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         [ +  + ]:          43 :         if (size <= 0) {
     216                 :             :                 struct stat st;
     217                 :             : 
     218         [ -  + ]:          33 :                 if (fstat(fd, &st))
     219                 :           0 :                         ohshite(_("failed to fstat ar member file (%s)"), name);
     220                 :          33 :                 size = st.st_size;
     221                 :             :         }
     222                 :             : 
     223                 :          43 :         dpkg_ar_member_init(ar, &member, name, size);
     224                 :          43 :         dpkg_ar_member_put_header(ar, &member);
     225                 :             : 
     226                 :             :         /* Copy data contents. */
     227         [ -  + ]:          43 :         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         [ -  + ]:          43 :         if (size & 1)
     232         [ #  # ]:           0 :                 if (fd_write(ar->fd, "\n", 1) < 0)
     233                 :           0 :                         ohshite(_("unable to write file '%s'"), ar->name);
     234                 :          43 : }
        

Generated by: LCOV version 2.0-1