Use single API to create dir tree

This commit is contained in:
Arun Prakash Jana 2019-11-20 23:23:44 +05:30
parent f0ca47afae
commit 0c0f12b528
No known key found for this signature in database
GPG Key ID: A75979F35C080412
1 changed files with 52 additions and 74 deletions

126
src/nnn.c
View File

@ -1317,60 +1317,6 @@ static void xrm(char *path)
} }
} }
/* Create non-existent parents and a file or dir */
static bool xmkentryp(char* path, bool dir)
{
char* p = path;
char *slash = path;
if (!p || !*p)
return FALSE;
/* Skip the first '/' */
++p;
while (*p != '\0') {
if (*p == '/') {
slash = p;
*p = '\0';
} else {
++p;
continue;
}
/* Create folder from path to '\0' inserted at p */
if (mkdir(path, 0777) == -1 && errno != EEXIST) {
DPRINTF_S("mkdir1 fail");
DPRINTF_S(strerror(errno));
*slash = '/';
return FALSE;
}
/* Restore path */
*slash = '/';
++p;
}
if (dir) {
if(mkdir(path, 0777) == -1 && errno != EEXIST) {
DPRINTF_S("mkdir2 fail");
DPRINTF_S(strerror(errno));
return FALSE;
}
} else {
int fd = open(path, O_CREAT, 0666);
if (fd == -1 && errno != EEXIST) {
DPRINTF_S("open fail");
DPRINTF_S(strerror(errno));
return FALSE;
}
close(fd);
}
return TRUE;
}
static uint lines_in_file(int fd, char *buf, size_t buflen) static uint lines_in_file(int fd, char *buf, size_t buflen)
{ {
ssize_t len; ssize_t len;
@ -3172,14 +3118,55 @@ static bool execute_file(int cur, char *path, char *newpath, int *presel)
return TRUE; return TRUE;
} }
static bool create_dir(const char *path) /* Create non-existent parents and a file or dir */
static bool xmktree(char* path, bool dir)
{ {
if (!xdiraccess(path)) { char* p = path;
if (errno != ENOENT) char *slash = path;
return FALSE;
if (mkdir(path, 0755) == -1) if (!p || !*p)
return FALSE;
/* Skip the first '/' */
++p;
while (*p != '\0') {
if (*p == '/') {
slash = p;
*p = '\0';
} else {
++p;
continue;
}
/* Create folder from path to '\0' inserted at p */
if (mkdir(path, 0777) == -1 && errno != EEXIST) {
DPRINTF_S("mkdir1!");
DPRINTF_S(strerror(errno));
*slash = '/';
return FALSE; return FALSE;
}
/* Restore path */
*slash = '/';
++p;
}
if (dir) {
if(mkdir(path, 0777) == -1 && errno != EEXIST) {
DPRINTF_S("mkdir2!");
DPRINTF_S(strerror(errno));
return FALSE;
}
} else {
int fd = open(path, O_CREAT, 0666);
if (fd == -1 && errno != EEXIST) {
DPRINTF_S("open!");
DPRINTF_S(strerror(errno));
return FALSE;
}
close(fd);
} }
return TRUE; return TRUE;
@ -3213,7 +3200,7 @@ static bool archive_mount(char *name, char *path, char *newpath, int *presel)
mkpath(cfgdir, dir, newpath); mkpath(cfgdir, dir, newpath);
free(dir); free(dir);
if (!create_dir(newpath)) { if (!xmktree(newpath, TRUE)) {
printwait(strerror(errno), presel); printwait(strerror(errno), presel);
return FALSE; return FALSE;
} }
@ -3246,7 +3233,7 @@ static bool sshfs_mount(char *newpath, int *presel)
/* Create the mount point */ /* Create the mount point */
mkpath(cfgdir, tmp, newpath); mkpath(cfgdir, tmp, newpath);
if (!create_dir(newpath)) { if (!xmktree(newpath, TRUE)) {
printwait(strerror(errno), presel); printwait(strerror(errno), presel);
return FALSE; return FALSE;
} }
@ -4985,10 +4972,10 @@ nochange:
/* Check if it's a dir or file */ /* Check if it's a dir or file */
if (r == 'f') { if (r == 'f') {
mkpath(path, tmp, newpath); mkpath(path, tmp, newpath);
r = xmkentryp(newpath, FALSE); r = xmktree(newpath, FALSE);
} else if (r == 'd') { } else if (r == 'd') {
mkpath(path, tmp, newpath); mkpath(path, tmp, newpath);
r = xmkentryp(newpath, TRUE); r = xmktree(newpath, TRUE);
} else if (r == 's' || r == 'h') { } else if (r == 's' || r == 'h') {
if (tmp[0] == '@' && tmp[1] == '\0') if (tmp[0] == '@' && tmp[1] == '\0')
tmp[0] = '\0'; tmp[0] = '\0';
@ -5340,21 +5327,12 @@ static bool setup_config(void)
/* Create ~/.config */ /* Create ~/.config */
xstrlcpy(cfgdir + r - 1, "/.config", len - r); xstrlcpy(cfgdir + r - 1, "/.config", len - r);
DPRINTF_S(cfgdir); DPRINTF_S(cfgdir);
if (!create_dir(cfgdir)) {
xerror();
return FALSE;
}
r += 8; /* length of "/.config" */ r += 8; /* length of "/.config" */
} }
/* Create ~/.config/nnn */ /* Create ~/.config/nnn */
xstrlcpy(cfgdir + r - 1, "/nnn", len - r); xstrlcpy(cfgdir + r - 1, "/nnn", len - r);
DPRINTF_S(cfgdir); DPRINTF_S(cfgdir);
if (!create_dir(cfgdir)) {
xerror();
return FALSE;
}
/* Create ~/.config/nnn/plugins */ /* Create ~/.config/nnn/plugins */
xstrlcpy(cfgdir + r + 4 - 1, "/plugins", 9); /* subtract length of "/nnn" (4) */ xstrlcpy(cfgdir + r + 4 - 1, "/plugins", 9); /* subtract length of "/nnn" (4) */
@ -5363,7 +5341,7 @@ static bool setup_config(void)
xstrlcpy(plugindir, cfgdir, len); xstrlcpy(plugindir, cfgdir, len);
DPRINTF_S(plugindir); DPRINTF_S(plugindir);
if (!create_dir(cfgdir)) { if (!xmktree(cfgdir, TRUE)) {
xerror(); xerror();
return FALSE; return FALSE;
} }
@ -5375,7 +5353,7 @@ static bool setup_config(void)
xstrlcpy(sessiondir, cfgdir, len); xstrlcpy(sessiondir, cfgdir, len);
DPRINTF_S(sessiondir); DPRINTF_S(sessiondir);
if (!create_dir(cfgdir)) { if (!xmktree(cfgdir, TRUE)) {
xerror(); xerror();
return FALSE; return FALSE;
} }