LCOV - code coverage report
Current view: top level - src - filesystem.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 64 64 100.0 %
Date: 2026-11-26 02:32:31 Functions: 4 4 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 44 52 84.6 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2007-2025 David Robillard <d@drobilla.net>
       2                 :            : // SPDX-License-Identifier: ISC
       3                 :            : 
       4                 :            : #include <zix/filesystem.h>
       5                 :            : 
       6                 :            : #include "path_iter.h"
       7                 :            : #include "system.h"
       8                 :            : 
       9                 :            : #include <zix/allocator.h>
      10                 :            : #include <zix/status.h>
      11                 :            : 
      12                 :            : #include <fcntl.h>
      13                 :            : #include <sys/stat.h>
      14                 :            : #include <sys/types.h>
      15                 :            : 
      16                 :            : #include <errno.h>
      17                 :            : #include <stdbool.h>
      18                 :            : #include <string.h>
      19                 :            : 
      20                 :            : ZixStatus
      21                 :          4 : zix_create_directories(ZixAllocator* const allocator,
      22                 :            :                        const char* const   dir_path)
      23                 :            : {
      24         [ +  + ]:          4 :   if (!dir_path[0]) {
      25                 :          1 :     return ZIX_STATUS_BAD_ARG;
      26                 :            :   }
      27                 :            : 
      28                 :            :   // Allocate a working copy of the path to chop along the way
      29                 :          3 :   const size_t path_len = strlen(dir_path);
      30                 :          3 :   char* const  path     = (char*)zix_malloc(allocator, path_len + 1U);
      31         [ +  + ]:          3 :   if (!path) {
      32                 :          1 :     return ZIX_STATUS_NO_MEM;
      33                 :            :   }
      34                 :            : 
      35                 :            :   // Copy directory path as prefix
      36                 :          2 :   memcpy(path, dir_path, path_len + 1U);
      37                 :            : 
      38                 :            :   // Start at the root directory (past any name)
      39                 :          2 :   ZixPathIter p = zix_path_begin(path);
      40         [ +  + ]:          4 :   while (p.state < ZIX_PATH_FILE_NAME) {
      41                 :          2 :     p = zix_path_next(path, p);
      42                 :            :   }
      43                 :            : 
      44                 :            :   // Create each directory down the path
      45                 :          2 :   ZixStatus st = ZIX_STATUS_SUCCESS;
      46   [ +  +  +  + ]:          9 :   while (!st && p.state != ZIX_PATH_END) {
      47                 :          7 :     char* const end      = &path[p.range.end];
      48                 :          7 :     const char  old_last = *end;
      49                 :            : 
      50                 :          7 :     *end = '\0';
      51         [ +  + ]:          7 :     if (zix_file_type(path) != ZIX_FILE_TYPE_DIRECTORY) {
      52                 :          3 :       st = zix_create_directory(path);
      53                 :            :     }
      54                 :            : 
      55                 :          7 :     *end = old_last;
      56                 :          7 :     p    = zix_path_next(path, p);
      57                 :            :   }
      58                 :            : 
      59                 :          2 :   zix_free(allocator, path);
      60                 :          2 :   return st;
      61                 :            : }
      62                 :            : 
      63                 :            : ZixFileOffset
      64                 :          4 : zix_file_size(const char* const path)
      65                 :            : {
      66                 :          4 :   struct stat sb = {0};
      67         [ +  + ]:          4 :   return stat(path, &sb) ? (off_t)0 : sb.st_size;
      68                 :            : }
      69                 :            : 
      70                 :            : // Wrapper for read() that transparently deals with short reads
      71                 :            : static ssize_t
      72                 :         30 : full_read(const int fd, void* const buf, const size_t count)
      73                 :            : {
      74                 :         30 :   size_t  n = 0;
      75                 :         30 :   ssize_t r = 0;
      76                 :            : 
      77   [ +  +  +  - ]:         90 :   while (n < count &&
      78                 :         30 :          (r = zix_system_read(fd, (char*)buf + n, count - n)) > 0) {
      79                 :         30 :     n += (size_t)r;
      80                 :            :   }
      81                 :            : 
      82                 :         30 :   return (ssize_t)n;
      83                 :            : }
      84                 :            : 
      85                 :            : bool
      86                 :         36 : zix_file_equals(ZixAllocator* const allocator,
      87                 :            :                 const char* const   path_a,
      88                 :            :                 const char* const   path_b)
      89                 :            : {
      90         [ +  + ]:         36 :   if (!strcmp(path_a, path_b)) {
      91                 :          4 :     return true; // Paths match
      92                 :            :   }
      93                 :            : 
      94                 :         32 :   errno = 0;
      95                 :            : 
      96                 :            :   // Open files and get file information
      97                 :         32 :   const int   fd_a   = zix_system_open(path_a, O_RDONLY, 0);
      98                 :         32 :   const int   fd_b   = zix_system_open(path_b, O_RDONLY, 0);
      99                 :         32 :   struct stat stat_a = {0};
     100                 :         32 :   struct stat stat_b = {0};
     101   [ +  +  +  +  :         32 :   if (fd_a < 0 || fd_b < 0 || fstat(fd_a, &stat_a) || fstat(fd_b, &stat_b)) {
             +  -  -  + ]
     102                 :          4 :     zix_system_close_fds(fd_b, fd_a);
     103                 :          4 :     return false;
     104                 :            :   }
     105                 :            : 
     106                 :         28 :   bool match = false;
     107   [ +  +  +  - ]:         28 :   if (stat_a.st_dev == stat_b.st_dev && stat_a.st_ino &&
     108         [ +  + ]:         19 :       stat_a.st_ino == stat_b.st_ino) {
     109                 :          2 :     match = true; // Fast path: paths refer to the same file
     110         [ +  + ]:         26 :   } else if (stat_a.st_size == stat_b.st_size) {
     111                 :            :     // Slow path: files have equal size, compare contents
     112                 :            : 
     113                 :            :     // Allocate two blocks in a single buffer (to simplify error handling)
     114                 :         18 :     const uint32_t align  = zix_system_page_size();
     115                 :         18 :     const uint32_t size   = zix_system_max_block_size(&stat_a, &stat_b, align);
     116                 :         18 :     BlockBuffer    blocks = zix_system_new_block(allocator, align, 2U * size);
     117         [ +  + ]:         18 :     void* const    data   = blocks.buffer ? blocks.buffer : blocks.fallback;
     118                 :            : 
     119                 :            :     // Compare files a block at a time
     120                 :         18 :     const uint32_t block_size = blocks.size / 2U;
     121                 :         18 :     void* const    block_a    = data;
     122                 :         18 :     void* const    block_b    = (void*)((char*)data + block_size);
     123                 :         18 :     match                     = true;
     124   [ +  +  +  - ]:         48 :     for (ssize_t n = 0; n < stat_a.st_size && match;) {
     125                 :         30 :       const ssize_t r = zix_system_read(fd_a, block_a, block_size);
     126   [ +  -  +  - ]:         30 :       if (r <= 0 || full_read(fd_b, block_b, (uint32_t)r) != r ||
     127         [ +  + ]:         30 :           !!memcmp(block_a, block_b, (size_t)r)) {
     128                 :          8 :         match = false;
     129                 :            :       }
     130                 :         30 :       n += r;
     131                 :            :     }
     132                 :            : 
     133                 :         18 :     zix_system_free_block(allocator, blocks);
     134                 :            :   }
     135                 :            : 
     136   [ +  -  +  + ]:         28 :   return !zix_system_close_fds(fd_b, fd_a) && match;
     137                 :            : }

Generated by: LCOV version 1.16