Skip to content

Commit

Permalink
feat: ":write ++p" creates parent dirs #20835
Browse files Browse the repository at this point in the history
- `:write ++p foo/bar/baz.txt` should create parent directories `foo/bar/` if
   they do not exist
    - Note: `:foo ++…` is usually for options. No existing options have
      a single-char abbreviation (presumably by design), so it's safe to
      special-case `++p` here.
- Same for `writefile(…, 'foo/bar/baz.txt', 'p')`
- `BufWriteCmd` can see the ++p flag via `v:cmdarg`.

closes #19884
  • Loading branch information
Viblanc committed Nov 7, 2022
1 parent 10fbda5 commit d337814
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 0 deletions.
2 changes: 2 additions & 0 deletions runtime/doc/editing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ Where {optname} is one of: *++ff* *++enc* *++bin* *++nobin* *++edit*
bad specifies behavior for bad characters
edit for |:read| only: keep option values as if editing
a file
p creates the parent directory (or directories) of
a filename if they do not exist

{value} cannot contain white space. It can be any valid value for these
options. Examples: >
Expand Down
8 changes: 8 additions & 0 deletions src/nvim/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -7067,6 +7067,9 @@ char *set_cmdarg(exarg_T *eap, char *oldarg)
if (eap->bad_char != 0) {
len += 7 + 4; // " ++bad=" + "keep" or "drop"
}
if (eap->mkdir_p != 0) {
len += 4;
}

const size_t newval_len = len + 1;
char *newval = xmalloc(newval_len);
Expand Down Expand Up @@ -7100,6 +7103,11 @@ char *set_cmdarg(exarg_T *eap, char *oldarg)
snprintf(newval + strlen(newval), newval_len, " ++bad=%c",
eap->bad_char);
}

if (eap->mkdir_p) {
snprintf(newval, newval_len, " ++p");
}

vimvars[VV_CMDARG].vv_str = newval;
return oldval;
}
Expand Down
4 changes: 4 additions & 0 deletions src/nvim/eval/funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -9970,6 +9970,7 @@ static void f_writefile(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
bool binary = false;
bool append = false;
bool do_fsync = !!p_fs;
bool mkdir_p = false;
if (argvars[2].v_type != VAR_UNKNOWN) {
const char *const flags = tv_get_string_chk(&argvars[2]);
if (flags == NULL) {
Expand All @@ -9985,6 +9986,8 @@ static void f_writefile(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
do_fsync = true; break;
case 'S':
do_fsync = false; break;
case 'p':
mkdir_p = true; break;
default:
// Using %s, p and not %c, *p to preserve multibyte characters
semsg(_("E5060: Unknown flag: %s"), p);
Expand All @@ -10004,6 +10007,7 @@ static void f_writefile(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
emsg(_("E482: Can't open file with an empty name"));
} else if ((error = file_open(&fp, fname,
((append ? kFileAppend : kFileTruncate)
| (mkdir_p ? kFileMkDir : kFileCreate)
| kFileCreate), 0666)) != 0) {
semsg(_("E482: Can't open file %s for writing: %s"),
fname, os_strerror(error));
Expand Down
7 changes: 7 additions & 0 deletions src/nvim/ex_cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,13 @@ int do_write(exarg_T *eap)
fname = curbuf->b_sfname;
}

if (eap->mkdir_p) {
if (os_file_mkdir(fname, 0755) < 0) {
retval = FAIL;
goto theend;
}
}

name_was_missing = curbuf->b_ffname == NULL;
retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
eap, eap->append, eap->forceit, true, false);
Expand Down
1 change: 1 addition & 0 deletions src/nvim/ex_cmds_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ struct exarg {
int regname; ///< register name (NUL if none)
int force_bin; ///< 0, FORCE_BIN or FORCE_NOBIN
int read_edit; ///< ++edit argument
int mkdir_p; ///< ++p argument
int force_ff; ///< ++ff= argument (first char of argument)
int force_enc; ///< ++enc= argument (index in cmd[])
int bad_char; ///< BAD_KEEP, BAD_DROP or replacement byte
Expand Down
7 changes: 7 additions & 0 deletions src/nvim/ex_docmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -4074,6 +4074,13 @@ static int getargopt(exarg_T *eap)
return OK;
}

// ":write ++p foo/bar/file
if (strncmp(arg, "p", 1) == 0) {
eap->mkdir_p = true;
eap->arg = skipwhite(arg + 1);
return OK;
}

if (STRNCMP(arg, "ff", 2) == 0) {
arg += 2;
pp = &eap->force_ff;
Expand Down
8 changes: 8 additions & 0 deletions src/nvim/os/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,21 @@ int file_open(FileDescriptor *const ret_fp, const char *const fname, const int f
FLAG(flags, kFileReadOnly, O_RDONLY, kFalse, wr != kTrue);
#ifdef O_NOFOLLOW
FLAG(flags, kFileNoSymlink, O_NOFOLLOW, kNone, true);
FLAG(flags, kFileMkDir, O_CREAT|O_WRONLY, kTrue, !(flags & kFileCreateOnly));
#endif
#undef FLAG
// wr is used for kFileReadOnly flag, but on
// QB:neovim-qb-slave-ubuntu-12-04-64bit it still errors out with
// `error: variable ‘wr’ set but not used [-Werror=unused-but-set-variable]`
(void)wr;

if (flags & kFileMkDir) {
int mkdir_ret = os_file_mkdir((char *)fname, 0755);
if (mkdir_ret < 0) {
return mkdir_ret;
}
}

const int fd = os_open(fname, os_open_flags, mode);

if (fd < 0) {
Expand Down
1 change: 1 addition & 0 deletions src/nvim/os/fileio.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef enum {
///< be used with kFileCreateOnly.
kFileNonBlocking = 128, ///< Do not restart read() or write() syscall if
///< EAGAIN was encountered.
kFileMkDir = 256,
} FileOpenFlags;

static inline bool file_eof(const FileDescriptor *fp)
Expand Down
31 changes: 31 additions & 0 deletions src/nvim/os/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,37 @@ int os_mkdir_recurse(const char *const dir, int32_t mode, char **const failed_di
return 0;
}

/// Create the parent directory of a file if it does not exist
///
/// @param[in] fname Full path of the file name whose parent directories
/// we want to create
/// @param[in] mode Permissions for the newly-created directory.
///
/// @return `0` for success, libuv error code for failure.
int os_file_mkdir(char *fname, int32_t mode)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
if (!dir_of_file_exists((char_u *)fname)) {
char *tail = path_tail_with_sep(fname);
char *last_char = tail + strlen(tail) - 1;
if (vim_ispathsep(*last_char)) {
emsg(_(e_noname));
return -1;
}
char c = *tail;
*tail = NUL;
int r;
char *failed_dir;
if ((r = os_mkdir_recurse(fname, mode, &failed_dir) < 0)) {
semsg(_(e_mkdir), failed_dir, os_strerror(r));
xfree(failed_dir);
}
*tail = c;
return r;
}
return 0;
}

/// Create a unique temporary directory.
///
/// @param[in] template Template of the path to the directory with XXXXXX
Expand Down
27 changes: 27 additions & 0 deletions test/functional/ex_cmds/write_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ describe(':write', function()
os.remove('test_bkc_file.txt')
os.remove('test_bkc_link.txt')
os.remove('test_fifo')
os.remove('test/write/p_opt.txt')
os.remove('test/write')
os.remove('test')
os.remove(fname)
os.remove(fname_bak)
os.remove(fname_broken)
Expand Down Expand Up @@ -94,6 +97,30 @@ describe(':write', function()
fifo:close()
end)

it("++p creates missing parent directories", function()
eq(0, eval("filereadable('p_opt.txt')"))
command("write ++p p_opt.txt")
eq(1, eval("filereadable('p_opt.txt')"))
os.remove("p_opt.txt")

eq(0, eval("filereadable('p_opt.txt')"))
command("write ++p ./p_opt.txt")
eq(1, eval("filereadable('p_opt.txt')"))
os.remove("p_opt.txt")

eq(0, eval("filereadable('test/write/p_opt.txt')"))
command("write ++p test/write/p_opt.txt")
eq(1, eval("filereadable('test/write/p_opt.txt')"))

eq(('Vim(write):E32: No file name'), pcall_err(command, 'write ++p test_write/'))
if not iswin() then
eq(('Vim(write):E17: "'..funcs.fnamemodify('.', ':p:h')..'" is a directory'),
pcall_err(command, 'write ++p .'))
eq(('Vim(write):E17: "'..funcs.fnamemodify('.', ':p:h')..'" is a directory'),
pcall_err(command, 'write ++p ./'))
end
end)

it('errors out correctly', function()
if isCI('cirrus') then
pending('FIXME: cirrus')
Expand Down
20 changes: 20 additions & 0 deletions test/functional/vimscript/writefile_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ describe('writefile()', function()
pcall_err(command, ('call writefile([42], %s)'):format(ddname_tail)))
end)

it('writefile(..., "p") creates missing parent directories', function()
os.remove(dname)
eq(nil, read_file(dfname))
eq(0, funcs.writefile({'abc', 'def', 'ghi'}, dfname, 'p'))
eq('abc\ndef\nghi\n', read_file(dfname))
os.remove(dfname)
os.remove(dname)
eq(nil, read_file(dfname))
eq(0, funcs.writefile({'\na\nb\n'}, dfname, 'pb'))
eq('\0a\0b\0', read_file(dfname))
os.remove(dfname)
os.remove(dname)
eq('Vim(call):E32: No file name',
pcall_err(command, ('call writefile([], "%s", "p")'):format(dfname .. '.d/')))
eq(('Vim(call):E482: Can\'t open file ./ for writing: illegal operation on a directory'),
pcall_err(command, 'call writefile([], "./", "p")'))
eq(('Vim(call):E482: Can\'t open file . for writing: illegal operation on a directory'),
pcall_err(command, 'call writefile([], ".", "p")'))
end)

it('errors out with invalid arguments', function()
write_file(fname, 'TEST')
eq('Vim(call):E119: Not enough arguments for function: writefile',
Expand Down

0 comments on commit d337814

Please sign in to comment.