LCOV - code coverage report
Current view: top level - core - helper.c (source / functions) Coverage Total Hit
Test: lcov.out Lines: 97.4 % 39 38
Test Date: 2025-03-25 15:17:39 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /* SPDX-License-Identifier: GPL-2.0-only */
       2              : /*
       3              :  * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
       4              :  */
       5              : 
       6              : #include "core/helper.h"
       7              : 
       8              : #include <errno.h>
       9              : #include <fcntl.h>
      10              : #include <stdio.h>
      11              : #include <stdlib.h>
      12              : #include <string.h>
      13              : #include <sys/types.h>
      14              : #include <unistd.h>
      15              : 
      16              : #include "core/logger.h"
      17              : 
      18              : #define OPEN_MODE_644 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
      19              : 
      20           15 : int bf_strncpy(char *dst, size_t len, const char *src)
      21              : {
      22              :     size_t src_len;
      23              :     size_t copy_len;
      24              : 
      25           15 :     bf_assert(dst && src);
      26           15 :     bf_assert(len);
      27              : 
      28           15 :     src_len = strlen(src);
      29           15 :     copy_len = bf_min(src_len, len - 1);
      30              : 
      31           15 :     memcpy(dst, src, copy_len);
      32           15 :     dst[copy_len] = '\0';
      33              : 
      34           15 :     return copy_len != src_len ? -E2BIG : 0;
      35              : }
      36              : 
      37            7 : int bf_read_file(const char *path, void **buf, size_t *len)
      38              : {
      39              :     _cleanup_close_ int fd = -1;
      40              :     _cleanup_free_ void *_buf = NULL;
      41              :     size_t _len;
      42              :     ssize_t r;
      43              : 
      44            7 :     bf_assert(path);
      45            6 :     bf_assert(buf);
      46            5 :     bf_assert(len);
      47              : 
      48            4 :     fd = open(path, O_RDONLY);
      49            4 :     if (fd < 0)
      50            1 :         return bf_err_r(errno, "failed to open %s", path);
      51              : 
      52            3 :     _len = lseek(fd, 0, SEEK_END);
      53            3 :     lseek(fd, 0, SEEK_SET);
      54              : 
      55            3 :     _buf = malloc(_len);
      56            3 :     if (!_buf)
      57            1 :         return bf_err_r(errno, "failed to allocate memory");
      58              : 
      59            2 :     r = read(fd, _buf, _len);
      60            2 :     if (r < 0)
      61            1 :         return bf_err_r(errno, "failed to read serialized data");
      62            1 :     if ((size_t)r != _len)
      63            0 :         return bf_err_r(EIO, "can't read full serialized data");
      64              : 
      65              :     closep(&fd);
      66              : 
      67            1 :     *buf = TAKE_PTR(_buf);
      68            1 :     *len = _len;
      69              : 
      70            1 :     return 0;
      71              : }
      72              : 
      73            6 : int bf_write_file(const char *path, const void *buf, size_t len)
      74              : {
      75              :     _cleanup_close_ int fd = -1;
      76              :     ssize_t r;
      77              : 
      78            6 :     bf_assert(path);
      79            5 :     bf_assert(buf);
      80              : 
      81            4 :     fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, OPEN_MODE_644);
      82            4 :     if (fd < 0)
      83            1 :         return bf_err_r(errno, "failed to open %s", path);
      84              : 
      85            3 :     r = write(fd, buf, len);
      86            3 :     if (r < 0)
      87            1 :         return bf_err_r(errno, "failed to write to %s", path);
      88            2 :     if ((size_t)r != len)
      89            1 :         return bf_err_r(EIO, "can't write full data to %s", path);
      90              : 
      91              :     closep(&fd);
      92              : 
      93              :     return 0;
      94              : }
        

Generated by: LCOV version 2.0-1