aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2023-01-31 12:00:33 +0000
committerLewis Russell <lewis6991@gmail.com>2023-02-01 11:14:21 +0000
commit6aee2336ca75301bd4db6b99c2392f63f8304335 (patch)
tree7c0204e9a70001b008284448795b890fcf6cf4c6 /src
parentb3d304df93347ef3f585ae91ae9ff6f5f28651af (diff)
downloadrneovim-6aee2336ca75301bd4db6b99c2392f63f8304335.tar.gz
rneovim-6aee2336ca75301bd4db6b99c2392f63f8304335.tar.bz2
rneovim-6aee2336ca75301bd4db6b99c2392f63f8304335.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/nvim/fileio.c76
-rw-r--r--src/nvim/os/os_defs.h6
2 files changed, 46 insertions, 36 deletions
diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c
index 77b8cc833f..716d7af149 100644
--- a/src/nvim/fileio.c
+++ b/src/nvim/fileio.c
@@ -389,13 +389,18 @@ int readfile(char *fname, char *sfname, linenr_T from, linenr_T lines_to_skip,
perm = os_getperm(fname);
// On Unix it is possible to read a directory, so we have to
// 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 ...
&& !S_ISFIFO(perm) // ... or fifo
&& !S_ISSOCK(perm) // ... or socket
-#ifdef OPEN_CHR_FILES
- && !(S_ISCHR(perm) && is_dev_fd_file(fname))
+ && !(IS_CHR_DEV(perm, fname))
// ... or a character special file named /dev/fd/<n>
-#endif
) {
if (S_ISDIR(perm)) {
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
}
- filemess(curbuf, sfname, ((fd == UV_EFBIG) ? _("[File too big]") :
#if defined(UNIX) && defined(EOVERFLOW)
+ filemess(curbuf, sfname, ((fd == UV_EFBIG) ? _("[File too big]") :
// libuv only returns -errno
// in Unix and in Windows
// open() does not set
// EOVERFLOW
(fd == -EOVERFLOW) ? _("[File too big]") :
-#endif
_("[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
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
}
if (!filtering) {
- filemess(buf,
+ // show that we are busy
#ifndef UNIX
- sfname,
+ filemess(buf, sfname, "", 0);
#else
- fname,
+ filemess(buf, fname, "", 0);
#endif
- "", 0); // show that we are busy
}
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))) {
err = set_err(_("E166: Can't open linked file for writing"));
} 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) {
-#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;
+ 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;
}
-#endif
+ }
+#else
+ err = set_err_arg(_("E212: Can't open file for writing: %s"), fd);
+ if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL && perm >= 0) {
if (!append) { // don't remove when appending
os_remove(wfname);
}
continue;
}
-#ifdef UNIX
- }
#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
{
- return file_info->stat.st_mtim.tv_nsec != mtime_ns
#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
// the seconds. Since the roundoff is done when flushing the inode, the
// time may change unexpectedly by one second!!!
|| file_info->stat.st_mtim.tv_sec - mtime > 1
|| mtime - file_info->stat.st_mtim.tv_sec > 1;
#else
+ return file_info->stat.st_mtim.tv_nsec != mtime_ns
|| file_info->stat.st_mtim.tv_sec != mtime;
#endif
}
@@ -5600,11 +5611,7 @@ char *file_pat_to_reg_pat(const char *pat, const char *pat_end, char *allow_dirs
// regexp.
// An escaped { must be unescaped since we use magic not
// verymagic. Use "\\\{n,m\}"" to get "\{n,m}".
- if (*++p == '?'
-#ifdef BACKSLASH_IN_FILENAME
- && no_bslash
-#endif
- ) {
+ if (*++p == '?' && (!BACKSLASH_IN_FILENAME_BOOL || no_bslash)) {
reg_pat[i++] = '?';
} else if (*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;
} else {
if (allow_dirs != NULL && vim_ispathsep(*p)
-#ifdef BACKSLASH_IN_FILENAME
- && (!no_bslash || *p != '\\')
-#endif
- ) {
+ && (!BACKSLASH_IN_FILENAME_BOOL || (!no_bslash || *p != '\\'))) {
*allow_dirs = true;
}
reg_pat[i++] = '\\';
diff --git a/src/nvim/os/os_defs.h b/src/nvim/os/os_defs.h
index a30e16eeba..f86c0d3483 100644
--- a/src/nvim/os/os_defs.h
+++ b/src/nvim/os/os_defs.h
@@ -13,6 +13,12 @@
# include "nvim/os/unix_defs.h"
#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)
# define NAME_MAX _XOPEN_NAME_MAX
#endif