SEL_MTIME,
SEL_REDRAW,
SEL_COPY,
+ SEL_RENAME,
SEL_HELP,
SEL_RUN,
SEL_RUNARG,
{ KEY_F(2), SEL_REDRAW, "", "" },
/* Copy currently selected file path */
{ CONTROL('K'), SEL_COPY, "", "" },
+ /* Show rename prompt */
+ { CONTROL('R'), SEL_RENAME, "", "" },
/* Show help */
{ '?', SEL_HELP, "", "" },
/* Run command */
"eD | Show current file details\n"
"em | Show concise media info\n"
"eM | Show full media info\n"
+ "d^R | Rename selected entry\n"
"es | Toggle sort by file size\n"
"eS | Toggle disk usage mode\n"
"et | Toggle sort by mtime\n"
tmp = readinput();
clearprompt();
if (tmp == NULL)
- goto nochange;
+ break;
for (r = 0; bookmark[r].key && r < MAX_BM; ++r) {
if (xstrcmp(bookmark[r].key, tmp) == -1)
mkpath(path, dents[cur].name, oldpath, PATH_MAX);
goto begin;
case SEL_STATS:
- {
- struct stat sb;
-
if (ndents > 0) {
mkpath(path, dents[cur].name, oldpath, PATH_MAX);
} else {
r = show_stats(oldpath, dents[cur].name, &sb);
if (r < 0) {
- printmsg(strerror(errno));
+ printwarn();
goto nochange;
}
}
}
-
break;
- }
case SEL_MEDIA: // fallthrough
case SEL_FMEDIA:
if (ndents > 0) {
} else if (!copier)
printmsg("NNN_COPIER is not set");
goto nochange;
+ case SEL_RENAME:
+ if (ndents <= 0)
+ break;
+
+ printprompt("rename to: ");
+ tmp = readinput();
+ clearprompt();
+ if (tmp == NULL)
+ break;
+
+ /* Allow only relative paths */
+ if (tmp[0] == '/' || basename(tmp) != tmp) {
+ printmsg("relative paths only");
+ goto nochange;
+ }
+
+ /* Skip renaming to same name */
+ if (xstrcmp(tmp, dents[cur].name) == 0)
+ break;
+
+ /* Open the descriptor to currently open directory */
+ fd = open(path, O_RDONLY | O_DIRECTORY | O_NOATIME);
+ if (fd == -1) {
+ printwarn();
+ goto nochange;
+ }
+
+ /* Check if another file with same name exists */
+ if (faccessat(fd, tmp, F_OK, AT_SYMLINK_NOFOLLOW) != -1) {
+ /* File with the same name exists */
+ xstrlcpy(g_buf, tmp, NAME_MAX);
+
+ printprompt("overwrite? (y): ");
+ tmp = readinput();
+ if (tmp == NULL || tmp[0] != 'y' || tmp[1] != '\0') {
+ close(fd);
+ break;
+ }
+
+ tmp = g_buf;
+ }
+
+ /* Rename the file */
+ r = renameat(fd, dents[cur].name, fd, tmp);
+ if (r != 0) {
+ printwarn();
+ close(fd);
+ goto nochange;
+ }
+
+ close(fd);
+ mkpath(path, tmp, oldpath, PATH_MAX);
+ goto begin;
case SEL_HELP:
show_help(path);
break;