LCOV - code coverage report
Current view: top level - src/core - fs_util.c (source / functions) Coverage Total Hit
Test: coverage-functional.info Lines: 78.3 % 23 18
Test Date: 2026-04-20 19:54:24 Functions: 66.7 % 3 2

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

Generated by: LCOV version 2.0-1