| File: | .build-ci/../shared/fs-util.c |
| Warning: | line 104, column 7 Potential leak of memory pointed to by 'file_path' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later | |||
| 2 | /* | |||
| 3 | * This file is part of nvme-cli. | |||
| 4 | * Copyright (c) 2026 Dell Technologies Inc. or its subsidiaries. | |||
| 5 | * | |||
| 6 | * Authors: Martin Belanger <martin.belanger@dell.com> | |||
| 7 | */ | |||
| 8 | ||||
| 9 | #include <errno(*__errno_location ()).h> | |||
| 10 | #include <limits.h> | |||
| 11 | #include <stdio.h> | |||
| 12 | #include <stdlib.h> | |||
| 13 | #include <string.h> | |||
| 14 | #include <sys/stat.h> | |||
| 15 | #include <unistd.h> | |||
| 16 | ||||
| 17 | #include "cleanup.h" | |||
| 18 | #include "fs-util.h" | |||
| 19 | ||||
| 20 | int shr_mkdir_p(const char *path, mode_t mode) | |||
| 21 | { | |||
| 22 | char buf[PATH_MAX4096]; | |||
| 23 | char *p; | |||
| 24 | size_t len; | |||
| 25 | int ret; | |||
| 26 | ||||
| 27 | len = strlen(path); | |||
| 28 | if (len >= sizeof(buf)) | |||
| 29 | return -ENAMETOOLONG36; | |||
| 30 | memcpy(buf, path, len + 1); | |||
| 31 | if (len && buf[len - 1] == '/') | |||
| 32 | buf[len - 1] = '\0'; | |||
| 33 | ||||
| 34 | for (p = buf + 1; *p; p++) { | |||
| 35 | if (*p != '/') | |||
| 36 | continue; | |||
| 37 | *p = '\0'; | |||
| 38 | ret = shr_mkdir(buf, mode); | |||
| 39 | if (ret < 0 && ret != -EEXIST17) | |||
| 40 | return ret; | |||
| 41 | *p = '/'; | |||
| 42 | } | |||
| 43 | ret = shr_mkdir(buf, mode); | |||
| 44 | return (ret == 0 || ret == -EEXIST17) ? 0 : ret; | |||
| 45 | } | |||
| 46 | ||||
| 47 | int shr_mkdir_from_fname(const char *file, mode_t mode) | |||
| 48 | { | |||
| 49 | __cleanup_free__attribute__((cleanup(shr_freep))) char *file_copy = NULL((void*)0); | |||
| 50 | char *parent; | |||
| 51 | ||||
| 52 | file_copy = strdup(file); | |||
| 53 | if (!file_copy) | |||
| 54 | return -ENOMEM12; | |||
| 55 | ||||
| 56 | parent = shr_dirname(file_copy); | |||
| 57 | return shr_mkdir_p(parent, mode); | |||
| 58 | } | |||
| 59 | ||||
| 60 | char *shr_basename(const char *path) | |||
| 61 | { | |||
| 62 | char *p = (char *)strrchr(path, '/')_Generic (0 ? (path) : (void *) 1, const void *: (const char * ) (strrchr (path, '/')), default: strrchr (path, '/')); | |||
| 63 | ||||
| 64 | return p ? p + 1 : (char *)path; | |||
| 65 | } | |||
| 66 | ||||
| 67 | static char *join_path(const char *dir, const char *path) | |||
| 68 | { | |||
| 69 | char *out; | |||
| 70 | size_t len; | |||
| 71 | ||||
| 72 | if (!dir) | |||
| 73 | return strdup(path); | |||
| 74 | if (!path || !*path) | |||
| 75 | return strdup(dir); | |||
| 76 | ||||
| 77 | len = strlen(dir) + 1 + strlen(path) + 1; | |||
| 78 | out = malloc(len); | |||
| 79 | if (!out) | |||
| 80 | return NULL((void*)0); | |||
| 81 | snprintf(out, len, "%s/%s", dir, path); | |||
| 82 | ||||
| 83 | return out; | |||
| 84 | } | |||
| 85 | ||||
| 86 | unsigned char *shr_read_file(const char *dir, const char *path, long *size, int retries) | |||
| 87 | { | |||
| 88 | __cleanup_free__attribute__((cleanup(shr_freep))) char *file_path = NULL((void*)0); | |||
| 89 | unsigned char *buf = NULL((void*)0); | |||
| 90 | FILE *file = NULL((void*)0); | |||
| 91 | size_t n; | |||
| 92 | int i; | |||
| 93 | ||||
| 94 | file_path = join_path(dir, path); | |||
| ||||
| 95 | if (!file_path) | |||
| 96 | return NULL((void*)0); | |||
| 97 | ||||
| 98 | for (i = 0; i < retries; i++) { | |||
| 99 | file = fopen(file_path, "rb"); | |||
| 100 | if (file) | |||
| 101 | break; | |||
| 102 | sleep((unsigned int)(retries > 1)); | |||
| 103 | } | |||
| 104 | if (!file) | |||
| ||||
| 105 | return NULL((void*)0); | |||
| 106 | ||||
| 107 | fseek(file, 0, SEEK_END2); | |||
| 108 | *size = ftell(file); | |||
| 109 | if (*size <= 0) { | |||
| 110 | fclose(file); | |||
| 111 | return NULL((void*)0); | |||
| 112 | } | |||
| 113 | fseek(file, 0, SEEK_SET0); | |||
| 114 | ||||
| 115 | buf = malloc(*size); | |||
| 116 | if (!buf) { | |||
| 117 | fclose(file); | |||
| 118 | return NULL((void*)0); | |||
| 119 | } | |||
| 120 | ||||
| 121 | n = fread(buf, 1, *size, file); | |||
| 122 | fclose(file); | |||
| 123 | ||||
| 124 | if (n != (size_t)*size) { | |||
| 125 | free(buf); | |||
| 126 | return NULL((void*)0); | |||
| 127 | } | |||
| 128 | ||||
| 129 | return buf; | |||
| 130 | } |