refactor(fileio.c): normalize ifdefs

As well as improving readbability, this also avoids all Treesitter
parsing errors which cannot handle elaborate use of the preprocessor.
This commit is contained in:
Lewis Russell
2023-01-31 12:00:33 +00:00
parent b3d304df93
commit 6aee2336ca
2 changed files with 46 additions and 36 deletions

View File

@@ -389,13 +389,18 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip,
perm = os_getperm(fname); perm = os_getperm(fname);
// On Unix it is possible to read a directory, so we have to // On Unix it is possible to read a directory, so we have to
// check for it before os_open(). // check for it before os_open().
#ifdef OPEN_CHR_FILES
# define IS_CHR_DEV(perm, fname) S_ISCHR(perm) && is_dev_fd_file(fname)
#else
# define IS_CHR_DEV(perm, fname) false
#endif
if (perm >= 0 && !S_ISREG(perm) // not a regular file ... if (perm >= 0 && !S_ISREG(perm) // not a regular file ...
&& !S_ISFIFO(perm) // ... or fifo && !S_ISFIFO(perm) // ... or fifo
&& !S_ISSOCK(perm) // ... or socket && !S_ISSOCK(perm) // ... or socket
#ifdef OPEN_CHR_FILES && !(IS_CHR_DEV(perm, fname))
&& !(S_ISCHR(perm) && is_dev_fd_file(fname))
// ... or a character special file named /dev/fd/<n> // ... or a character special file named /dev/fd/<n>
#endif
) { ) {
if (S_ISDIR(perm)) { if (S_ISDIR(perm)) {
if (!silent) { if (!silent) {
@@ -513,15 +518,18 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip,
} }
return OK; // a new file is not an error return OK; // a new file is not an error
} }
filemess(curbuf, sfname, ((fd == UV_EFBIG) ? _("[File too big]") :
#if defined(UNIX) && defined(EOVERFLOW) #if defined(UNIX) && defined(EOVERFLOW)
filemess(curbuf, sfname, ((fd == UV_EFBIG) ? _("[File too big]") :
// libuv only returns -errno // libuv only returns -errno
// in Unix and in Windows // in Unix and in Windows
// open() does not set // open() does not set
// EOVERFLOW // EOVERFLOW
(fd == -EOVERFLOW) ? _("[File too big]") : (fd == -EOVERFLOW) ? _("[File too big]") :
#endif
_("[Permission Denied]")), 0); _("[Permission Denied]")), 0);
#else
filemess(curbuf, sfname, ((fd == UV_EFBIG) ? _("[File too big]") :
_("[Permission Denied]")), 0);
#endif
curbuf->b_p_ro = true; // must use "w!" now curbuf->b_p_ro = true; // must use "w!" now
return FAIL; return FAIL;
@@ -2865,13 +2873,12 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en
msg_scroll = true; // don't overwrite previous file message msg_scroll = true; // don't overwrite previous file message
} }
if (!filtering) { if (!filtering) {
filemess(buf, // show that we are busy
#ifndef UNIX #ifndef UNIX
sfname, filemess(buf, sfname, "", 0);
#else #else
fname, filemess(buf, fname, "", 0);
#endif #endif
"", 0); // show that we are busy
} }
msg_scroll = false; // always overwrite the file message now msg_scroll = false; // always overwrite the file message now
@@ -3114,29 +3121,32 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en
&& !os_fileinfo_id_equal(&file_info, &file_info_old))) { && !os_fileinfo_id_equal(&file_info, &file_info_old))) {
err = set_err(_("E166: Can't open linked file for writing")); err = set_err(_("E166: Can't open linked file for writing"));
} else { } else {
#endif err = set_err_arg(_("E212: Can't open file for writing: %s"), fd);
if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL && perm >= 0) {
// we write to the file, thus it should be marked
// writable after all
if (!(perm & 0200)) {
made_writable = true;
}
perm |= 0200;
if (file_info_old.stat.st_uid != getuid()
|| file_info_old.stat.st_gid != getgid()) {
perm &= 0777;
}
if (!append) { // don't remove when appending
os_remove(wfname);
}
continue;
}
}
#else
err = set_err_arg(_("E212: Can't open file for writing: %s"), fd); err = set_err_arg(_("E212: Can't open file for writing: %s"), fd);
if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL && perm >= 0) {
&& perm >= 0) {
#ifdef UNIX
// we write to the file, thus it should be marked
// writable after all
if (!(perm & 0200)) {
made_writable = true;
}
perm |= 0200;
if (file_info_old.stat.st_uid != getuid()
|| file_info_old.stat.st_gid != getgid()) {
perm &= 0777;
}
#endif
if (!append) { // don't remove when appending if (!append) { // don't remove when appending
os_remove(wfname); os_remove(wfname);
} }
continue; continue;
} }
#ifdef UNIX
}
#endif #endif
} }
@@ -3774,14 +3784,15 @@ static int check_mtime(buf_T *buf, FileInfo *file_info)
static bool time_differs(const FileInfo *file_info, long mtime, long mtime_ns) FUNC_ATTR_CONST static bool time_differs(const FileInfo *file_info, long mtime, long mtime_ns) FUNC_ATTR_CONST
{ {
return file_info->stat.st_mtim.tv_nsec != mtime_ns
#if defined(__linux__) || defined(MSWIN) #if defined(__linux__) || defined(MSWIN)
return file_info->stat.st_mtim.tv_nsec != mtime_ns
// On a FAT filesystem, esp. under Linux, there are only 5 bits to store // On a FAT filesystem, esp. under Linux, there are only 5 bits to store
// the seconds. Since the roundoff is done when flushing the inode, the // the seconds. Since the roundoff is done when flushing the inode, the
// time may change unexpectedly by one second!!! // time may change unexpectedly by one second!!!
|| file_info->stat.st_mtim.tv_sec - mtime > 1 || file_info->stat.st_mtim.tv_sec - mtime > 1
|| mtime - file_info->stat.st_mtim.tv_sec > 1; || mtime - file_info->stat.st_mtim.tv_sec > 1;
#else #else
return file_info->stat.st_mtim.tv_nsec != mtime_ns
|| file_info->stat.st_mtim.tv_sec != mtime; || file_info->stat.st_mtim.tv_sec != mtime;
#endif #endif
} }
@@ -5600,11 +5611,7 @@ char *file_pat_to_reg_pat(const char *pat, const char *pat_end, char *allow_dirs
// regexp. // regexp.
// An escaped { must be unescaped since we use magic not // An escaped { must be unescaped since we use magic not
// verymagic. Use "\\\{n,m\}"" to get "\{n,m}". // verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
if (*++p == '?' if (*++p == '?' && (!BACKSLASH_IN_FILENAME_BOOL || no_bslash)) {
#ifdef BACKSLASH_IN_FILENAME
&& no_bslash
#endif
) {
reg_pat[i++] = '?'; reg_pat[i++] = '?';
} else if (*p == ',' || *p == '%' || *p == '#' } else if (*p == ',' || *p == '%' || *p == '#'
|| ascii_isspace(*p) || *p == '{' || *p == '}') { || ascii_isspace(*p) || *p == '{' || *p == '}') {
@@ -5615,10 +5622,7 @@ char *file_pat_to_reg_pat(const char *pat, const char *pat_end, char *allow_dirs
p += 2; p += 2;
} else { } else {
if (allow_dirs != NULL && vim_ispathsep(*p) if (allow_dirs != NULL && vim_ispathsep(*p)
#ifdef BACKSLASH_IN_FILENAME && (!BACKSLASH_IN_FILENAME_BOOL || (!no_bslash || *p != '\\'))) {
&& (!no_bslash || *p != '\\')
#endif
) {
*allow_dirs = true; *allow_dirs = true;
} }
reg_pat[i++] = '\\'; reg_pat[i++] = '\\';

View File

@@ -13,6 +13,12 @@
# include "nvim/os/unix_defs.h" # include "nvim/os/unix_defs.h"
#endif #endif
#ifdef BACKSLASH_IN_FILENAME
# define BACKSLASH_IN_FILENAME_BOOL true
#else
# define BACKSLASH_IN_FILENAME_BOOL false
#endif
#if !defined(NAME_MAX) && defined(_XOPEN_NAME_MAX) #if !defined(NAME_MAX) && defined(_XOPEN_NAME_MAX)
# define NAME_MAX _XOPEN_NAME_MAX # define NAME_MAX _XOPEN_NAME_MAX
#endif #endif