#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
# include "vimio.h"
#endif
#include "vim.h"
#ifdef HAVE_ST_BLKSIZE
# define STATFS stat
# define F_BSIZE st_blksize
# define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
#else
# ifdef HAVE_SYS_STATFS_H
# include <sys/statfs.h>
# define STATFS statfs
# define F_BSIZE f_bsize
# ifdef __MINT__
# define fstatfs(fd, buf, len, nul) mch_fstat((fd), (buf))
# endif
# endif
#endif
#ifdef AMIGA
# ifdef FEAT_ARP
extern int dos2;
# endif
# ifdef SASC
# include <proto/dos.h>
# include <ios1.h>
# endif
#endif
#define MEMFILE_PAGE_SIZE 4096
static long_u total_mem_used = 0;
static void mf_ins_hash __ARGS((memfile_T *, bhdr_T *));
static void mf_rem_hash __ARGS((memfile_T *, bhdr_T *));
static bhdr_T *mf_find_hash __ARGS((memfile_T *, blocknr_T));
static void mf_ins_used __ARGS((memfile_T *, bhdr_T *));
static void mf_rem_used __ARGS((memfile_T *, bhdr_T *));
static bhdr_T *mf_release __ARGS((memfile_T *, int));
static bhdr_T *mf_alloc_bhdr __ARGS((memfile_T *, int));
static void mf_free_bhdr __ARGS((bhdr_T *));
static void mf_ins_free __ARGS((memfile_T *, bhdr_T *));
static bhdr_T *mf_rem_free __ARGS((memfile_T *));
static int mf_read __ARGS((memfile_T *, bhdr_T *));
static int mf_write __ARGS((memfile_T *, bhdr_T *));
static int mf_trans_add __ARGS((memfile_T *, bhdr_T *));
static void mf_do_open __ARGS((memfile_T *, char_u *, int));
memfile_T *
mf_open(fname, flags)
char_u *fname;
int flags;
{
memfile_T *mfp;
int i;
off_t size;
#if defined(STATFS) && defined(UNIX) && !defined(__QNX__)
# define USE_FSTATFS
struct STATFS stf;
#endif
if ((mfp = (memfile_T *)alloc((unsigned)sizeof(memfile_T))) == NULL)
return NULL;
if (fname == NULL)
{
mfp->mf_fname = NULL;
mfp->mf_ffname = NULL;
mfp->mf_fd = -1;
}
else
{
mf_do_open(mfp, fname, flags);
if (mfp->mf_fd < 0)
{
vim_free(mfp);
return NULL;
}
}
mfp->mf_free_first = NULL;
mfp->mf_used_first = NULL;
mfp->mf_used_last = NULL;
mfp->mf_dirty = FALSE;
mfp->mf_used_count = 0;
for (i = 0; i < MEMHASHSIZE; ++i)
{
mfp->mf_hash[i] = NULL;
mfp->mf_trans[i] = NULL;
}
mfp->mf_page_size = MEMFILE_PAGE_SIZE;
#ifdef USE_FSTATFS
if (mfp->mf_fd >= 0
&& fstatfs(mfp->mf_fd, &stf, sizeof(struct statfs), 0) == 0
&& stf.F_BSIZE >= MIN_SWAP_PAGE_SIZE
&& stf.F_BSIZE <= MAX_SWAP_PAGE_SIZE)
mfp->mf_page_size = stf.F_BSIZE;
#endif
if (mfp->mf_fd < 0 || (flags & (O_TRUNC|O_EXCL))
|| (size = lseek(mfp->mf_fd, (off_t)0L, SEEK_END)) <= 0)
mfp->mf_blocknr_max = 0;
else
mfp->mf_blocknr_max = (blocknr_T)((size + mfp->mf_page_size - 1)
/ mfp->mf_page_size);
mfp->mf_blocknr_min = -1;
mfp->mf_neg_count = 0;
mfp->mf_infile_count = mfp->mf_blocknr_max;
{
int shift = 10;
unsigned page_size = mfp->mf_page_size;
while (shift > 0 && (page_size & 1) == 0)
{
page_size = page_size >> 1;
--shift;
}
mfp->mf_used_count_max = (p_mm << shift) / page_size;
if (mfp->mf_used_count_max < 10)
mfp->mf_used_count_max = 10;
}
return mfp;
}
int
mf_open_file(mfp, fname)
memfile_T *mfp;
char_u *fname;
{
mf_do_open(mfp, fname, O_RDWR|O_CREAT|O_EXCL);
if (mfp->mf_fd < 0)
return FAIL;
mfp->mf_dirty = TRUE;
return OK;
}
void
mf_close(mfp, del_file)
memfile_T *mfp;
int del_file;
{
bhdr_T *hp, *nextp;
NR_TRANS *tp, *tpnext;
int i;
if (mfp == NULL)
return;
if (mfp->mf_fd >= 0)
{
if (close(mfp->mf_fd) < 0)
EMSG(_(e_swapclose));
}
if (del_file && mfp->mf_fname != NULL)
mch_remove(mfp->mf_fname);
for (hp = mfp->mf_used_first; hp != NULL; hp = nextp)
{
total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
nextp = hp->bh_next;
mf_free_bhdr(hp);
}
while (mfp->mf_free_first != NULL)
vim_free(mf_rem_free(mfp));
for (i = 0; i < MEMHASHSIZE; ++i)
for (tp = mfp->mf_trans[i]; tp != NULL; tp = tpnext)
{
tpnext = tp->nt_next;
vim_free(tp);
}
vim_free(mfp->mf_fname);
vim_free(mfp->mf_ffname);
vim_free(mfp);
}
void
mf_close_file(buf, getlines)
buf_T *buf;
int getlines;
{
memfile_T *mfp;
linenr_T lnum;
mfp = buf->b_ml.ml_mfp;
if (mfp == NULL || mfp->mf_fd < 0)
return;
if (getlines)
{
mf_dont_release = TRUE;
for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
(void)ml_get_buf(buf, lnum, FALSE);
mf_dont_release = FALSE;
}
if (close(mfp->mf_fd) < 0)
EMSG(_(e_swapclose));
mfp->mf_fd = -1;
if (mfp->mf_fname != NULL)
{
mch_remove(mfp->mf_fname);
vim_free(mfp->mf_fname);
vim_free(mfp->mf_ffname);
mfp->mf_fname = NULL;
mfp->mf_ffname = NULL;
}
}
void
mf_new_page_size(mfp, new_size)
memfile_T *mfp;
unsigned new_size;
{
total_mem_used += new_size - mfp->mf_page_size;
mfp->mf_page_size = new_size;
}
bhdr_T *
mf_new(mfp, negative, page_count)
memfile_T *mfp;
int negative;
int page_count;
{
bhdr_T *hp;
bhdr_T *freep;
char_u *p;
hp = mf_release(mfp, page_count);
freep = mfp->mf_free_first;
if (!negative && freep != NULL && freep->bh_page_count >= page_count)
{
if (freep->bh_page_count > page_count)
{
if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
return NULL;
hp->bh_bnum = freep->bh_bnum;
freep->bh_bnum += page_count;
freep->bh_page_count -= page_count;
}
else if (hp == NULL)
{
if ((p = (char_u *)alloc(mfp->mf_page_size * page_count)) == NULL)
return NULL;
hp = mf_rem_free(mfp);
hp->bh_data = p;
}
else
{
freep = mf_rem_free(mfp);
hp->bh_bnum = freep->bh_bnum;
vim_free(freep);
}
}
else
{
if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
return NULL;
if (negative)
{
hp->bh_bnum = mfp->mf_blocknr_min--;
mfp->mf_neg_count++;
}
else
{
hp->bh_bnum = mfp->mf_blocknr_max;
mfp->mf_blocknr_max += page_count;
}
}
hp->bh_flags = BH_LOCKED | BH_DIRTY;
mfp->mf_dirty = TRUE;
hp->bh_page_count = page_count;
mf_ins_used(mfp, hp);
mf_ins_hash(mfp, hp);
(void)vim_memset((char *)(hp->bh_data), 0, (size_t)mfp->mf_page_size);
return hp;
}
bhdr_T *
mf_get(mfp, nr, page_count)
memfile_T *mfp;
blocknr_T nr;
int page_count;
{
bhdr_T *hp;
if (nr >= mfp->mf_blocknr_max || nr <= mfp->mf_blocknr_min)
return NULL;
hp = mf_find_hash(mfp, nr);
if (hp == NULL)
{
if (nr < 0 || nr >= mfp->mf_infile_count)
return NULL;
hp = mf_release(mfp, page_count);
if (hp == NULL && (hp = mf_alloc_bhdr(mfp, page_count)) == NULL)
return NULL;
hp->bh_bnum = nr;
hp->bh_flags = 0;
hp->bh_page_count = page_count;
if (mf_read(mfp, hp) == FAIL)
{
mf_free_bhdr(hp);
return NULL;
}
}
else
{
mf_rem_used(mfp, hp);
mf_rem_hash(mfp, hp);
}
hp->bh_flags |= BH_LOCKED;
mf_ins_used(mfp, hp);
mf_ins_hash(mfp, hp);
return hp;
}
void
mf_put(mfp, hp, dirty, infile)
memfile_T *mfp;
bhdr_T *hp;
int dirty;
int infile;
{
int flags;
flags = hp->bh_flags;
if ((flags & BH_LOCKED) == 0)
EMSG(_("E293: block was not locked"));
flags &= ~BH_LOCKED;
if (dirty)
{
flags |= BH_DIRTY;
mfp->mf_dirty = TRUE;
}
hp->bh_flags = flags;
if (infile)
mf_trans_add(mfp, hp);
}
void
mf_free(mfp, hp)
memfile_T *mfp;
bhdr_T *hp;
{
vim_free(hp->bh_data);
mf_rem_hash(mfp, hp);
mf_rem_used(mfp, hp);
if (hp->bh_bnum < 0)
{
vim_free(hp);
mfp->mf_neg_count--;
}
else
mf_ins_free(mfp, hp);
}
#if defined(__MORPHOS__) && defined(__libnix__)
extern unsigned long *__stdfiledes;
static unsigned long
fdtofh(int filedescriptor)
{
return __stdfiledes[filedescriptor];
}
#endif
int
mf_sync(mfp, flags)
memfile_T *mfp;
int flags;
{
int status;
bhdr_T *hp;
#if defined(SYNC_DUP_CLOSE) && !defined(MSDOS)
int fd;
#endif
int got_int_save = got_int;
if (mfp->mf_fd < 0)
{
mfp->mf_dirty = FALSE;
return FAIL;
}
got_int = FALSE;
status = OK;
for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
if (((flags & MFS_ALL) || hp->bh_bnum >= 0)
&& (hp->bh_flags & BH_DIRTY)
&& (status == OK || (hp->bh_bnum >= 0
&& hp->bh_bnum < mfp->mf_infile_count)))
{
if ((flags & MFS_ZERO) && hp->bh_bnum != 0)
continue;
if (mf_write(mfp, hp) == FAIL)
{
if (status == FAIL)
break;
status = FAIL;
}
if (flags & MFS_STOP)
{
if (ui_char_avail())
break;
}
else
ui_breakcheck();
if (got_int)
break;
}
if (hp == NULL || status == FAIL)
mfp->mf_dirty = FALSE;
if ((flags & MFS_FLUSH) && *p_sws != NUL)
{
#if defined(UNIX)
# ifdef HAVE_FSYNC
# if defined(__EMX__)
error "Dont use fsync with EMX! Read emxdoc.doc or emxfix01.doc for info."
# endif
if (STRCMP(p_sws, "fsync") == 0)
{
if (fsync(mfp->mf_fd))
status = FAIL;
}
else
# endif
# if defined(__OPENNT) || defined(__TANDEM)
fflush(NULL);
# else
sync();
# endif
#endif
#ifdef VMS
if (STRCMP(p_sws, "fsync") == 0)
{
if (fsync(mfp->mf_fd))
status = FAIL;
}
#endif
#ifdef MSDOS
if (_dos_commit(mfp->mf_fd))
status = FAIL;
#else
# ifdef SYNC_DUP_CLOSE
if ((fd = dup(mfp->mf_fd)) >= 0)
close(fd);
# endif
#endif
#ifdef AMIGA
# if defined(__AROS__) || defined(__amigaos4__)
if (fsync(mfp->mf_fd) != 0)
status = FAIL;
# else
# ifdef FEAT_ARP
if (dos2)
# endif
# ifdef SASC
{
struct UFB *fp = chkufb(mfp->mf_fd);
if (fp != NULL)
Flush(fp->ufbfh);
}
# else
# if defined(_DCC) || defined(__GNUC__) || defined(__MORPHOS__)
{
# if defined(__GNUC__) && !defined(__MORPHOS__) && defined(__libnix__)
extern unsigned long fdtofh(int filedescriptor);
# endif
# if !defined(__libnix__)
fflush(NULL);
# else
BPTR fh = (BPTR)fdtofh(mfp->mf_fd);
if (fh != 0)
Flush(fh);
# endif
}
# else
Flush(_devtab[mfp->mf_fd].fd);
# endif
# endif
# endif
#endif
}
got_int |= got_int_save;
return status;
}
void
mf_set_dirty(mfp)
memfile_T *mfp;
{
bhdr_T *hp;
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;
}
static void
mf_ins_hash(mfp, hp)
memfile_T *mfp;
bhdr_T *hp;
{
bhdr_T *hhp;
int hash;
hash = MEMHASH(hp->bh_bnum);
hhp = mfp->mf_hash[hash];
hp->bh_hash_next = hhp;
hp->bh_hash_prev = NULL;
if (hhp != NULL)
hhp->bh_hash_prev = hp;
mfp->mf_hash[hash] = hp;
}
static void
mf_rem_hash(mfp, hp)
memfile_T *mfp;
bhdr_T *hp;
{
if (hp->bh_hash_prev == NULL)
mfp->mf_hash[MEMHASH(hp->bh_bnum)] = hp->bh_hash_next;
else
hp->bh_hash_prev->bh_hash_next = hp->bh_hash_next;
if (hp->bh_hash_next)
hp->bh_hash_next->bh_hash_prev = hp->bh_hash_prev;
}
static bhdr_T *
mf_find_hash(mfp, nr)
memfile_T *mfp;
blocknr_T nr;
{
bhdr_T *hp;
for (hp = mfp->mf_hash[MEMHASH(nr)]; hp != NULL; hp = hp->bh_hash_next)
if (hp->bh_bnum == nr)
break;
return hp;
}
static void
mf_ins_used(mfp, hp)
memfile_T *mfp;
bhdr_T *hp;
{
hp->bh_next = mfp->mf_used_first;
mfp->mf_used_first = hp;
hp->bh_prev = NULL;
if (hp->bh_next == NULL)
mfp->mf_used_last = hp;
else
hp->bh_next->bh_prev = hp;
mfp->mf_used_count += hp->bh_page_count;
total_mem_used += hp->bh_page_count * mfp->mf_page_size;
}
static void
mf_rem_used(mfp, hp)
memfile_T *mfp;
bhdr_T *hp;
{
if (hp->bh_next == NULL)
mfp->mf_used_last = hp->bh_prev;
else
hp->bh_next->bh_prev = hp->bh_prev;
if (hp->bh_prev == NULL)
mfp->mf_used_first = hp->bh_next;
else
hp->bh_prev->bh_next = hp->bh_next;
mfp->mf_used_count -= hp->bh_page_count;
total_mem_used -= hp->bh_page_count * mfp->mf_page_size;
}
static bhdr_T *
mf_release(mfp, page_count)
memfile_T *mfp;
int page_count;
{
bhdr_T *hp;
int need_release;
buf_T *buf;
if (mf_dont_release)
return NULL;
need_release = ((mfp->mf_used_count >= mfp->mf_used_count_max)
|| (total_mem_used >> 10) >= (long_u)p_mmt);
if (mfp->mf_fd < 0 && need_release && p_uc)
{
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
if (buf->b_ml.ml_mfp == mfp)
break;
if (buf != NULL && buf->b_may_swap)
ml_open_file(buf);
}
if (mfp->mf_fd < 0 || !need_release)
return NULL;
for (hp = mfp->mf_used_last; hp != NULL; hp = hp->bh_prev)
if (!(hp->bh_flags & BH_LOCKED))
break;
if (hp == NULL)
return NULL;
if ((hp->bh_flags & BH_DIRTY) && mf_write(mfp, hp) == FAIL)
return NULL;
mf_rem_used(mfp, hp);
mf_rem_hash(mfp, hp);
if (hp->bh_page_count != page_count)
{
vim_free(hp->bh_data);
if ((hp->bh_data = alloc(mfp->mf_page_size * page_count)) == NULL)
{
vim_free(hp);
return NULL;
}
hp->bh_page_count = page_count;
}
return hp;
}
int
mf_release_all()
{
buf_T *buf;
memfile_T *mfp;
bhdr_T *hp;
int retval = FALSE;
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
{
mfp = buf->b_ml.ml_mfp;
if (mfp != NULL)
{
if (mfp->mf_fd < 0 && buf->b_may_swap)
ml_open_file(buf);
if (mfp->mf_fd >= 0)
{
for (hp = mfp->mf_used_last; hp != NULL; )
{
if (!(hp->bh_flags & BH_LOCKED)
&& (!(hp->bh_flags & BH_DIRTY)
|| mf_write(mfp, hp) != FAIL))
{
mf_rem_used(mfp, hp);
mf_rem_hash(mfp, hp);
mf_free_bhdr(hp);
hp = mfp->mf_used_last;
retval = TRUE;
}
else
hp = hp->bh_prev;
}
}
}
}
return retval;
}
static bhdr_T *
mf_alloc_bhdr(mfp, page_count)
memfile_T *mfp;
int page_count;
{
bhdr_T *hp;
if ((hp = (bhdr_T *)alloc((unsigned)sizeof(bhdr_T))) != NULL)
{
if ((hp->bh_data = (char_u *)alloc(mfp->mf_page_size * page_count))
== NULL)
{
vim_free(hp);
return NULL;
}
hp->bh_page_count = page_count;
}
return hp;
}
static void
mf_free_bhdr(hp)
bhdr_T *hp;
{
vim_free(hp->bh_data);
vim_free(hp);
}
static void
mf_ins_free(mfp, hp)
memfile_T *mfp;
bhdr_T *hp;
{
hp->bh_next = mfp->mf_free_first;
mfp->mf_free_first = hp;
}
static bhdr_T *
mf_rem_free(mfp)
memfile_T *mfp;
{
bhdr_T *hp;
hp = mfp->mf_free_first;
mfp->mf_free_first = hp->bh_next;
return hp;
}
static int
mf_read(mfp, hp)
memfile_T *mfp;
bhdr_T *hp;
{
off_t offset;
unsigned page_size;
unsigned size;
if (mfp->mf_fd < 0)
return FAIL;
page_size = mfp->mf_page_size;
offset = (off_t)page_size * hp->bh_bnum;
size = page_size * hp->bh_page_count;
if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
{
PERROR(_("E294: Seek error in swap file read"));
return FAIL;
}
if ((unsigned)vim_read(mfp->mf_fd, hp->bh_data, size) != size)
{
PERROR(_("E295: Read error in swap file"));
return FAIL;
}
return OK;
}
static int
mf_write(mfp, hp)
memfile_T *mfp;
bhdr_T *hp;
{
off_t offset;
blocknr_T nr;
bhdr_T *hp2;
unsigned page_size;
unsigned page_count;
unsigned size;
if (mfp->mf_fd < 0)
return FAIL;
if (hp->bh_bnum < 0)
if (mf_trans_add(mfp, hp) == FAIL)
return FAIL;
page_size = mfp->mf_page_size;
for (;;)
{
nr = hp->bh_bnum;
if (nr > mfp->mf_infile_count)
{
nr = mfp->mf_infile_count;
hp2 = mf_find_hash(mfp, nr);
}
else
hp2 = hp;
offset = (off_t)page_size * nr;
if (lseek(mfp->mf_fd, offset, SEEK_SET) != offset)
{
PERROR(_("E296: Seek error in swap file write"));
return FAIL;
}
if (hp2 == NULL)
page_count = 1;
else
page_count = hp2->bh_page_count;
size = page_size * page_count;
if ((unsigned)vim_write(mfp->mf_fd,
(hp2 == NULL ? hp : hp2)->bh_data, size) != size)
{
if (!did_swapwrite_msg)
EMSG(_("E297: Write error in swap file"));
did_swapwrite_msg = TRUE;
return FAIL;
}
did_swapwrite_msg = FALSE;
if (hp2 != NULL)
hp2->bh_flags &= ~BH_DIRTY;
if (nr + (blocknr_T)page_count > mfp->mf_infile_count)
mfp->mf_infile_count = nr + page_count;
if (nr == hp->bh_bnum)
break;
}
return OK;
}
static int
mf_trans_add(mfp, hp)
memfile_T *mfp;
bhdr_T *hp;
{
bhdr_T *freep;
blocknr_T new_bnum;
int hash;
NR_TRANS *np;
int page_count;
if (hp->bh_bnum >= 0)
return OK;
if ((np = (NR_TRANS *)alloc((unsigned)sizeof(NR_TRANS))) == NULL)
return FAIL;
freep = mfp->mf_free_first;
page_count = hp->bh_page_count;
if (freep != NULL && freep->bh_page_count >= page_count)
{
new_bnum = freep->bh_bnum;
if (freep->bh_page_count > page_count)
{
freep->bh_bnum += page_count;
freep->bh_page_count -= page_count;
}
else
{
freep = mf_rem_free(mfp);
vim_free(freep);
}
}
else
{
new_bnum = mfp->mf_blocknr_max;
mfp->mf_blocknr_max += page_count;
}
np->nt_old_bnum = hp->bh_bnum;
np->nt_new_bnum = new_bnum;
mf_rem_hash(mfp, hp);
hp->bh_bnum = new_bnum;
mf_ins_hash(mfp, hp);
hash = MEMHASH(np->nt_old_bnum);
np->nt_next = mfp->mf_trans[hash];
mfp->mf_trans[hash] = np;
if (np->nt_next != NULL)
np->nt_next->nt_prev = np;
np->nt_prev = NULL;
return OK;
}
blocknr_T
mf_trans_del(mfp, old_nr)
memfile_T *mfp;
blocknr_T old_nr;
{
int hash;
NR_TRANS *np;
blocknr_T new_bnum;
hash = MEMHASH(old_nr);
for (np = mfp->mf_trans[hash]; np != NULL; np = np->nt_next)
if (np->nt_old_bnum == old_nr)
break;
if (np == NULL)
return old_nr;
mfp->mf_neg_count--;
new_bnum = np->nt_new_bnum;
if (np->nt_prev != NULL)
np->nt_prev->nt_next = np->nt_next;
else
mfp->mf_trans[hash] = np->nt_next;
if (np->nt_next != NULL)
np->nt_next->nt_prev = np->nt_prev;
vim_free(np);
return new_bnum;
}
void
mf_set_ffname(mfp)
memfile_T *mfp;
{
mfp->mf_ffname = FullName_save(mfp->mf_fname, FALSE);
}
void
mf_fullname(mfp)
memfile_T *mfp;
{
if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL)
{
vim_free(mfp->mf_fname);
mfp->mf_fname = mfp->mf_ffname;
mfp->mf_ffname = NULL;
}
}
int
mf_need_trans(mfp)
memfile_T *mfp;
{
return (mfp->mf_fname != NULL && mfp->mf_neg_count > 0);
}
static void
mf_do_open(mfp, fname, flags)
memfile_T *mfp;
char_u *fname;
int flags;
{
#ifdef HAVE_LSTAT
struct stat sb;
#endif
mfp->mf_fname = fname;
mf_set_ffname(mfp);
#if defined(MSDOS) || defined(MSWIN) || defined(RISCOS)
mf_fullname(mfp);
#endif
#ifdef HAVE_LSTAT
if ((flags & O_CREAT) && mch_lstat((char *)mfp->mf_fname, &sb) >= 0)
{
mfp->mf_fd = -1;
EMSG(_("E300: Swap file already exists (symlink attack?)"));
}
else
#endif
{
flags |= O_EXTRA | O_NOFOLLOW;
#ifdef WIN32
flags |= O_NOINHERIT;
#endif
mfp->mf_fd = mch_open_rw((char *)mfp->mf_fname, flags);
}
if (mfp->mf_fd < 0)
{
vim_free(mfp->mf_fname);
vim_free(mfp->mf_ffname);
mfp->mf_fname = NULL;
mfp->mf_ffname = NULL;
}
else
{
#ifdef HAVE_SELINUX
mch_copy_sec(fname, mfp->mf_fname);
#endif
mch_hide(mfp->mf_fname);
}
}