mirror of
https://github.com/neovim/neovim.git
synced 2025-09-08 12:28:18 +00:00
fileinfo: implement os_fileinfo_size
this replaces os_get_file_size and file_info.stat.st_size
This commit is contained in:
@@ -947,9 +947,10 @@ void ex_diffpatch(exarg_T *eap)
|
|||||||
os_remove((char *)buf);
|
os_remove((char *)buf);
|
||||||
|
|
||||||
// Only continue if the output file was created.
|
// Only continue if the output file was created.
|
||||||
off_t file_size;
|
FileInfo file_info;
|
||||||
bool file_size_success = os_get_file_size((char *)tmp_new, &file_size);
|
bool info_ok = os_get_file_info((char *)tmp_new, &file_info);
|
||||||
if (!file_size_success || file_size == 0) {
|
off_t filesize = os_fileinfo_size(&file_info);
|
||||||
|
if (!info_ok || filesize == 0) {
|
||||||
EMSG(_("E816: Cannot read patch output"));
|
EMSG(_("E816: Cannot read patch output"));
|
||||||
} else {
|
} else {
|
||||||
if (curbuf->b_fname != NULL) {
|
if (curbuf->b_fname != NULL) {
|
||||||
|
@@ -9165,15 +9165,16 @@ static void f_getfsize(typval_T *argvars, typval_T *rettv)
|
|||||||
|
|
||||||
rettv->v_type = VAR_NUMBER;
|
rettv->v_type = VAR_NUMBER;
|
||||||
|
|
||||||
off_t file_size;
|
FileInfo file_info;
|
||||||
if (os_get_file_size(fname, &file_size)) {
|
if (os_get_file_info(fname, &file_info)) {
|
||||||
|
off_t filesize = os_fileinfo_size(&file_info);
|
||||||
if (os_isdir((char_u *)fname))
|
if (os_isdir((char_u *)fname))
|
||||||
rettv->vval.v_number = 0;
|
rettv->vval.v_number = 0;
|
||||||
else {
|
else {
|
||||||
rettv->vval.v_number = (varnumber_T)file_size;
|
rettv->vval.v_number = (varnumber_T)filesize;
|
||||||
|
|
||||||
/* non-perfect check for overflow */
|
/* non-perfect check for overflow */
|
||||||
if ((off_t)rettv->vval.v_number != file_size) {
|
if ((off_t)rettv->vval.v_number != filesize) {
|
||||||
rettv->vval.v_number = -2;
|
rettv->vval.v_number = -2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -5127,9 +5127,10 @@ void buf_reload(buf_T *buf, int orig_mode)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void buf_store_file_info(buf_T *buf, FileInfo *file_info)
|
void buf_store_file_info(buf_T *buf, FileInfo *file_info)
|
||||||
|
FUNC_ATTR_NONNULL_ALL
|
||||||
{
|
{
|
||||||
buf->b_mtime = (long)file_info->stat.st_mtim.tv_sec;
|
buf->b_mtime = (long)file_info->stat.st_mtim.tv_sec;
|
||||||
buf->b_orig_size = file_info->stat.st_size;
|
buf->b_orig_size = os_fileinfo_size(file_info);
|
||||||
buf->b_orig_mode = (int)file_info->stat.st_mode;
|
buf->b_orig_mode = (int)file_info->stat.st_mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1632,7 +1632,7 @@ void ml_sync_all(int check_file, int check_char)
|
|||||||
FileInfo file_info;
|
FileInfo file_info;
|
||||||
if (!os_get_file_info((char *)buf->b_ffname, &file_info)
|
if (!os_get_file_info((char *)buf->b_ffname, &file_info)
|
||||||
|| file_info.stat.st_mtim.tv_sec != buf->b_mtime_read
|
|| file_info.stat.st_mtim.tv_sec != buf->b_mtime_read
|
||||||
|| (off_t)file_info.stat.st_size != buf->b_orig_size) {
|
|| os_fileinfo_size(&file_info) != 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 */
|
||||||
|
@@ -258,20 +258,6 @@ int os_file_is_writable(const char *name)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the size of a file in bytes.
|
|
||||||
///
|
|
||||||
/// @param[out] size pointer to an off_t to put the size into.
|
|
||||||
/// @return `true` for success, `false` for failure.
|
|
||||||
bool os_get_file_size(const char *name, off_t *size)
|
|
||||||
{
|
|
||||||
uv_stat_t statbuf;
|
|
||||||
if (os_stat(name, &statbuf)) {
|
|
||||||
*size = statbuf.st_size;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Rename a file or directory.
|
/// Rename a file or directory.
|
||||||
///
|
///
|
||||||
/// @return `OK` for success, `FAIL` for failure.
|
/// @return `OK` for success, `FAIL` for failure.
|
||||||
@@ -408,6 +394,15 @@ uint64_t os_file_info_get_inode(const FileInfo *file_info)
|
|||||||
return file_info->stat.st_ino;
|
return file_info->stat.st_ino;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the size of a file from a `FileInfo`.
|
||||||
|
///
|
||||||
|
/// @return filesize in bytes.
|
||||||
|
off_t os_fileinfo_size(const FileInfo *file_info)
|
||||||
|
FUNC_ATTR_NONNULL_ALL
|
||||||
|
{
|
||||||
|
return file_info->stat.st_size;
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the `FileID` for a given path
|
/// Get the `FileID` for a given path
|
||||||
///
|
///
|
||||||
/// @param path Path to the file.
|
/// @param path Path to the file.
|
||||||
|
@@ -597,6 +597,20 @@ describe('fs function', function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('os_fileinfo_size', function()
|
||||||
|
it('returns the correct size of a file', function()
|
||||||
|
local path = 'unit-test-directory/test.file'
|
||||||
|
local file = io.open(path, 'w')
|
||||||
|
file:write('some bytes to get filesize != 0')
|
||||||
|
file:flush()
|
||||||
|
file:close()
|
||||||
|
local size = lfs.attributes(path, 'size')
|
||||||
|
local file_info = file_info_new()
|
||||||
|
assert.is_true(fs.os_get_file_info(path, file_info))
|
||||||
|
eq(size, fs.os_fileinfo_size(file_info))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
describe('os_get_file_id', function()
|
describe('os_get_file_id', function()
|
||||||
it('returns false if given an non-existing file', function()
|
it('returns false if given an non-existing file', function()
|
||||||
local file_id = file_id_new()
|
local file_id = file_id_new()
|
||||||
|
Reference in New Issue
Block a user