LCOV - code coverage report
Current view: top level - src/posix - filesystem_posix.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 124 124 100.0 %
Date: 2026-11-26 02:32:31 Functions: 22 22 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 65 82 79.3 %

           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 "../errno_status.h"
       7                 :            : #include "../qualifiers.h"
       8                 :            : #include "../system.h"
       9                 :            : #include "../zix_config.h"
      10                 :            : 
      11                 :            : #include <zix/allocator.h>
      12                 :            : #include <zix/attributes.h>
      13                 :            : #include <zix/status.h>
      14                 :            : 
      15                 :            : #if USE_FLOCK && USE_FILENO
      16                 :            : #  include <sys/file.h>
      17                 :            : #endif
      18                 :            : 
      19                 :            : #if USE_CLONEFILE
      20                 :            : #  include <sys/attr.h>
      21                 :            : #  include <sys/clonefile.h>
      22                 :            : #endif
      23                 :            : 
      24                 :            : #if USE_REALPATH
      25                 :            : #  include <limits.h>
      26                 :            : #endif
      27                 :            : 
      28                 :            : #include <dirent.h>
      29                 :            : #include <fcntl.h>
      30                 :            : #include <stdint.h>
      31                 :            : #include <sys/stat.h>
      32                 :            : #include <sys/types.h>
      33                 :            : #include <unistd.h>
      34                 :            : 
      35                 :            : #include <errno.h>
      36                 :            : #include <stdbool.h>
      37                 :            : #include <stdio.h>
      38                 :            : #include <stdlib.h>
      39                 :            : #include <string.h>
      40                 :            : 
      41                 :            : static inline ZixStatus
      42                 :         65 : zix_posix_status(const int rc)
      43                 :            : {
      44         [ +  + ]:         65 :   return rc ? zix_errno_status(errno) : ZIX_STATUS_SUCCESS;
      45                 :            : }
      46                 :            : 
      47                 :            : static ZixStatus
      48                 :         16 : finish_copy(const int dst_fd, const int src_fd, const ZixStatus status)
      49                 :            : {
      50                 :            : #ifdef __APPLE__
      51                 :            :   const int rc = dst_fd >= 0 ? fcntl(dst_fd, F_FULLFSYNC) : 0;
      52                 :            : #else
      53         [ +  + ]:         16 :   const int rc = dst_fd >= 0 ? fdatasync(dst_fd) : 0;
      54                 :            : #endif
      55                 :            : 
      56                 :         16 :   const ZixStatus st0 = zix_posix_status(rc);
      57                 :         16 :   const ZixStatus st1 = zix_system_close_fds(dst_fd, src_fd);
      58                 :            : 
      59   [ +  +  -  + ]:         16 :   return status ? status : st0 ? st0 : st1;
      60                 :            : }
      61                 :            : 
      62                 :            : static char*
      63                 :         20 : copy_path(ZixAllocator* const allocator,
      64                 :            :           const char* const   path,
      65                 :            :           const size_t        length)
      66                 :            : {
      67                 :         20 :   char* result = NULL;
      68                 :            : 
      69         [ +  - ]:         20 :   if (path) {
      70                 :         20 :     result = (char*)zix_calloc(allocator, length + 1U, 1U);
      71         [ +  - ]:         20 :     if (result) {
      72                 :         20 :       memcpy(result, path, length + 1U);
      73                 :            :     }
      74                 :            :   }
      75                 :            : 
      76                 :         20 :   return result;
      77                 :            : }
      78                 :            : 
      79                 :            : #if !defined(PATH_MAX) && USE_PATHCONF
      80                 :            : 
      81                 :            : static size_t
      82                 :            : max_path_size(const char* const path)
      83                 :            : {
      84                 :            :   const long path_max = pathconf(path, _PC_PATH_MAX);
      85                 :            :   return (path_max > 0) ? (size_t)path_max : zix_system_page_size();
      86                 :            : }
      87                 :            : 
      88                 :            : #elif !defined(PATH_MAX)
      89                 :            : 
      90                 :            : static size_t
      91                 :            : max_path_size(const char* const path)
      92                 :            : {
      93                 :            :   (void)path;
      94                 :            : 
      95                 :            :   return zix_system_page_size();
      96                 :            : }
      97                 :            : 
      98                 :            : #endif
      99                 :            : 
     100                 :            : #if USE_CLONEFILE
     101                 :            : 
     102                 :            : static ZixStatus
     103                 :            : zix_clonefile(const char* const    src,
     104                 :            :               const char* const    dst,
     105                 :            :               const ZixCopyOptions options)
     106                 :            : {
     107                 :            :   errno = 0;
     108                 :            : 
     109                 :            :   ZixStatus         st        = ZIX_STATUS_SUCCESS;
     110                 :            :   const ZixFileType dst_type  = zix_file_type(dst);
     111                 :            :   const bool        overwrite = (options == ZIX_COPY_OPTION_OVERWRITE_EXISTING);
     112                 :            :   if (overwrite && dst_type == ZIX_FILE_TYPE_REGULAR) {
     113                 :            :     st = zix_remove(dst);
     114                 :            :   } else if (dst_type != ZIX_FILE_TYPE_NONE) {
     115                 :            :     st = ZIX_STATUS_NOT_SUPPORTED;
     116                 :            :   }
     117                 :            : 
     118                 :            :   return st ? st : zix_posix_status(clonefile(src, dst, 0));
     119                 :            : }
     120                 :            : 
     121                 :            : #endif
     122                 :            : 
     123                 :            : #if USE_COPY_FILE_RANGE
     124                 :            : 
     125                 :            : static ZixStatus
     126                 :          7 : zix_copy_file_range(const int src_fd, const int dst_fd, const size_t size)
     127                 :            : {
     128                 :          7 :   errno = 0;
     129                 :            : 
     130                 :          7 :   size_t  remaining = size;
     131                 :          7 :   ssize_t r         = 0;
     132   [ +  +  +  + ]:         15 :   while (remaining > 0 &&
     133                 :          7 :          (r = copy_file_range(src_fd, NULL, dst_fd, NULL, remaining, 0U)) > 0) {
     134                 :          1 :     remaining -= (size_t)r;
     135                 :            :   }
     136                 :            : 
     137                 :            :   return (r >= 0) ? ZIX_STATUS_SUCCESS
     138         [ +  + ]:         13 :                   : zix_errno_status(
     139   [ +  +  -  + ]:          6 :                       (errno == EXDEV || errno == EINVAL) ? ENOSYS : errno);
     140                 :            : }
     141                 :            : 
     142                 :            : #endif
     143                 :            : 
     144                 :            : static ZixStatus
     145                 :          6 : copy_blocks(const int    src_fd,
     146                 :            :             const int    dst_fd,
     147                 :            :             void* const  block,
     148                 :            :             const size_t block_size)
     149                 :            : {
     150                 :          6 :   ZixStatus st     = ZIX_STATUS_SUCCESS;
     151                 :          6 :   ssize_t   n_read = 0;
     152                 :            : 
     153   [ +  +  +  + ]:         12 :   while (!st && (n_read = read(src_fd, block, block_size)) > 0) {
     154         [ +  + ]:          6 :     if (write(dst_fd, block, (size_t)n_read) != n_read) {
     155                 :          3 :       st = zix_errno_status(errno);
     156                 :            :     }
     157                 :            :   }
     158                 :            : 
     159                 :          6 :   return st;
     160                 :            : }
     161                 :            : 
     162                 :            : ZixStatus
     163                 :         16 : zix_copy_file(ZixAllocator* const  allocator,
     164                 :            :               const char* const    src,
     165                 :            :               const char* const    dst,
     166                 :            :               const ZixCopyOptions options)
     167                 :            : {
     168                 :         16 :   ZixStatus st = ZIX_STATUS_SUCCESS;
     169                 :            : 
     170                 :         16 :   errno = 0;
     171                 :            : 
     172                 :            : #if USE_CLONEFILE
     173                 :            :   // Try to copy via the kernel on MacOS to take advantage of CoW
     174                 :            :   st = zix_clonefile(src, dst, options);
     175                 :            :   if (st != ZIX_STATUS_NOT_SUPPORTED) {
     176                 :            :     return st;
     177                 :            :   }
     178                 :            : #endif
     179                 :            : 
     180                 :            :   // Open source file and get its status
     181                 :         16 :   const int   src_fd   = zix_system_open(src, O_RDONLY, 0);
     182                 :         16 :   struct stat src_stat = {0};
     183   [ +  +  -  + ]:         16 :   if (src_fd < 0 || fstat(src_fd, &src_stat)) {
     184                 :          1 :     return finish_copy(-1, src_fd, zix_errno_status(errno));
     185                 :            :   }
     186                 :            : 
     187                 :            :   // Fail if the source is not a regular file (since we need a size)
     188         [ +  + ]:         15 :   if (!S_ISREG(src_stat.st_mode)) {
     189                 :          4 :     return finish_copy(-1, src_fd, ZIX_STATUS_BAD_ARG);
     190                 :            :   }
     191                 :            : 
     192                 :            :   // Open a new destination file
     193                 :         11 :   const bool  overwrite = (options == ZIX_COPY_OPTION_OVERWRITE_EXISTING);
     194         [ +  + ]:         11 :   const int   dst_flags = O_WRONLY | O_CREAT | (overwrite ? O_TRUNC : O_EXCL);
     195                 :         11 :   const int   dst_fd    = zix_system_open(dst, dst_flags, 0644);
     196                 :         11 :   struct stat dst_stat  = {0};
     197   [ +  +  -  + ]:         11 :   if (dst_fd < 0 || fstat(dst_fd, &dst_stat)) {
     198                 :          4 :     return finish_copy(dst_fd, src_fd, zix_errno_status(errno));
     199                 :            :   }
     200                 :            : 
     201                 :            : #if USE_COPY_FILE_RANGE
     202                 :            :   // Try to copy via the kernel on Linux/BSD to take advantage of CoW
     203                 :          7 :   st = zix_copy_file_range(src_fd, dst_fd, (size_t)src_stat.st_size);
     204         [ +  + ]:          7 :   if (st != ZIX_STATUS_NOT_SUPPORTED) {
     205                 :          1 :     return finish_copy(dst_fd, src_fd, st);
     206                 :            :   }
     207                 :            : #endif
     208                 :            : 
     209                 :            :   // Set sequential hints so the kernel can optimize the page cache
     210                 :            : #if USE_POSIX_FADVISE
     211                 :          6 :   (void)posix_fadvise(src_fd, 0, src_stat.st_size, POSIX_FADV_SEQUENTIAL);
     212                 :          6 :   (void)posix_fadvise(dst_fd, 0, src_stat.st_size, POSIX_FADV_SEQUENTIAL);
     213                 :            : #endif
     214                 :            : 
     215                 :          6 :   errno = 0;
     216                 :            : 
     217                 :            :   // Allocate a block and copy file content one block at a time
     218                 :          6 :   const uint32_t align = zix_system_page_size();
     219                 :          6 :   const uint32_t size  = zix_system_max_block_size(&src_stat, &dst_stat, align);
     220                 :          6 :   BlockBuffer    block = zix_system_new_block(allocator, align, size);
     221         [ +  - ]:          6 :   void* const    data  = block.buffer ? block.buffer : block.fallback;
     222                 :          6 :   st                   = copy_blocks(src_fd, dst_fd, data, block.size);
     223                 :            : 
     224                 :          6 :   zix_system_free_block(allocator, block);
     225                 :          6 :   return finish_copy(dst_fd, src_fd, st);
     226                 :            : }
     227                 :            : 
     228                 :            : ZixStatus
     229                 :          4 : zix_create_directory(const char* const dir_path)
     230                 :            : {
     231                 :          4 :   return (!dir_path[0]) ? ZIX_STATUS_BAD_ARG
     232         [ +  - ]:          4 :                         : zix_posix_status(mkdir(dir_path, 0777));
     233                 :            : }
     234                 :            : 
     235                 :            : ZixStatus
     236                 :          2 : zix_create_directory_like(const char* const dir_path,
     237                 :            :                           const char* const existing_path)
     238                 :            : {
     239                 :          2 :   struct stat sb = {0};
     240                 :          2 :   return !dir_path[0] ? ZIX_STATUS_BAD_ARG
     241   [ +  -  +  + ]:          4 :          : stat(existing_path, &sb)
     242                 :          1 :            ? zix_errno_status(errno)
     243                 :          1 :            : zix_posix_status(mkdir(dir_path, sb.st_mode));
     244                 :            : }
     245                 :            : 
     246                 :            : ZixStatus
     247                 :          1 : zix_create_hard_link(const char* const target_path, const char* const link_path)
     248                 :            : {
     249                 :          1 :   return zix_posix_status(link(target_path, link_path));
     250                 :            : }
     251                 :            : 
     252                 :            : ZixStatus
     253                 :          3 : zix_create_symlink(const char* const target_path, const char* const link_path)
     254                 :            : {
     255                 :          3 :   return zix_posix_status(symlink(target_path, link_path));
     256                 :            : }
     257                 :            : 
     258                 :            : ZixStatus
     259                 :          1 : zix_create_directory_symlink(const char* const target_path,
     260                 :            :                              const char* const link_path)
     261                 :            : {
     262                 :          1 :   return zix_create_symlink(target_path, link_path);
     263                 :            : }
     264                 :            : 
     265                 :            : char*
     266                 :         15 : zix_create_temporary_directory(ZixAllocator* const allocator,
     267                 :            :                                const char* const   path_pattern)
     268                 :            : {
     269                 :         15 :   const size_t length = strlen(path_pattern);
     270                 :         15 :   char* const  result = (char*)zix_calloc(allocator, length + 1U, 1U);
     271         [ +  - ]:         15 :   if (result) {
     272                 :         15 :     memcpy(result, path_pattern, length + 1U);
     273         [ +  + ]:         15 :     if (!mkdtemp(result)) {
     274                 :          1 :       zix_free(allocator, result);
     275                 :          1 :       return NULL;
     276                 :            :     }
     277                 :            :   }
     278                 :            : 
     279                 :         14 :   return result;
     280                 :            : }
     281                 :            : 
     282                 :            : ZixStatus
     283                 :         37 : zix_remove(const char* const path)
     284                 :            : {
     285                 :         37 :   return zix_posix_status(remove(path));
     286                 :            : }
     287                 :            : 
     288                 :            : void
     289                 :          1 : zix_dir_for_each(const char* const          path,
     290                 :            :                  void* const                data,
     291                 :            :                  const ZixDirEntryVisitFunc f)
     292                 :            : {
     293                 :          1 :   DIR* dir = opendir(path);
     294         [ +  - ]:          1 :   if (dir) {
     295                 :            :     // NOLINTNEXTLINE(concurrency-mt-unsafe)
     296         [ +  + ]:          5 :     for (struct dirent* entry = NULL; (entry = readdir(dir));) {
     297   [ +  +  +  + ]:          4 :       if (!!strcmp(entry->d_name, ".") && !!strcmp(entry->d_name, "..")) {
     298                 :          2 :         f(path, entry->d_name, data);
     299                 :            :       }
     300                 :            :     }
     301                 :          1 :     closedir(dir);
     302                 :            :   }
     303                 :          1 : }
     304                 :            : 
     305                 :            : char*
     306                 :          8 : zix_canonical_path(ZixAllocator* const allocator, const char* const path)
     307                 :            : {
     308         [ +  + ]:          8 :   if (!path) {
     309                 :          1 :     return NULL;
     310                 :            :   }
     311                 :            : 
     312                 :            : #if USE_REALPATH && defined(PATH_MAX)
     313                 :            :   // Some POSIX systems have a static PATH_MAX so we can resolve on the stack
     314                 :          7 :   char        buffer[PATH_MAX] = {0};
     315                 :          7 :   char* const canonical        = realpath(path, buffer);
     316         [ +  + ]:          7 :   if (canonical) {
     317                 :          4 :     return copy_path(allocator, buffer, strlen(buffer));
     318                 :            :   }
     319                 :            : 
     320                 :            : #elif USE_REALPATH && USE_PATHCONF
     321                 :            :   // Others don't so we have to query PATH_MAX at runtime to allocate the result
     322                 :            :   const size_t size      = max_path_size(path);
     323                 :            :   char* const  buffer    = (char*)zix_calloc(allocator, size, 1);
     324                 :            :   char* const  canonical = realpath(path, buffer);
     325                 :            :   if (canonical) {
     326                 :            :     return canonical;
     327                 :            :   }
     328                 :            : 
     329                 :            :   zix_free(allocator, buffer);
     330                 :            : 
     331                 :            : #else
     332                 :            :   (void)allocator;
     333                 :            : #endif
     334                 :            : 
     335                 :          3 :   return NULL;
     336                 :            : }
     337                 :            : 
     338                 :            : ZixStatus
     339                 :          2 : zix_file_lock(FILE* const file, const ZixFileLockMode mode)
     340                 :            : {
     341                 :            : #if USE_FLOCK && USE_FILENO
     342         [ -  + ]:          2 :   return zix_posix_status(
     343                 :            :     flock(fileno(file),
     344                 :            :           (mode == ZIX_FILE_LOCK_BLOCK) ? LOCK_EX : (LOCK_EX | LOCK_NB)));
     345                 :            : 
     346                 :            : #else
     347                 :            :   (void)file;
     348                 :            :   (void)mode;
     349                 :            :   return ZIX_STATUS_NOT_SUPPORTED;
     350                 :            : #endif
     351                 :            : }
     352                 :            : 
     353                 :            : ZixStatus
     354                 :          1 : zix_file_unlock(FILE* const file, const ZixFileLockMode mode)
     355                 :            : {
     356                 :            : #if USE_FLOCK && USE_FILENO
     357         [ -  + ]:          1 :   return zix_posix_status(
     358                 :            :     flock(fileno(file),
     359                 :            :           (mode == ZIX_FILE_LOCK_BLOCK) ? LOCK_UN : (LOCK_UN | LOCK_NB)));
     360                 :            : 
     361                 :            : #else
     362                 :            :   (void)file;
     363                 :            :   (void)mode;
     364                 :            :   return ZIX_STATUS_NOT_SUPPORTED;
     365                 :            : #endif
     366                 :            : }
     367                 :            : 
     368                 :            : ZIX_CONST_FUNC static ZixFileType
     369                 :         27 : stat_file_type(const struct stat* sb)
     370                 :            : {
     371                 :            :   typedef struct {
     372                 :            :     unsigned    mask;
     373                 :            :     ZixFileType type;
     374                 :            :   } Mapping;
     375                 :            : 
     376                 :            :   static ZIX_CONSTEXPR Mapping map[] = {
     377                 :            :     {S_IFREG, ZIX_FILE_TYPE_REGULAR},
     378                 :            :     {S_IFDIR, ZIX_FILE_TYPE_DIRECTORY},
     379                 :            :     {S_IFLNK, ZIX_FILE_TYPE_SYMLINK},
     380                 :            :     {S_IFBLK, ZIX_FILE_TYPE_BLOCK},
     381                 :            :     {S_IFCHR, ZIX_FILE_TYPE_CHARACTER},
     382                 :            :     {S_IFIFO, ZIX_FILE_TYPE_FIFO},
     383                 :            :     {S_IFSOCK, ZIX_FILE_TYPE_SOCKET},
     384                 :            :     {0U, ZIX_FILE_TYPE_UNKNOWN},
     385                 :            :   };
     386                 :            : 
     387                 :         27 :   const unsigned mask = (unsigned)sb->st_mode & (unsigned)S_IFMT;
     388                 :         27 :   unsigned       m    = 0U;
     389   [ +  -  +  + ]:         55 :   while (map[m].mask && map[m].mask != mask) {
     390                 :         28 :     ++m;
     391                 :            :   }
     392                 :            : 
     393                 :         27 :   return map[m].type;
     394                 :            : }
     395                 :            : 
     396                 :            : ZixFileType
     397                 :         28 : zix_file_type(const char* const path)
     398                 :            : {
     399                 :         28 :   struct stat sb = {0};
     400         [ +  + ]:         28 :   return stat(path, &sb) ? ZIX_FILE_TYPE_NONE : stat_file_type(&sb);
     401                 :            : }
     402                 :            : 
     403                 :            : ZixFileType
     404                 :          4 : zix_symlink_type(const char* const path)
     405                 :            : {
     406                 :          4 :   struct stat sb = {0};
     407         [ +  - ]:          4 :   return lstat(path, &sb) ? ZIX_FILE_TYPE_NONE : stat_file_type(&sb);
     408                 :            : }
     409                 :            : 
     410                 :            : char*
     411                 :         15 : zix_temp_directory_path(ZixAllocator* const allocator)
     412                 :            : {
     413                 :         15 :   const char* tmpdir = getenv("TMPDIR"); // NOLINT(concurrency-mt-unsafe)
     414                 :            : 
     415         [ -  + ]:         15 :   tmpdir = tmpdir ? tmpdir : "/tmp";
     416                 :            : 
     417                 :         15 :   return copy_path(allocator, tmpdir, strlen(tmpdir));
     418                 :            : }
     419                 :            : 
     420                 :            : char*
     421                 :          1 : zix_current_path(ZixAllocator* const allocator)
     422                 :            : {
     423                 :            : #ifdef PATH_MAX
     424                 :            :   // Some POSIX systems have a static PATH_MAX so we can store it on the stack
     425                 :          1 :   char        buffer[PATH_MAX] = {0};
     426                 :          1 :   char* const cwd              = getcwd(buffer, PATH_MAX);
     427         [ +  - ]:          1 :   return cwd ? copy_path(allocator, cwd, strlen(cwd)) : NULL;
     428                 :            : 
     429                 :            : #elif USE_PATHCONF
     430                 :            :   // Others don't so we have to query PATH_MAX at runtime to allocate the result
     431                 :            :   const size_t size    = max_path_size(".");
     432                 :            :   char* const  buffer  = (char*)zix_calloc(allocator, size, 1);
     433                 :            :   char* const  current = getcwd(buffer, size);
     434                 :            :   if (!current) {
     435                 :            :     zix_free(allocator, buffer);
     436                 :            :   }
     437                 :            : 
     438                 :            :   return current;
     439                 :            : 
     440                 :            : #else
     441                 :            :   (void)allocator;
     442                 :            :   return NULL;
     443                 :            : 
     444                 :            : #endif
     445                 :            : }

Generated by: LCOV version 1.16