Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 9572c94

Browse files
authored
Merge pull request #243 from laijs/hyper_mkdir
re-implement hyper_mkdir()
2 parents f71d02c + 285f5d5 commit 9572c94

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

src/util.c

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/reboot.h>
1919
#include <grp.h>
2020
#include <pwd.h>
21+
#include <libgen.h>
2122

2223
#include "util.h"
2324
#include "hyper.h"
@@ -287,53 +288,37 @@ int hyper_create_file(const char *hyper_path)
287288
return 0;
288289
}
289290

290-
int hyper_mkdir(char *hyper_path, mode_t mode)
291+
int hyper_mkdir(char *path, mode_t mode)
291292
{
292293
struct stat st;
293-
char *p, *path = strdup(hyper_path);
294-
295-
if (path == NULL) {
296-
errno = ENOMEM;
297-
goto fail;
298-
}
299294

300295
if (stat(path, &st) >= 0) {
301296
if (S_ISDIR(st.st_mode))
302-
goto out;
297+
return 0;
303298
errno = ENOTDIR;
304-
goto fail;
299+
return -1;
305300
}
306301

307302
if (errno != ENOENT)
308-
goto fail;
303+
return -1;
309304

310-
p = strrchr(path, '/');
311-
if (p == NULL) {
312-
errno = EINVAL;
313-
goto fail;
305+
char *parent_path = strdup(path);
306+
if (parent_path == NULL) {
307+
errno = ENOMEM;
308+
return -1;
314309
}
315-
316-
if (p != path) {
317-
*p = '\0';
318-
319-
if (hyper_mkdir(path, mode) < 0)
320-
goto fail;
321-
322-
*p = '/';
310+
if (hyper_mkdir(dirname(parent_path), mode) < 0) {
311+
free(parent_path);
312+
return -1;
323313
}
314+
free(parent_path);
324315

325316
fprintf(stdout, "create directory %s\n", path);
326317
if (mkdir(path, mode) < 0 && errno != EEXIST) {
327318
perror("failed to create directory");
328-
goto fail;
319+
return -1;
329320
}
330-
out:
331-
free(path);
332321
return 0;
333-
334-
fail:
335-
free(path);
336-
return -1;
337322
}
338323

339324
void online_cpu(void)

0 commit comments

Comments
 (0)