Debian Codegraph @

src/c-blosc2/blosc/directories.c

raw
1 /*********************************************************************
2 Blosc - Blocked Shuffling and Compression Library
3
4 Copyright (c) 2021 Blosc Development Team <[email protected]>
5 https://blosc.org
6 License: BSD 3-Clause (see LICENSE.txt)
7
8 See LICENSE.txt for details about copyright and rights to use.
9 **********************************************************************/
10
11 #include "blosc2.h"
12
13 #include <sys/stat.h>
14 #include <errno.h>
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19
20 #if defined(_WIN32) || defined(__MINGW32__)
21 #include <windows.h>
22 #include <malloc.h>
23 #include <io.h>
24
25 int blosc2_remove_dir(const char* dir_path) {
26 char* path;
27 char last_char = dir_path[strlen(dir_path) - 1];
28 if (last_char != '\\' || last_char != '/') {
29 path = malloc(strlen(dir_path) + 2 + 1);
30 sprintf(path, "%s\\*", dir_path);
31 }
32 else {
33 path = malloc(strlen(dir_path) + 1 + 1);
34 strcpy(path, dir_path);
35 strcat(path, "*");
36 }
37 char* fname;
38 struct _finddata_t cfile;
39
40 intptr_t file = _findfirst(path, &cfile);
41 free(path);
42
43 if (file == -1) {
44 BLOSC_TRACE_ERROR("Could not open the file.");
45 return BLOSC2_ERROR_FILE_OPEN;
46 }
47 int ret;
48
49 while ( _findnext(file, &cfile) == 0) {
50 if (strcmp(".", cfile.name) == 0 || strcmp("..", cfile.name) == 0) {
51 continue;
52 }
53 fname = malloc(strlen(dir_path) + 1 + strlen(cfile.name) + 1);
54 sprintf(fname, "%s\\%s", dir_path, cfile.name);
55
56 ret = remove(fname);
57 free(fname);
58 if (ret < 0) {
59 BLOSC_TRACE_ERROR("Could not remove file %s", fname);
60 _findclose(file);
61 return BLOSC2_ERROR_FAILURE;
62 }
63 }
64
65 rmdir(dir_path);
66 _findclose(file);
67 return BLOSC2_ERROR_SUCCESS;
68 }
69
70 #else
71 #include <dirent.h>
72 #include <unistd.h>
73
74 /* Return the directory path with the '/' at the end */
75 char* blosc2_normalize_dirpath(const char* dir_path) {
76 char last_char = dir_path[strlen(dir_path) - 1];
77 char* path;
78 if (last_char != '\\' || last_char != '/'){
79 path = malloc(strlen(dir_path) + 1 + 1);
80 sprintf(path, "%s/", dir_path);
81 }
82 else {
83 path = malloc(strlen(dir_path) + 1);
84 strcpy(path, dir_path);
85 }
86 return path;
87 }
88
89 /* Function needed for removing each time the directory */
90 int blosc2_remove_dir(const char* dir_path) {
91 char* path = blosc2_normalize_dirpath(dir_path);
92
93 DIR* dr = opendir(path);
94 struct stat statbuf;
95 if (dr == NULL) {
96 BLOSC_TRACE_ERROR("No file or directory found.");
97 free(path);
98 return BLOSC2_ERROR_NOT_FOUND;
99 }
100 struct dirent *de;
101 int ret;
102 char* fname;
103 while ((de = readdir(dr)) != NULL) {
104 fname = malloc(strlen(path) + strlen(de->d_name) + 1);
105 if (path != NULL) {
106 sprintf(fname, "%s%s", path, de->d_name);
107 }
108 else {
109 sprintf(fname, "%s", de->d_name);
110 }
111 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
112 free(fname);
113 continue;
114 }
115 if (!stat(fname, &statbuf)) {
116 ret = unlink(fname);
117 if (ret < 0) {
118 BLOSC_TRACE_ERROR("Could not remove file %s", fname);
119 free(fname);
120 closedir(dr);
121 free(path);
122 return BLOSC2_ERROR_FAILURE;
123 }
124 }
125 free(fname);
126 }
127 closedir(dr);
128 rmdir(path);
129 free(path);
130 return BLOSC2_ERROR_SUCCESS;
131 }
132
133 #endif /* _WIN32 */
134
135 int blosc2_remove_urlpath(const char* urlpath){
136 if (urlpath != NULL) {
137 struct stat statbuf;
138 if (stat(urlpath, &statbuf) != 0){
139 if (errno == ENOENT) {
140 // Path does not exist
141 return BLOSC2_ERROR_SUCCESS;
142 }
143 BLOSC_TRACE_ERROR("Could not access %s", urlpath);
144 return BLOSC2_ERROR_FAILURE;
145 }
146 if ((statbuf.st_mode & S_IFDIR) != 0) {
147 return blosc2_remove_dir(urlpath);
148 }
149 if (remove(urlpath) < 0) {
150 BLOSC_TRACE_ERROR("Could not remove %s", urlpath);
151 return BLOSC2_ERROR_FILE_REMOVE;
152 }
153 }
154 return BLOSC2_ERROR_SUCCESS;
155 }
156
157 int blosc2_rename_urlpath(char* old_urlpath, char* new_urlpath){
158 if (old_urlpath != NULL && new_urlpath != NULL) {
159 struct stat statbuf;
160 if (stat(old_urlpath, &statbuf) != 0) {
161 BLOSC_TRACE_ERROR("Could not access %s", old_urlpath);
162 return BLOSC2_ERROR_FAILURE;
163 }
164 int ret = rename(old_urlpath, new_urlpath);
165 if (ret < 0) {
166 BLOSC_TRACE_ERROR("Could not rename %s to %s", old_urlpath, new_urlpath);
167 return BLOSC2_ERROR_FAILURE;
168 }
169 }
170 return BLOSC2_ERROR_SUCCESS;
171 }