replaced last mch_stat() and mch_lstat()

files: memline.c and fileio.c
This commit is contained in:
Stefan Hoffmann
2014-05-03 16:00:03 +02:00
parent c41535d69d
commit 65abdad02a
4 changed files with 140 additions and 157 deletions

View File

@@ -86,7 +86,7 @@ static void set_file_time(char_u *fname, time_t atime, time_t mtime);
static int set_rw_fname(char_u *fname, char_u *sfname); static int set_rw_fname(char_u *fname, char_u *sfname);
static int msg_add_fileformat(int eol_type); static int msg_add_fileformat(int eol_type);
static void msg_add_eol(void); static void msg_add_eol(void);
static int check_mtime(buf_T *buf, struct stat *s); static int check_mtime(buf_T *buf, FileInfo *file_info);
static int time_differs(long t1, long t2); static int time_differs(long t1, long t2);
static int apply_autocmds_exarg(event_T event, char_u *fname, char_u *fname_io, static int apply_autocmds_exarg(event_T event, char_u *fname, char_u *fname_io,
int force, buf_T *buf, int force, buf_T *buf,
@@ -262,7 +262,6 @@ readfile (
#endif #endif
int fileformat = 0; /* end-of-line format */ int fileformat = 0; /* end-of-line format */
int keep_fileformat = FALSE; int keep_fileformat = FALSE;
struct stat st;
int file_readonly; int file_readonly;
linenr_T skip_count = 0; linenr_T skip_count = 0;
linenr_T read_count = 0; linenr_T read_count = 0;
@@ -441,8 +440,9 @@ readfile (
if (newfile && !read_stdin && !read_buffer) { if (newfile && !read_stdin && !read_buffer) {
/* Remember time of file. */ /* Remember time of file. */
if (mch_stat((char *)fname, &st) >= 0) { FileInfo file_info;
buf_store_time(curbuf, &st, fname); if (os_get_file_info((char *)fname, &file_info)) {
buf_store_file_info(curbuf, &file_info, fname);
curbuf->b_mtime_read = curbuf->b_mtime; curbuf->b_mtime_read = curbuf->b_mtime;
#ifdef UNIX #ifdef UNIX
/* /*
@@ -456,7 +456,7 @@ readfile (
* not be able to write to the file ourselves. * not be able to write to the file ourselves.
* Setting the bits is done below, after creating the swap file. * Setting the bits is done below, after creating the swap file.
*/ */
swap_mode = (st.st_mode & 0644) | 0600; swap_mode = (file_info.stat.st_mode & 0644) | 0600;
#endif #endif
} else { } else {
curbuf->b_mtime = 0; curbuf->b_mtime = 0;
@@ -2473,7 +2473,6 @@ buf_write (
int overwriting; /* TRUE if writing over original */ int overwriting; /* TRUE if writing over original */
int no_eol = FALSE; /* no end-of-line written */ int no_eol = FALSE; /* no end-of-line written */
int device = FALSE; /* writing to a device */ int device = FALSE; /* writing to a device */
struct stat st_old;
int prev_got_int = got_int; int prev_got_int = got_int;
bool file_readonly = false; /* overwritten file is read-only */ bool file_readonly = false; /* overwritten file is read-only */
static char *err_readonly = static char *err_readonly =
@@ -2774,15 +2773,14 @@ buf_write (
* Get information about original file (if there is one). * Get information about original file (if there is one).
*/ */
#if defined(UNIX) #if defined(UNIX)
st_old.st_dev = 0;
st_old.st_ino = 0;
perm = -1; perm = -1;
if (mch_stat((char *)fname, &st_old) < 0) FileInfo file_info_old;
if (!os_get_file_info((char *)fname, &file_info_old)) {
newfile = TRUE; newfile = TRUE;
else { } else {
perm = st_old.st_mode; perm = file_info_old.stat.st_mode;
if (!S_ISREG(st_old.st_mode)) { /* not a file */ if (!S_ISREG(file_info_old.stat.st_mode)) { /* not a file */
if (S_ISDIR(st_old.st_mode)) { if (S_ISDIR(file_info_old.stat.st_mode)) {
errnum = (char_u *)"E502: "; errnum = (char_u *)"E502: ";
errmsg = (char_u *)_("is a directory"); errmsg = (char_u *)_("is a directory");
goto fail; goto fail;
@@ -2822,8 +2820,10 @@ buf_write (
errmsg = (char_u *)_("is a directory"); errmsg = (char_u *)_("is a directory");
goto fail; goto fail;
} }
if (overwriting) if (overwriting) {
(void)mch_stat((char *)fname, &st_old); os_get_file_info((char *)fname, &file_info_old);
}
} }
#endif /* !UNIX */ #endif /* !UNIX */
@@ -2849,7 +2849,7 @@ buf_write (
* Check if the timestamp hasn't changed since reading the file. * Check if the timestamp hasn't changed since reading the file.
*/ */
if (overwriting) { if (overwriting) {
retval = check_mtime(buf, &st_old); retval = check_mtime(buf, &file_info_old);
if (retval == FAIL) if (retval == FAIL)
goto fail; goto fail;
} }
@@ -2890,14 +2890,11 @@ buf_write (
* off. This helps when editing large files on almost-full disks. * off. This helps when editing large files on almost-full disks.
*/ */
if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) { if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) {
#if defined(UNIX) || defined(WIN32) FileInfo file_info;
struct stat st;
#endif
if ((bkc_flags & BKC_YES) || append) /* "yes" */ if ((bkc_flags & BKC_YES) || append) { /* "yes" */
backup_copy = TRUE; backup_copy = TRUE;
#if defined(UNIX) || defined(WIN32) } else if ((bkc_flags & BKC_AUTO)) { /* "auto" */
else if ((bkc_flags & BKC_AUTO)) { /* "auto" */
int i; int i;
# ifdef UNIX # ifdef UNIX
@@ -2908,18 +2905,16 @@ buf_write (
* - we don't have write permission in the directory * - we don't have write permission in the directory
* - we can't set the owner/group of the new file * - we can't set the owner/group of the new file
*/ */
if (st_old.st_nlink > 1 if (file_info_old.stat.st_nlink > 1
|| mch_lstat((char *)fname, &st) < 0 || !os_get_file_info_link((char *)fname, &file_info)
|| st.st_dev != st_old.st_dev || !os_file_info_id_equal(&file_info, &file_info_old)
|| st.st_ino != st_old.st_ino
# ifndef HAVE_FCHOWN # ifndef HAVE_FCHOWN
|| st.st_uid != st_old.st_uid || file_info.stat.st_uid != file_info_old.stat.st_uid
|| st.st_gid != st_old.st_gid || file_info.stat.st_gid != file_info_old.stat.st_gid
# endif # endif
) ) {
backup_copy = TRUE; backup_copy = TRUE;
else } else
# else
# endif # endif
{ {
/* /*
@@ -2931,9 +2926,10 @@ buf_write (
STRCPY(IObuff, fname); STRCPY(IObuff, fname);
for (i = 4913;; i += 123) { for (i = 4913;; i += 123) {
sprintf((char *)path_tail(IObuff), "%d", i); sprintf((char *)path_tail(IObuff), "%d", i);
if (mch_lstat((char *)IObuff, &st) < 0) if (!os_get_file_info_link((char *)IObuff, &file_info)) {
break; break;
} }
}
fd = mch_open((char *)IObuff, fd = mch_open((char *)IObuff,
O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
if (fd < 0) /* can't write in directory */ if (fd < 0) /* can't write in directory */
@@ -2941,13 +2937,14 @@ buf_write (
else { else {
# ifdef UNIX # ifdef UNIX
# ifdef HAVE_FCHOWN # ifdef HAVE_FCHOWN
ignored = fchown(fd, st_old.st_uid, st_old.st_gid); fchown(fd, file_info_old.stat.st_uid, file_info_old.stat.st_gid);
# endif # endif
if (mch_stat((char *)IObuff, &st) < 0 if (!os_get_file_info((char *)IObuff, &file_info)
|| st.st_uid != st_old.st_uid || file_info.stat.st_uid != file_info_old.stat.st_uid
|| st.st_gid != st_old.st_gid || file_info.stat.st_gid != file_info_old.stat.st_gid
|| (long)st.st_mode != perm) || (long)file_info.stat.st_mode != perm) {
backup_copy = TRUE; backup_copy = TRUE;
}
# endif # endif
/* Close the file before removing it, on MS-Windows we /* Close the file before removing it, on MS-Windows we
* can't delete an open file. */ * can't delete an open file. */
@@ -2962,27 +2959,25 @@ buf_write (
*/ */
if ((bkc_flags & BKC_BREAKSYMLINK) || (bkc_flags & BKC_BREAKHARDLINK)) { if ((bkc_flags & BKC_BREAKSYMLINK) || (bkc_flags & BKC_BREAKHARDLINK)) {
# ifdef UNIX # ifdef UNIX
int lstat_res; bool file_info_link_ok = os_get_file_info_link((char *)fname, &file_info);
lstat_res = mch_lstat((char *)fname, &st);
/* Symlinks. */ /* Symlinks. */
if ((bkc_flags & BKC_BREAKSYMLINK) if ((bkc_flags & BKC_BREAKSYMLINK)
&& lstat_res == 0 && file_info_link_ok
&& st.st_ino != st_old.st_ino) && !os_file_info_id_equal(&file_info, &file_info_old)) {
backup_copy = FALSE; backup_copy = FALSE;
}
/* Hardlinks. */ /* Hardlinks. */
if ((bkc_flags & BKC_BREAKHARDLINK) if ((bkc_flags & BKC_BREAKHARDLINK)
&& st_old.st_nlink > 1 && file_info_old.stat.st_nlink > 1
&& (lstat_res != 0 || st.st_ino == st_old.st_ino)) && (!file_info_link_ok
|| os_file_info_id_equal(&file_info, &file_info_old))) {
backup_copy = FALSE; backup_copy = FALSE;
# else }
# endif # endif
} }
#endif
/* make sure we have a valid backup extension to use */ /* make sure we have a valid backup extension to use */
if (*p_bex == NUL) if (*p_bex == NUL)
backup_ext = (char_u *)".bak"; backup_ext = (char_u *)".bak";
@@ -2994,7 +2989,6 @@ buf_write (
int bfd; int bfd;
char_u *copybuf, *wp; char_u *copybuf, *wp;
int some_error = FALSE; int some_error = FALSE;
struct stat st_new;
char_u *dirp; char_u *dirp;
char_u *rootname; char_u *rootname;
@@ -3019,12 +3013,6 @@ buf_write (
*/ */
dirp = p_bdir; dirp = p_bdir;
while (*dirp) { while (*dirp) {
#ifdef UNIX
st_new.st_ino = 0;
st_new.st_dev = 0;
st_new.st_gid = 0;
#endif
/* /*
* Isolate one directory name, using an entry in 'bdir'. * Isolate one directory name, using an entry in 'bdir'.
*/ */
@@ -3035,6 +3023,7 @@ buf_write (
goto nobackup; goto nobackup;
} }
FileInfo file_info_new;
{ {
/* /*
* Make backup file name. * Make backup file name.
@@ -3049,20 +3038,17 @@ buf_write (
/* /*
* Check if backup file already exists. * Check if backup file already exists.
*/ */
if (mch_stat((char *)backup, &st_new) >= 0) { if (os_get_file_info((char *)backup, &file_info_new)) {
#ifdef UNIX
/* /*
* Check if backup file is same as original file. * Check if backup file is same as original file.
* May happen when modname() gave the same file back (e.g. silly * May happen when modname() gave the same file back (e.g. silly
* link). If we don't check here, we either ruin the file when * link). If we don't check here, we either ruin the file when
* copying or erase it after writing. * copying or erase it after writing.
*/ */
if (st_new.st_dev == st_old.st_dev if (os_file_info_id_equal(&file_info_new, &file_info_old)) {
&& st_new.st_ino == st_old.st_ino) {
free(backup); free(backup);
backup = NULL; /* no backup file to delete */ backup = NULL; /* no backup file to delete */
} }
#endif
/* /*
* If we are not going to keep the backup file, don't * If we are not going to keep the backup file, don't
@@ -3076,8 +3062,9 @@ buf_write (
wp = backup; wp = backup;
*wp = 'z'; *wp = 'z';
while (*wp > 'a' while (*wp > 'a'
&& mch_stat((char *)backup, &st_new) >= 0) && os_get_file_info((char *)backup, &file_info_new)) {
--*wp; --*wp;
}
/* They all exist??? Must be something wrong. */ /* They all exist??? Must be something wrong. */
if (*wp == 'a') { if (*wp == 'a') {
free(backup); free(backup);
@@ -3114,13 +3101,13 @@ buf_write (
* bits for the group same as the protection bits for * bits for the group same as the protection bits for
* others. * others.
*/ */
if (st_new.st_gid != st_old.st_gid if (file_info_new.stat.st_gid != file_info_old.stat.st_gid
# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
&& fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 && fchown(bfd, (uid_t)-1, file_info_old.stat.st_gid) != 0
# endif # endif
) ) {
os_setperm(backup, os_setperm(backup, (perm & 0707) | ((perm & 07) << 3));
(perm & 0707) | ((perm & 07) << 3)); }
# ifdef HAVE_SELINUX # ifdef HAVE_SELINUX
mch_copy_sec(fname, backup); mch_copy_sec(fname, backup);
# endif # endif
@@ -3155,7 +3142,9 @@ buf_write (
errmsg = (char_u *)_( errmsg = (char_u *)_(
"E508: Can't read file for backup (add ! to override)"); "E508: Can't read file for backup (add ! to override)");
#ifdef UNIX #ifdef UNIX
set_file_time(backup, st_old.st_atime, st_old.st_mtime); set_file_time(backup,
file_info_old.stat.st_atim.tv_sec,
file_info_old.stat.st_mtim.tv_sec);
#endif #endif
#ifdef HAVE_ACL #ifdef HAVE_ACL
mch_set_acl(backup, acl); mch_set_acl(backup, acl);
@@ -3266,7 +3255,8 @@ nobackup:
#if defined(UNIX) #if defined(UNIX)
/* When using ":w!" and the file was read-only: make it writable */ /* When using ":w!" and the file was read-only: make it writable */
if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() if (forceit && perm >= 0 && !(perm & 0200)
&& file_info_old.stat.st_uid == getuid()
&& vim_strchr(p_cpo, CPO_FWRITE) == NULL) { && vim_strchr(p_cpo, CPO_FWRITE) == NULL) {
perm |= 0200; perm |= 0200;
(void)os_setperm(fname, perm); (void)os_setperm(fname, perm);
@@ -3412,15 +3402,14 @@ nobackup:
*/ */
if (errmsg == NULL) { if (errmsg == NULL) {
#ifdef UNIX #ifdef UNIX
struct stat st; FileInfo file_info;
/* Don't delete the file when it's a hard or symbolic link. */ /* Don't delete the file when it's a hard or symbolic link. */
if ((!newfile && st_old.st_nlink > 1) if ((!newfile && file_info_old.stat.st_nlink > 1)
|| (mch_lstat((char *)fname, &st) == 0 || (os_get_file_info_link((char *)fname, &file_info)
&& (st.st_dev != st_old.st_dev && !os_file_info_id_equal(&file_info, &file_info_old))) {
|| st.st_ino != st_old.st_ino)))
errmsg = (char_u *)_("E166: Can't open linked file for writing"); errmsg = (char_u *)_("E166: Can't open linked file for writing");
else } else
#endif #endif
{ {
errmsg = (char_u *)_("E212: Can't open file for writing"); errmsg = (char_u *)_("E212: Can't open file for writing");
@@ -3432,8 +3421,10 @@ nobackup:
if (!(perm & 0200)) if (!(perm & 0200))
made_writable = TRUE; made_writable = TRUE;
perm |= 0200; perm |= 0200;
if (st_old.st_uid != getuid() || st_old.st_gid != getgid()) if (file_info_old.stat.st_uid != getuid()
|| file_info_old.stat.st_gid != getgid()) {
perm &= 0777; perm &= 0777;
}
#endif #endif
if (!append) /* don't remove when appending */ if (!append) /* don't remove when appending */
os_remove((char *)wfname); os_remove((char *)wfname);
@@ -3650,14 +3641,14 @@ restore_backup:
* file. Get the new device and inode number. */ * file. Get the new device and inode number. */
if (backup != NULL && !backup_copy) { if (backup != NULL && !backup_copy) {
# ifdef HAVE_FCHOWN # ifdef HAVE_FCHOWN
struct stat st;
/* don't change the owner when it's already OK, some systems remove /* don't change the owner when it's already OK, some systems remove
* permission or ACL stuff */ * permission or ACL stuff */
if (mch_stat((char *)wfname, &st) < 0 FileInfo file_info;
|| st.st_uid != st_old.st_uid if (!os_get_file_info((char *)wfname, &file_info)
|| st.st_gid != st_old.st_gid) { || file_info.stat.st_uid != file_info_old.stat.st_uid
ignored = fchown(fd, st_old.st_uid, st_old.st_gid); || file_info.stat.st_gid != file_info_old.stat.st_gid) {
fchown(fd, file_info_old.stat.st_uid, file_info_old.stat.st_gid);
if (perm >= 0) /* set permission again, may have changed */ if (perm >= 0) /* set permission again, may have changed */
(void)os_setperm(wfname, perm); (void)os_setperm(wfname, perm);
} }
@@ -3867,7 +3858,9 @@ restore_backup:
free(backup); /* don't delete the file */ free(backup); /* don't delete the file */
backup = NULL; backup = NULL;
#ifdef UNIX #ifdef UNIX
set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); set_file_time((char_u *)org,
file_info_old.stat.st_atim.tv_sec,
file_info_old.stat.st_mtim.tv_sec);
#endif #endif
} }
} }
@@ -3960,8 +3953,8 @@ nofail:
/* Update the timestamp to avoid an "overwrite changed file" /* Update the timestamp to avoid an "overwrite changed file"
* prompt when writing again. */ * prompt when writing again. */
if (mch_stat((char *)fname, &st_old) >= 0) { if (os_get_file_info((char *)fname, &file_info_old)) {
buf_store_time(buf, &st_old, fname); buf_store_file_info(buf, &file_info_old, fname);
buf->b_mtime_read = buf->b_mtime; buf->b_mtime_read = buf->b_mtime;
} }
} }
@@ -4137,10 +4130,11 @@ static void msg_add_eol(void)
* The size isn't checked, because using a tool like "gzip" takes care of * The size isn't checked, because using a tool like "gzip" takes care of
* using the same timestamp but can't set the size. * using the same timestamp but can't set the size.
*/ */
static int check_mtime(buf_T *buf, struct stat *st) static int check_mtime(buf_T *buf, FileInfo *file_info)
{ {
if (buf->b_mtime_read != 0 if (buf->b_mtime_read != 0
&& time_differs((long)st->st_mtime, buf->b_mtime_read)) { && time_differs((long)file_info->stat.st_mtim.tv_sec,
buf->b_mtime_read)) {
msg_scroll = TRUE; /* don't overwrite messages here */ msg_scroll = TRUE; /* don't overwrite messages here */
msg_silent = 0; /* must give this prompt */ msg_silent = 0; /* must give this prompt */
/* don't use emsg() here, don't want to flush the buffers */ /* don't use emsg() here, don't want to flush the buffers */
@@ -5079,8 +5073,6 @@ buf_check_timestamp (
int focus /* called for GUI focus event */ int focus /* called for GUI focus event */
) )
{ {
struct stat st;
int stat_res;
int retval = 0; int retval = 0;
char_u *path; char_u *path;
char_u *tbuf; char_u *tbuf;
@@ -5107,27 +5099,25 @@ buf_check_timestamp (
) )
return 0; return 0;
if ( !(buf->b_flags & BF_NOTEDITED) FileInfo file_info;
bool file_info_ok;
if (!(buf->b_flags & BF_NOTEDITED)
&& buf->b_mtime != 0 && buf->b_mtime != 0
&& ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 && (!(file_info_ok = os_get_file_info((char *)buf->b_ffname, &file_info))
|| time_differs((long)st.st_mtime, buf->b_mtime) || time_differs((long)file_info.stat.st_mtim.tv_sec, buf->b_mtime)
|| st.st_size != buf->b_orig_size || (int)file_info.stat.st_mode != buf->b_orig_mode
#ifdef HAVE_ST_MODE
|| (int)st.st_mode != buf->b_orig_mode
#else
|| os_getperm(buf->b_ffname) != buf->b_orig_mode
#endif
)) { )) {
retval = 1; retval = 1;
/* set b_mtime to stop further warnings (e.g., when executing /* set b_mtime to stop further warnings (e.g., when executing
* FileChangedShell autocmd) */ * FileChangedShell autocmd) */
if (stat_res < 0) { if (!file_info_ok) {
buf->b_mtime = 0; buf->b_mtime = 0;
buf->b_orig_size = 0; buf->b_orig_size = 0;
buf->b_orig_mode = 0; buf->b_orig_mode = 0;
} else } else {
buf_store_time(buf, &st, buf->b_ffname); buf_store_file_info(buf, &file_info, buf->b_ffname);
}
/* Don't do anything for a directory. Might contain the file /* Don't do anything for a directory. Might contain the file
* explorer. */ * explorer. */
@@ -5140,10 +5130,10 @@ buf_check_timestamp (
* was set, the global option value otherwise. * was set, the global option value otherwise.
*/ */
else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
&& !bufIsChanged(buf) && stat_res >= 0) && !bufIsChanged(buf) && file_info_ok)
reload = TRUE; reload = TRUE;
else { else {
if (stat_res < 0) if (!file_info_ok)
reason = "deleted"; reason = "deleted";
else if (bufIsChanged(buf)) else if (bufIsChanged(buf))
reason = "conflict"; reason = "conflict";
@@ -5428,15 +5418,12 @@ void buf_reload(buf_T *buf, int orig_mode)
/* Careful: autocommands may have made "buf" invalid! */ /* Careful: autocommands may have made "buf" invalid! */
} }
void buf_store_time(buf_T *buf, struct stat *st, char_u *fname) // TODO(stefan991): remove unused parameter fname
void buf_store_file_info(buf_T *buf, FileInfo *file_info, char_u *fname)
{ {
buf->b_mtime = (long)st->st_mtime; buf->b_mtime = (long)file_info->stat.st_mtim.tv_sec;
buf->b_orig_size = st->st_size; buf->b_orig_size = file_info->stat.st_size;
#ifdef HAVE_ST_MODE buf->b_orig_mode = (int)file_info->stat.st_mode;
buf->b_orig_mode = (int)st->st_mode;
#else
buf->b_orig_mode = os_getperm(fname);
#endif
} }
/* /*

View File

@@ -2,6 +2,7 @@
#define NEOVIM_FILEIO_H #define NEOVIM_FILEIO_H
#include "buffer_defs.h" #include "buffer_defs.h"
#include "os/os.h"
/* /*
* Struct to save values in before executing autocommands for a buffer that is * Struct to save values in before executing autocommands for a buffer that is
@@ -40,7 +41,7 @@ int vim_rename(char_u *from, char_u *to);
int check_timestamps(int focus); int check_timestamps(int focus);
int buf_check_timestamp(buf_T *buf, int focus); int buf_check_timestamp(buf_T *buf, int focus);
void buf_reload(buf_T *buf, int orig_mode); void buf_reload(buf_T *buf, int orig_mode);
void buf_store_time(buf_T *buf, struct stat *st, char_u *fname); void buf_store_file_info(buf_T *buf, FileInfo *file_info, char_u *fname);
void write_lnum_adjust(linenr_T offset); void write_lnum_adjust(linenr_T offset);
void vim_deltempdir(void); void vim_deltempdir(void);
char_u *vim_tempname(int extra_char); char_u *vim_tempname(int extra_char);

View File

@@ -832,8 +832,6 @@ static void ml_upd_block0(buf_T *buf, upd_block0_T what)
*/ */
static void set_b0_fname(ZERO_BL *b0p, buf_T *buf) static void set_b0_fname(ZERO_BL *b0p, buf_T *buf)
{ {
struct stat st;
if (buf->b_ffname == NULL) if (buf->b_ffname == NULL)
b0p->b0_fname[0] = NUL; b0p->b0_fname[0] = NUL;
else { else {
@@ -861,12 +859,13 @@ static void set_b0_fname(ZERO_BL *b0p, buf_T *buf)
memmove(b0p->b0_fname + 1, uname, ulen); memmove(b0p->b0_fname + 1, uname, ulen);
} }
} }
if (mch_stat((char *)buf->b_ffname, &st) >= 0) { FileInfo file_info;
long_to_char((long)st.st_mtime, b0p->b0_mtime); if (os_get_file_info((char *)buf->b_ffname, &file_info)) {
long_to_char((long)file_info.stat.st_mtim.tv_sec, b0p->b0_mtime);
#ifdef CHECK_INODE #ifdef CHECK_INODE
long_to_char((long)st.st_ino, b0p->b0_ino); long_to_char((long)file_info.stat.st_ino, b0p->b0_ino);
#endif #endif
buf_store_time(buf, &st, buf->b_ffname); buf_store_file_info(buf, &file_info, buf->b_ffname);
buf->b_mtime_read = buf->b_mtime; buf->b_mtime_read = buf->b_mtime;
} else { } else {
long_to_char(0L, b0p->b0_mtime); long_to_char(0L, b0p->b0_mtime);
@@ -943,7 +942,6 @@ void ml_recover(void)
infoptr_T *ip; infoptr_T *ip;
blocknr_T bnum; blocknr_T bnum;
int page_count; int page_count;
struct stat org_stat, swp_stat;
int len; int len;
int directly; int directly;
linenr_T lnum; linenr_T lnum;
@@ -1155,12 +1153,15 @@ void ml_recover(void)
/* /*
* check date of swap file and original file * check date of swap file and original file
*/ */
FileInfo org_file_info;
FileInfo swp_file_info;
mtime = char_to_long(b0p->b0_mtime); mtime = char_to_long(b0p->b0_mtime);
if (curbuf->b_ffname != NULL if (curbuf->b_ffname != NULL
&& mch_stat((char *)curbuf->b_ffname, &org_stat) != -1 && os_get_file_info((char *)curbuf->b_ffname, &org_file_info)
&& ((mch_stat((char *)mfp->mf_fname, &swp_stat) != -1 && ((os_get_file_info((char *)mfp->mf_fname, &swp_file_info)
&& org_stat.st_mtime > swp_stat.st_mtime) && org_file_info.stat.st_mtim.tv_sec
|| org_stat.st_mtime != mtime)) { > swp_file_info.stat.st_mtim.tv_sec)
|| org_file_info.stat.st_mtim.tv_sec != mtime)) {
EMSG(_("E308: Warning: Original file may have been changed")); EMSG(_("E308: Warning: Original file may have been changed"));
} }
out_flush(); out_flush();
@@ -1714,7 +1715,6 @@ static int process_still_running;
*/ */
static time_t swapfile_info(char_u *fname) static time_t swapfile_info(char_u *fname)
{ {
struct stat st;
int fd; int fd;
struct block0 b0; struct block0 b0;
time_t x = (time_t)0; time_t x = (time_t)0;
@@ -1724,18 +1724,19 @@ static time_t swapfile_info(char_u *fname)
#endif #endif
/* print the swap file date */ /* print the swap file date */
if (mch_stat((char *)fname, &st) != -1) { FileInfo file_info;
if (os_get_file_info((char *)fname, &file_info)) {
#ifdef UNIX #ifdef UNIX
/* print name of owner of the file */ /* print name of owner of the file */
if (os_get_uname(st.st_uid, uname, B0_UNAME_SIZE) == OK) { if (os_get_uname(file_info.stat.st_uid, uname, B0_UNAME_SIZE) == OK) {
MSG_PUTS(_(" owned by: ")); MSG_PUTS(_(" owned by: "));
msg_outtrans((char_u *)uname); msg_outtrans((char_u *)uname);
MSG_PUTS(_(" dated: ")); MSG_PUTS(_(" dated: "));
} else } else
#endif #endif
MSG_PUTS(_(" dated: ")); MSG_PUTS(_(" dated: "));
x = st.st_mtime; /* Manx C can't do &st.st_mtime */ x = file_info.stat.st_mtim.tv_sec;
p = ctime(&x); /* includes '\n' */ p = ctime(&x); // includes '\n'
if (p == NULL) if (p == NULL)
MSG_PUTS("(invalid)\n"); MSG_PUTS("(invalid)\n");
else else
@@ -1847,7 +1848,6 @@ static int recov_file_names(char_u **names, char_u *path, int prepend_dot)
void ml_sync_all(int check_file, int check_char) void ml_sync_all(int check_file, int check_char)
{ {
buf_T *buf; buf_T *buf;
struct stat st;
for (buf = firstbuf; buf != NULL; buf = buf->b_next) { for (buf = firstbuf; buf != NULL; buf = buf->b_next) {
if (buf->b_ml.ml_mfp == NULL || buf->b_ml.ml_mfp->mf_fname == NULL) if (buf->b_ml.ml_mfp == NULL || buf->b_ml.ml_mfp->mf_fname == NULL)
@@ -1862,9 +1862,10 @@ void ml_sync_all(int check_file, int check_char)
* If the original file does not exist anymore or has been changed * If the original file does not exist anymore or has been changed
* call ml_preserve() to get rid of all negative numbered blocks. * call ml_preserve() to get rid of all negative numbered blocks.
*/ */
if (mch_stat((char *)buf->b_ffname, &st) == -1 FileInfo file_info;
|| st.st_mtime != buf->b_mtime_read if (!os_get_file_info((char *)buf->b_ffname, &file_info)
|| st.st_size != buf->b_orig_size) { || file_info.stat.st_mtim.tv_sec != buf->b_mtime_read
|| (off_t)file_info.stat.st_size != buf->b_orig_size) {
ml_preserve(buf, FALSE); ml_preserve(buf, FALSE);
did_check_timestamps = FALSE; did_check_timestamps = FALSE;
need_check_timestamps = TRUE; /* give message later */ need_check_timestamps = TRUE; /* give message later */
@@ -3432,7 +3433,6 @@ attention_message (
char_u *fname /* swap file name */ char_u *fname /* swap file name */
) )
{ {
struct stat st;
time_t x, sx; time_t x, sx;
char *p; char *p;
@@ -3445,10 +3445,11 @@ attention_message (
MSG_PUTS(_("While opening file \"")); MSG_PUTS(_("While opening file \""));
msg_outtrans(buf->b_fname); msg_outtrans(buf->b_fname);
MSG_PUTS("\"\n"); MSG_PUTS("\"\n");
if (mch_stat((char *)buf->b_fname, &st) != -1) { FileInfo file_info;
if (os_get_file_info((char *)buf->b_fname, &file_info)) {
MSG_PUTS(_(" dated: ")); MSG_PUTS(_(" dated: "));
x = st.st_mtime; /* Manx C can't do &st.st_mtime */ x = file_info.stat.st_mtim.tv_sec;
p = ctime(&x); /* includes '\n' */ p = ctime(&x); // includes '\n'
if (p == NULL) if (p == NULL)
MSG_PUTS("(invalid)\n"); MSG_PUTS("(invalid)\n");
else else
@@ -3556,20 +3557,12 @@ findswapname (
fname = NULL; fname = NULL;
break; break;
} }
/* // check if the swapfile already exists
* check if the swapfile already exists // Extra security check: When a swap file is a symbolic link, this
*/ // is most likely a symlink attack.
if (!os_file_exists(fname)) { /* it does not exist */ FileInfo file_info;
#ifdef HAVE_LSTAT bool file_or_link_found = os_get_file_info_link((char *)fname, &file_info);
struct stat sb; if (!file_or_link_found) {
/*
* Extra security check: When a swap file is a symbolic link, this
* is most likely a symlink attack.
*/
if (mch_lstat((char *)fname, &sb) < 0)
#else
#endif
break; break;
} }
@@ -3831,7 +3824,6 @@ fnamecmp_ino (
long ino_block0 long ino_block0
) )
{ {
struct stat st;
ino_t ino_c = 0; /* ino of current file */ ino_t ino_c = 0; /* ino of current file */
ino_t ino_s; /* ino of file from swap file */ ino_t ino_s; /* ino of file from swap file */
char_u buf_c[MAXPATHL]; /* full path of fname_c */ char_u buf_c[MAXPATHL]; /* full path of fname_c */
@@ -3839,18 +3831,21 @@ fnamecmp_ino (
int retval_c; /* flag: buf_c valid */ int retval_c; /* flag: buf_c valid */
int retval_s; /* flag: buf_s valid */ int retval_s; /* flag: buf_s valid */
if (mch_stat((char *)fname_c, &st) == 0) FileInfo file_info;
ino_c = (ino_t)st.st_ino; if (os_get_file_info((char *)fname_c, &file_info)) {
ino_c = (ino_t)file_info.stat.st_ino;
}
/* /*
* First we try to get the inode from the file name, because the inode in * First we try to get the inode from the file name, because the inode in
* the swap file may be outdated. If that fails (e.g. this path is not * the swap file may be outdated. If that fails (e.g. this path is not
* valid on this machine), use the inode from block 0. * valid on this machine), use the inode from block 0.
*/ */
if (mch_stat((char *)fname_s, &st) == 0) if (os_get_file_info((char *)fname_s, &file_info)) {
ino_s = (ino_t)st.st_ino; ino_s = (ino_t)file_info.stat.st_ino;
else } else {
ino_s = (ino_t)ino_block0; ino_s = (ino_t)ino_block0;
}
if (ino_c && ino_s) if (ino_c && ino_s)
return ino_c != ino_s; return ino_c != ino_s;

View File

@@ -229,6 +229,7 @@
# define MAXPATHL 1024 # define MAXPATHL 1024
#endif #endif
// TODO(stefan991): remove macro
#define CHECK_INODE /* used when checking if a swap file already #define CHECK_INODE /* used when checking if a swap file already
exists for a file */ exists for a file */
# ifndef DFLT_MAXMEM # ifndef DFLT_MAXMEM
@@ -276,7 +277,6 @@
#endif #endif
#define HAVE_DUP /* have dup() */ #define HAVE_DUP /* have dup() */
#define HAVE_ST_MODE /* have stat.st_mode */
/* We have three kinds of ACL support. */ /* We have three kinds of ACL support. */
#define HAVE_ACL (HAVE_POSIX_ACL || HAVE_SOLARIS_ACL || HAVE_AIX_ACL) #define HAVE_ACL (HAVE_POSIX_ACL || HAVE_SOLARIS_ACL || HAVE_AIX_ACL)