LCOV - code coverage report
Current view: top level - external/libfetch - file.c (source / functions) Hit Total Coverage
Test: rapport Lines: 0 60 0.0 %
Date: 2021-12-10 16:22:55 Functions: 0 6 0.0 %
Branches: 0 28 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*-
       2                 :            :  * SPDX-License-Identifier: BSD-3-Clause
       3                 :            :  *
       4                 :            :  * Copyright (c) 1998-2011 Dag-Erling Smørgrav
       5                 :            :  * All rights reserved.
       6                 :            :  *
       7                 :            :  * Redistribution and use in source and binary forms, with or without
       8                 :            :  * modification, are permitted provided that the following conditions
       9                 :            :  * are met:
      10                 :            :  * 1. Redistributions of source code must retain the above copyright
      11                 :            :  *    notice, this list of conditions and the following disclaimer
      12                 :            :  *    in this position and unchanged.
      13                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      14                 :            :  *    notice, this list of conditions and the following disclaimer in the
      15                 :            :  *    documentation and/or other materials provided with the distribution.
      16                 :            :  * 3. The name of the author may not be used to endorse or promote products
      17                 :            :  *    derived from this software without specific prior written permission
      18                 :            :  *
      19                 :            :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
      20                 :            :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      21                 :            :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      22                 :            :  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
      23                 :            :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      24                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      25                 :            :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      26                 :            :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      27                 :            :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      28                 :            :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      29                 :            :  */
      30                 :            : 
      31                 :            : #include <sys/cdefs.h>
      32                 :            : #include "bsd_compat.h"
      33                 :            : __FBSDID("$FreeBSD: head/lib/libfetch/file.c 326219 2017-11-26 02:00:33Z pfg $");
      34                 :            : 
      35                 :            : #include <sys/param.h>
      36                 :            : #include <sys/stat.h>
      37                 :            : 
      38                 :            : #include <dirent.h>
      39                 :            : #include <fcntl.h>
      40                 :            : #include <stdio.h>
      41                 :            : #include <string.h>
      42                 :            : 
      43                 :            : #include "fetch.h"
      44                 :            : #include "common.h"
      45                 :            : 
      46                 :            : FILE *
      47                 :          0 : fetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
      48                 :            : {
      49                 :            :         FILE *f;
      50                 :            : 
      51   [ #  #  #  # ]:          0 :         if (us && fetchStatFile(u, us, flags) == -1)
      52                 :          0 :                 return (NULL);
      53                 :            : 
      54                 :          0 :         f = fopen(u->doc, "re");
      55                 :            : 
      56         [ #  # ]:          0 :         if (f == NULL) {
      57                 :          0 :                 fetch_syserr();
      58                 :          0 :                 return (NULL);
      59                 :            :         }
      60                 :            : 
      61   [ #  #  #  # ]:          0 :         if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
      62                 :          0 :                 fclose(f);
      63                 :          0 :                 fetch_syserr();
      64                 :          0 :                 return (NULL);
      65                 :            :         }
      66                 :            : 
      67                 :          0 :         return (f);
      68                 :          0 : }
      69                 :            : 
      70                 :            : FILE *
      71                 :          0 : fetchGetFile(struct url *u, const char *flags)
      72                 :            : {
      73                 :          0 :         return (fetchXGetFile(u, NULL, flags));
      74                 :            : }
      75                 :            : 
      76                 :            : FILE *
      77                 :          0 : fetchPutFile(struct url *u, const char *flags)
      78                 :            : {
      79                 :            :         FILE *f;
      80                 :            : 
      81   [ #  #  #  # ]:          0 :         if (CHECK_FLAG('a'))
      82                 :          0 :                 f = fopen(u->doc, "ae");
      83                 :            :         else
      84                 :          0 :                 f = fopen(u->doc, "w+e");
      85                 :            : 
      86         [ #  # ]:          0 :         if (f == NULL) {
      87                 :          0 :                 fetch_syserr();
      88                 :          0 :                 return (NULL);
      89                 :            :         }
      90                 :            : 
      91   [ #  #  #  # ]:          0 :         if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
      92                 :          0 :                 fclose(f);
      93                 :          0 :                 fetch_syserr();
      94                 :          0 :                 return (NULL);
      95                 :            :         }
      96                 :            : 
      97                 :          0 :         return (f);
      98                 :          0 : }
      99                 :            : 
     100                 :            : static int
     101                 :          0 : fetch_stat_file(const char *fn, struct url_stat *us)
     102                 :            : {
     103                 :            :         struct stat sb;
     104                 :            : 
     105                 :          0 :         us->size = -1;
     106                 :          0 :         us->atime = us->mtime = 0;
     107         [ #  # ]:          0 :         if (stat(fn, &sb) == -1) {
     108                 :          0 :                 fetch_syserr();
     109                 :          0 :                 return (-1);
     110                 :            :         }
     111                 :          0 :         us->size = sb.st_size;
     112                 :          0 :         us->atime = sb.st_atime;
     113                 :          0 :         us->mtime = sb.st_mtime;
     114                 :          0 :         return (0);
     115                 :          0 : }
     116                 :            : 
     117                 :            : int
     118                 :          0 : fetchStatFile(struct url *u, struct url_stat *us, const char *flags __unused)
     119                 :            : {
     120                 :          0 :         return (fetch_stat_file(u->doc, us));
     121                 :            : }
     122                 :            : 
     123                 :            : struct url_ent *
     124                 :          0 : fetchListFile(struct url *u, const char *flags __unused)
     125                 :            : {
     126                 :            :         struct dirent *de;
     127                 :            :         struct url_stat us;
     128                 :            :         struct url_ent *ue;
     129                 :            :         int size, len;
     130                 :            :         char fn[PATH_MAX], *p;
     131                 :            :         DIR *dir;
     132                 :            :         int l;
     133                 :            : 
     134         [ #  # ]:          0 :         if ((dir = opendir(u->doc)) == NULL) {
     135                 :          0 :                 fetch_syserr();
     136                 :          0 :                 return (NULL);
     137                 :            :         }
     138                 :            : 
     139                 :          0 :         ue = NULL;
     140                 :          0 :         strncpy(fn, u->doc, sizeof(fn) - 2);
     141                 :          0 :         fn[sizeof(fn) - 2] = 0;
     142                 :          0 :         strcat(fn, "/");
     143                 :          0 :         p = strchr(fn, 0);
     144                 :          0 :         l = sizeof(fn) - strlen(fn) - 1;
     145                 :            : 
     146         [ #  # ]:          0 :         while ((de = readdir(dir)) != NULL) {
     147                 :          0 :                 strncpy(p, de->d_name, l - 1);
     148                 :          0 :                 p[l - 1] = 0;
     149         [ #  # ]:          0 :                 if (fetch_stat_file(fn, &us) == -1)
     150                 :            :                         /* should I return a partial result, or abort? */
     151                 :          0 :                         break;
     152                 :          0 :                 fetch_add_entry(&ue, &size, &len, de->d_name, &us);
     153                 :            :         }
     154                 :            : 
     155                 :          0 :         closedir(dir);
     156                 :          0 :         return (ue);
     157                 :          0 : }

Generated by: LCOV version 1.15