aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Arun Prakash Jana <engineerarun@gmail.com>2019-03-02 14:37:57 +0530
committerGravatar Arun Prakash Jana <engineerarun@gmail.com>2019-03-02 14:37:57 +0530
commitfa46963dd79191036e86fc1be0ff95fbe9e0fbb0 (patch)
treeef1d5a9d26ff8dfa862923bc9b3fbfd767a1f27a
parent3f262910d3ecb98050817708ea8381664605bb10 (diff)
downloadnnn-fa46963dd79191036e86fc1be0ff95fbe9e0fbb0.tar.gz
Create trash dir
-rw-r--r--src/nnn.c52
1 files changed, 48 insertions, 4 deletions
diff --git a/src/nnn.c b/src/nnn.c
index 7f33c58..fdf3d67 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -341,6 +341,9 @@ static char g_buf[CMD_LEN_MAX] __attribute__ ((aligned));
/* Buffer for file path copy file */
static char g_cppath[PATH_MAX] __attribute__ ((aligned));
+/* Buffer for trash dir */
+static char g_trash[PATH_MAX] __attribute__ ((aligned));
+
/* Buffer to store tmp file path */
static char g_tmpfpath[HOME_LEN_MAX] __attribute__ ((aligned));
@@ -1120,6 +1123,25 @@ static bool xdiraccess(const char *path)
return TRUE;
}
+/*
+ * Creates a dir if not present
+ */
+static bool createdir(const char *path, mode_t mode)
+{
+ DIR *dirp = opendir(path);
+
+ if (dirp) {
+ closedir(dirp);
+ return TRUE;
+ }
+
+ if (errno == ENOENT && !mkdir(path, mode))
+ return TRUE;
+
+ fprintf(stderr, "create %s %s\n", path, strerror(errno));
+ return FALSE;
+}
+
static int digit_compare(const char *a, const char *b)
{
while (*a && *b && *a == *b)
@@ -4053,16 +4075,38 @@ int main(int argc, char *argv[])
home = getenv("HOME");
DPRINTF_S(home);
- if (home)
+
+ if (getenv("NNN_TRASH")) {
+ if (!home) {
+ fprintf(stderr, "trash! no HOME!\n");
+ return 1;
+ }
+
+ /* Create trash dir if missing */
+ g_tmpfplen = xstrlcpy(g_trash, home, PATH_MAX);
+ g_tmpfplen += xstrlcpy(g_trash + g_tmpfplen - 1,
+ "/.local/share/nnn", PATH_MAX - g_tmpfplen);
+ DPRINTF_S(g_trash);
+ if (!createdir(g_trash, 0777))
+ return 1;
+
+ xstrlcpy(g_trash + g_tmpfplen - 2, "/trash", PATH_MAX - g_tmpfplen);
+ DPRINTF_S(g_trash);
+ if (!createdir(g_trash, 0777))
+ return 1;
+ }
+
+ if (home) {
+ /* Prefix for other temporary ops */
g_tmpfplen = xstrlcpy(g_tmpfpath, home, HOME_LEN_MAX);
- else if (getenv("TMPDIR"))
+ } else if (getenv("TMPDIR"))
g_tmpfplen = xstrlcpy(g_tmpfpath, getenv("TMPDIR"), HOME_LEN_MAX);
else if (xdiraccess("/tmp"))
g_tmpfplen = xstrlcpy(g_tmpfpath, "/tmp", HOME_LEN_MAX);
if (!cfg.picker && g_tmpfplen) {
- xstrlcpy(g_cppath, g_tmpfpath, HOME_LEN_MAX);
- xstrlcpy(g_cppath + g_tmpfplen - 1, "/.nnncp", HOME_LEN_MAX - g_tmpfplen);
+ xstrlcpy(g_cppath, g_tmpfpath, PATH_MAX);
+ xstrlcpy(g_cppath + g_tmpfplen - 1, "/.nnncp", PATH_MAX - g_tmpfplen);
}
/* Disable auto-select if opted */