return FAIL;
}
+ // Do not sync this buffer yet, may first want to read the file.
+ if (curbuf->b_ml.ml_mfp != NULL)
+ curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES_NOSYNC;
+
// The autocommands in readfile() may change the buffer, but only AFTER
// reading the file.
set_bufref(&old_curbuf, curbuf);
retval = read_buffer(TRUE, eap, flags);
}
+ // Can now sync this buffer in ml_sync_all().
+ if (curbuf->b_ml.ml_mfp != NULL
+ && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
+ curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
+
// if first time loading this buffer, init b_chartab[]
if (curbuf->b_flags & BF_NEVERLOADED)
{
if (arg.cat_seed_len > 0)
arg.cat_seed = header + CRYPT_MAGIC_LEN + arg.cat_salt_len;
if (arg.cat_add_len > 0)
- arg.cat_add = header + CRYPT_MAGIC_LEN + arg.cat_salt_len + arg.cat_seed_len;
+ arg.cat_add = header + CRYPT_MAGIC_LEN
+ + arg.cat_salt_len + arg.cat_seed_len;
return crypt_create(method_nr, key, &arg);
}
if (arg.cat_seed_len > 0)
arg.cat_seed = *header + CRYPT_MAGIC_LEN + arg.cat_salt_len;
if (arg.cat_add_len > 0)
- arg.cat_add = *header + CRYPT_MAGIC_LEN + arg.cat_salt_len + arg.cat_seed_len;
+ arg.cat_add = *header + CRYPT_MAGIC_LEN
+ + arg.cat_salt_len + arg.cat_seed_len;
// TODO: Should this be crypt method specific? (Probably not worth
// it). sha2_seed is pretty bad for large amounts of entropy, so make
}
}
-#ifdef FEAT_SODIUM
- static void
+/*
+ * If the crypt method for "curbuf" does not support encrypting the swap file
+ * then disable the swap file.
+ */
+ void
crypt_check_swapfile_curbuf(void)
{
+#ifdef FEAT_SODIUM
int method = crypt_get_method_nr(curbuf);
if (crypt_method_is_sodium(method))
{
msg_scroll = TRUE;
msg(_("Note: Encryption of swapfile not supported, disabling swap file"));
}
-}
#endif
+}
void
crypt_check_current_method(void)
set_option_value_give_err((char_u *)"key", 0L, p1, OPT_LOCAL);
crypt_free_key(p1);
p1 = curbuf->b_p_key;
-#ifdef FEAT_SODIUM
crypt_check_swapfile_curbuf();
-#endif
}
break;
}
sodium_free(sd_state);
return FAIL;
}
- if (state->method_nr == CRYPT_M_SOD2)
+ // "cat_add" should not be NULL, check anyway for safety
+ if (state->method_nr == CRYPT_M_SOD2 && arg->cat_add != NULL)
{
memcpy(arg->cat_add, &opslimit, sizeof(opslimit));
arg->cat_add += sizeof(opslimit);
exarg_T *eap, // can be NULL!
int flags)
{
+ int retval = FAIL; // jump to "theend" instead of returning
int fd = 0;
int newfile = (flags & READ_NEW);
int check_readonly;
&& !(flags & READ_DUMMY))
{
if (set_rw_fname(fname, sfname) == FAIL)
- return FAIL;
+ goto theend;
}
// Remember the initial values of curbuf, curbuf->b_ffname and
if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
FALSE, curbuf, eap))
{
- int status = OK;
+ retval = OK;
#ifdef FEAT_EVAL
if (aborting())
- status = FAIL;
+ retval = FAIL;
#endif
// The BufReadCmd code usually uses ":read" to get the text and
// perhaps ":file" to change the buffer name. But we should
// consider this to work like ":edit", thus reset the
// BF_NOTEDITED flag. Then ":write" will work to overwrite the
// same file.
- if (status == OK)
+ if (retval == OK)
curbuf->b_flags &= ~BF_NOTEDITED;
- return status;
+ goto theend;
}
}
else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
FALSE, NULL, eap))
+ {
#ifdef FEAT_EVAL
- return aborting() ? FAIL : OK;
+ retval = aborting() ? FAIL : OK;
#else
- return OK;
+ retval = OK;
#endif
+ goto theend;
+ }
curbuf->b_op_start = orig_start;
if (flags & READ_NOFILE)
+ {
// Return NOTDONE instead of FAIL so that BufEnter can be triggered
// and other operations don't fail.
- return NOTDONE;
+ retval = NOTDONE;
+ goto theend;
+ }
}
if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
msg_end();
msg_scroll = msg_save;
- return FAIL;
+ goto theend;
}
// If the name ends in a path separator, we can't open it. Check here,
filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
msg_end();
msg_scroll = msg_save;
- return NOTDONE;
+ retval = NOTDONE;
+ goto theend;
}
}
# endif
)
{
- int retval = FAIL;
-
if (S_ISDIR(perm))
{
filemess(curbuf, fname, (char_u *)_(msg_is_a_directory), 0);
filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
msg_end();
msg_scroll = msg_save;
- return retval;
+ goto theend;
}
#endif
#if defined(MSWIN)
filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
msg_end();
msg_scroll = msg_save;
- return FAIL;
+ goto theend;
}
#endif
}
&& (old_b_fname != curbuf->b_fname)))
{
emsg(_(e_autocommands_changed_buffer_or_buffer_name));
- return FAIL;
+ goto theend;
}
}
if (dir_of_file_exists(fname))
save_file_ff(curbuf);
#if defined(FEAT_EVAL)
- if (aborting()) // autocmds may abort script processing
- return FAIL;
+ if (!aborting()) // autocmds may abort script processing
#endif
- return OK; // a new file is not an error
+ retval = OK; // a new file is not an error
+ goto theend;
}
else
{
}
}
- return FAIL;
+ goto theend;
}
/*
emsg(_(e_autocommands_changed_buffer_or_buffer_name));
if (!read_buffer)
close(fd);
- return FAIL;
+ goto theend;
}
#ifdef UNIX
// Set swap file protection bits after creating it.
{
if (!read_buffer && !read_stdin)
close(fd);
- return FAIL;
+ goto theend;
}
++no_wait_return; // don't wait for return yet
--no_wait_return;
msg_scroll = msg_save;
curbuf->b_p_ro = TRUE; // must use "w!" now
- return FAIL;
+ goto theend;
}
#endif
/*
else
emsg(_(e_readpre_autocommands_must_not_change_current_buffer));
curbuf->b_p_ro = TRUE; // must use "w!" now
- return FAIL;
+ goto theend;
}
}
#ifdef FEAT_VIMINFO
check_marks_read();
#endif
- return OK; // an interrupt isn't really an error
+ retval = OK; // an interrupt isn't really an error
+ goto theend;
}
if (!filtering && !(flags & READ_DUMMY))
msg_scroll = m;
# ifdef FEAT_EVAL
if (aborting()) // autocmds may abort script processing
- return FAIL;
+ goto theend;
# endif
}
- if (recoverymode && error)
- return FAIL;
- return OK;
+ if (!(recoverymode && error))
+ retval = OK;
+
+theend:
+ if (curbuf->b_ml.ml_mfp != NULL
+ && curbuf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES_NOSYNC)
+ // OK to sync the swap file now
+ curbuf->b_ml.ml_mfp->mf_dirty = MF_DIRTY_YES;
+
+ return retval;
}
#if defined(OPEN_CHR_FILES) || defined(PROTO)
if (cryptkey == NULL && !*did_ask)
{
if (*curbuf->b_p_key)
+ {
cryptkey = curbuf->b_p_key;
+ crypt_check_swapfile_curbuf();
+ }
else
{
// When newfile is TRUE, store the typed key in the 'key'
mfp->mf_free_first = NULL; // free list is empty
mfp->mf_used_first = NULL; // used list is empty
mfp->mf_used_last = NULL;
- mfp->mf_dirty = FALSE;
+ mfp->mf_dirty = MF_DIRTY_NO;
mfp->mf_used_count = 0;
mf_hash_init(&mfp->mf_hash);
mf_hash_init(&mfp->mf_trans);
if (mfp->mf_fd < 0)
return FAIL;
- mfp->mf_dirty = TRUE;
+ mfp->mf_dirty = MF_DIRTY_YES;
return OK;
}
}
}
hp->bh_flags = BH_LOCKED | BH_DIRTY; // new block is always dirty
- mfp->mf_dirty = TRUE;
+ mfp->mf_dirty = MF_DIRTY_YES;
hp->bh_page_count = page_count;
mf_ins_used(mfp, hp);
mf_ins_hash(mfp, hp);
if (dirty)
{
flags |= BH_DIRTY;
- mfp->mf_dirty = TRUE;
+ if (mfp->mf_dirty != MF_DIRTY_YES_NOSYNC)
+ mfp->mf_dirty = MF_DIRTY_YES;
}
hp->bh_flags = flags;
if (infile)
bhdr_T *hp;
int got_int_save = got_int;
- if (mfp->mf_fd < 0) // there is no file, nothing to do
+ if (mfp->mf_fd < 0)
{
- mfp->mf_dirty = FALSE;
+ // there is no file, nothing to do
+ mfp->mf_dirty = MF_DIRTY_NO;
return FAIL;
}
* In case of an error this flag is also set, to avoid trying all the time.
*/
if (hp == NULL || status == FAIL)
- mfp->mf_dirty = FALSE;
+ mfp->mf_dirty = MF_DIRTY_NO;
if ((flags & MFS_FLUSH) && *p_sws != NUL)
{
for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
if (hp->bh_bnum > 0)
hp->bh_flags |= BH_DIRTY;
- mfp->mf_dirty = TRUE;
+ mfp->mf_dirty = MF_DIRTY_YES;
}
/*
#define BLOCK0_ID1_C0 'c' // block 0 id 1 'cm' 0
#define BLOCK0_ID1_C1 'C' // block 0 id 1 'cm' 1
#define BLOCK0_ID1_C2 'd' // block 0 id 1 'cm' 2
-// BLOCK0_ID1_C3 and BLOCK0_ID1_C4 are for libsodium enctyption. However, for
+// BLOCK0_ID1_C3 and BLOCK0_ID1_C4 are for libsodium encryption. However, for
// these the swapfile is disabled, thus they will not be used. Added for
// consistency anyway.
#define BLOCK0_ID1_C3 'S' // block 0 id 1 'cm' 3
continue;
if (mf_open_file(mfp, fname) == OK) // consumes fname!
{
+ // don't sync yet in ml_sync_all()
+ mfp->mf_dirty = MF_DIRTY_YES_NOSYNC;
#if defined(MSWIN)
/*
* set full pathname for swap file now, because a ":!cd dir" may
&& b0p->b0_id[1] != BLOCK0_ID1_C0
&& b0p->b0_id[1] != BLOCK0_ID1_C1
&& b0p->b0_id[1] != BLOCK0_ID1_C2
- && b0p->b0_id[1] != BLOCK0_ID1_C3)
+ && b0p->b0_id[1] != BLOCK0_ID1_C3
+ && b0p->b0_id[1] != BLOCK0_ID1_C4)
)
return FAIL;
return OK;
need_check_timestamps = TRUE; // give message later
}
}
- if (buf->b_ml.ml_mfp->mf_dirty)
+ if (buf->b_ml.ml_mfp->mf_dirty == MF_DIRTY_YES)
{
(void)mf_sync(buf->b_ml.ml_mfp, (check_char ? MFS_STOP : 0)
| (bufIsChanged(buf) ? MFS_FLUSH : 0));
void crypt_decode_inplace(cryptstate_T *state, char_u *buf, size_t len, int last);
void crypt_free_key(char_u *key);
void crypt_check_method(int method);
+void crypt_check_swapfile_curbuf(void);
void crypt_check_current_method(void);
char_u *crypt_get_key(int store, int twice);
void crypt_append_msg(buf_T *buf);
int cmod_did_esilent; // incremented when emsg_silent is
} cmdmod_T;
+typedef enum {
+ MF_DIRTY_NO = 0, // no dirty blocks
+ MF_DIRTY_YES, // there are dirty blocks
+ MF_DIRTY_YES_NOSYNC, // there are dirty blocks, do not sync yet
+} mfdirty_T;
+
#define MF_SEED_LEN 8
struct memfile
blocknr_T mf_neg_count; // number of negative blocks numbers
blocknr_T mf_infile_count; // number of pages in the file
unsigned mf_page_size; // number of bytes in a page
- int mf_dirty; // TRUE if there are dirty blocks
+ mfdirty_T mf_dirty;
#ifdef FEAT_CRYPT
buf_T *mf_buffer; // buffer this memfile is for
char_u mf_seed[MF_SEED_LEN]; // seed for encryption
" Tests for encryption.
+source shared.vim
source check.vim
CheckFeature cryptv
call Crypt_uncrypt('xchacha20v2')
endfunc
+func Test_crypt_sodium_v2_startup()
+ CheckFeature sodium
+ CheckRunVimInTerminal
+
+ let buf = RunVimInTerminal('--cmd "set cm=xchacha20v2" -x Xfoo', #{wait_for_ruler: 0, rows: 6})
+ call g:TermWait(buf, g:RunningWithValgrind() ? 1000 : 50)
+ call term_sendkeys(buf, "foo\<CR>foo\<CR>")
+ call term_sendkeys(buf, "ifoo\<Esc>")
+ call term_sendkeys(buf, "ZZ")
+ call TermWait(buf)
+
+ " Wait for Vim to write the file and exit. Then wipe out the terminal buffer.
+ call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})
+ exe buf .. 'bwipe!'
+ call assert_true(filereadable('Xfoo'))
+
+ let buf = RunVimInTerminal('--cmd "set ch=3 cm=xchacha20v2 key=foo" Xfoo', #{rows: 10})
+ call g:TermWait(buf, g:RunningWithValgrind() ? 1000 : 50)
+ call StopVimInTerminal(buf)
+
+ call delete('Xfoo')
+endfunc
+
func Uncrypt_stable(method, crypted_text, key, uncrypted_text)
split Xtest.txt
set bin noeol key= fenc=latin1
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 1583,
/**/
1582,
/**/