LCOV - code coverage report
Current view: top level - libemail/src/core - fs_util.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 95.8 % 24 23
Test Date: 2026-04-15 21:12:52 Functions: 100.0 % 3 3

            Line data    Source code
       1              : #include "fs_util.h"
       2              : #include "platform/path.h"
       3              : #include <stdio.h>
       4              : #include <stdlib.h>
       5              : #include <string.h>
       6              : #include <sys/stat.h>
       7              : #include <unistd.h>
       8              : #include <errno.h>
       9              : 
      10              : /**
      11              :  * @brief Creates a directory and all missing parent directories.
      12              :  * @param path  Directory path to create.
      13              :  * @param mode  Permission bits applied to each created directory.
      14              :  * @return 0 on success, -1 on error.
      15              :  */
      16          164 : int fs_mkdir_p(const char *path, mode_t mode) {
      17          164 :     char tmp[1024];
      18          164 :     char *p = NULL;
      19              :     size_t len;
      20              : 
      21          164 :     if (!path || !*path) return -1;
      22              : 
      23          164 :     snprintf(tmp, sizeof(tmp), "%s", path);
      24          164 :     len = strlen(tmp);
      25          164 :     if (len > 0 && tmp[len - 1] == '/')
      26            2 :         tmp[len - 1] = 0;
      27              :     
      28        10414 :     for (p = tmp + 1; *p; p++) {
      29        10251 :         if (*p == '/') {
      30          928 :             *p = 0;
      31              :             /* Skip empty components (multiple slashes) */
      32          928 :             if (strlen(tmp) > 0) {
      33          928 :                 if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
      34            1 :                     return -1;
      35              :                 }
      36              :             }
      37          927 :             *p = '/';
      38              :         }
      39              :     }
      40              :     
      41          163 :     if (strlen(tmp) > 0) {
      42          162 :         if (mkdir(tmp, mode) != 0 && errno != EEXIST) {
      43            0 :             return -1;
      44              :         }
      45              :         // Explicitly set mode in case of umask
      46          162 :         return chmod(tmp, mode);
      47              :     }
      48              :     
      49            1 :     return 0;
      50              : }
      51              : 
      52              : /**
      53              :  * @brief Sets the permission bits on an existing file or directory.
      54              :  * @param path  Path to the file.
      55              :  * @param mode  Desired permission bits.
      56              :  * @return 0 on success, -1 on error.
      57              :  */
      58            3 : int fs_ensure_permissions(const char *path, mode_t mode) {
      59            3 :     return chmod(path, mode);
      60              : }
      61              : 
      62              : /**
      63              :  * @brief Returns the user's home directory path.
      64              :  *
      65              :  * Delegates to the platform layer (platform_home_dir()).
      66              :  * @return Pointer to the home path string (do not free), or NULL if unavailable.
      67              :  */
      68            2 : const char* fs_get_home_dir(void) {
      69            2 :     return platform_home_dir();
      70              : }
        

Generated by: LCOV version 2.0-1