mirror of
https://github.com/neovim/neovim.git
synced 2025-09-29 14:38:32 +00:00
FileID: refactor buffer.c to use FileID
This commit is contained in:
@@ -70,9 +70,6 @@
|
|||||||
#include "nvim/window.h"
|
#include "nvim/window.h"
|
||||||
#include "nvim/os/os.h"
|
#include "nvim/os/os.h"
|
||||||
|
|
||||||
// Todo(stefan991): remove this macro
|
|
||||||
#define INVALID_DEVICE_ID UINT64_MAX
|
|
||||||
|
|
||||||
#define HAVE_BUFLIST_MATCH
|
#define HAVE_BUFLIST_MATCH
|
||||||
|
|
||||||
#ifdef INCLUDE_GENERATED_DECLARATIONS
|
#ifdef INCLUDE_GENERATED_DECLARATIONS
|
||||||
@@ -1294,12 +1291,12 @@ buflist_new (
|
|||||||
*/
|
*/
|
||||||
/* We can use inode numbers when the file exists. Works better
|
/* We can use inode numbers when the file exists. Works better
|
||||||
* for hard links. */
|
* for hard links. */
|
||||||
FileInfo file_info;
|
FileID file_id;
|
||||||
bool file_info_valid = (sfname != NULL &&
|
bool file_id_valid = (sfname != NULL &&
|
||||||
os_get_file_info((char *)sfname, &file_info));
|
os_get_file_id((char *)sfname, &file_id));
|
||||||
if (ffname != NULL && !(flags & BLN_DUMMY)
|
if (ffname != NULL && !(flags & BLN_DUMMY)
|
||||||
&& (buf = buflist_findname_file_info(ffname, &file_info,
|
&& (buf = buflist_findname_file_id(ffname, &file_id,
|
||||||
file_info_valid)) != NULL) {
|
file_id_valid)) != NULL) {
|
||||||
free(ffname);
|
free(ffname);
|
||||||
if (lnum != 0)
|
if (lnum != 0)
|
||||||
buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
|
buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
|
||||||
@@ -1426,12 +1423,11 @@ buflist_new (
|
|||||||
hash_init(&buf->b_s.b_keywtab_ic);
|
hash_init(&buf->b_s.b_keywtab_ic);
|
||||||
|
|
||||||
buf->b_fname = buf->b_sfname;
|
buf->b_fname = buf->b_sfname;
|
||||||
if (!file_info_valid) {
|
if (!file_id_valid) {
|
||||||
buf->b_dev_valid = FALSE;
|
buf->file_id_valid = false;
|
||||||
} else {
|
} else {
|
||||||
buf->b_dev_valid = TRUE;
|
buf->file_id_valid = true;
|
||||||
buf->b_dev = file_info.stat.st_dev;
|
buf->file_id = file_id;
|
||||||
buf->b_ino = file_info.stat.st_ino;
|
|
||||||
}
|
}
|
||||||
buf->b_u_synced = TRUE;
|
buf->b_u_synced = TRUE;
|
||||||
buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
|
buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
|
||||||
@@ -1652,24 +1648,24 @@ buf_T *buflist_findname_exp(char_u *fname)
|
|||||||
*/
|
*/
|
||||||
buf_T *buflist_findname(char_u *ffname)
|
buf_T *buflist_findname(char_u *ffname)
|
||||||
{
|
{
|
||||||
FileInfo file_info;
|
FileID file_id;
|
||||||
bool file_info_valid = os_get_file_info((char *)ffname, &file_info);
|
bool file_id_valid = os_get_file_id((char *)ffname, &file_id);
|
||||||
return buflist_findname_file_info(ffname, &file_info, file_info_valid);
|
return buflist_findname_file_id(ffname, &file_id, file_id_valid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Same as buflist_findname(), but pass the FileInfo structure to avoid
|
* Same as buflist_findname(), but pass the FileID structure to avoid
|
||||||
* getting it twice for the same file.
|
* getting it twice for the same file.
|
||||||
* Returns NULL if not found.
|
* Returns NULL if not found.
|
||||||
*/
|
*/
|
||||||
static buf_T *buflist_findname_file_info(char_u *ffname, FileInfo *file_info,
|
static buf_T *buflist_findname_file_id(char_u *ffname, FileID *file_id,
|
||||||
bool file_info_valid)
|
bool file_id_valid)
|
||||||
{
|
{
|
||||||
buf_T *buf;
|
buf_T *buf;
|
||||||
|
|
||||||
for (buf = firstbuf; buf != NULL; buf = buf->b_next) {
|
for (buf = firstbuf; buf != NULL; buf = buf->b_next) {
|
||||||
if ((buf->b_flags & BF_DUMMY) == 0
|
if ((buf->b_flags & BF_DUMMY) == 0
|
||||||
&& !otherfile_buf(buf, ffname, file_info, file_info_valid)) {
|
&& !otherfile_buf(buf, ffname, file_id, file_id_valid)) {
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2185,8 +2181,8 @@ setfname (
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
buf_T *obuf = NULL;
|
buf_T *obuf = NULL;
|
||||||
FileInfo file_info;
|
FileID file_id;
|
||||||
bool file_info_valid = false;
|
bool file_id_valid = false;
|
||||||
|
|
||||||
if (ffname == NULL || *ffname == NUL) {
|
if (ffname == NULL || *ffname == NUL) {
|
||||||
/* Removing the name. */
|
/* Removing the name. */
|
||||||
@@ -2204,9 +2200,9 @@ setfname (
|
|||||||
* - if the buffer is loaded, fail
|
* - if the buffer is loaded, fail
|
||||||
* - if the buffer is not loaded, delete it from the list
|
* - if the buffer is not loaded, delete it from the list
|
||||||
*/
|
*/
|
||||||
file_info_valid = os_get_file_info((char *)ffname, &file_info);
|
file_id_valid = os_get_file_id((char *)ffname, &file_id);
|
||||||
if (!(buf->b_flags & BF_DUMMY)) {
|
if (!(buf->b_flags & BF_DUMMY)) {
|
||||||
obuf = buflist_findname_file_info(ffname, &file_info, file_info_valid);
|
obuf = buflist_findname_file_id(ffname, &file_id, file_id_valid);
|
||||||
}
|
}
|
||||||
if (obuf != NULL && obuf != buf) {
|
if (obuf != NULL && obuf != buf) {
|
||||||
if (obuf->b_ml.ml_mfp != NULL) { /* it's loaded, fail */
|
if (obuf->b_ml.ml_mfp != NULL) { /* it's loaded, fail */
|
||||||
@@ -2232,12 +2228,11 @@ setfname (
|
|||||||
buf->b_sfname = sfname;
|
buf->b_sfname = sfname;
|
||||||
}
|
}
|
||||||
buf->b_fname = buf->b_sfname;
|
buf->b_fname = buf->b_sfname;
|
||||||
if (!file_info_valid) {
|
if (!file_id_valid) {
|
||||||
buf->b_dev_valid = FALSE;
|
buf->file_id_valid = false;
|
||||||
} else {
|
} else {
|
||||||
buf->b_dev_valid = TRUE;
|
buf->file_id_valid = true;
|
||||||
buf->b_dev = file_info.stat.st_dev;
|
buf->file_id = file_id;
|
||||||
buf->b_ino = file_info.stat.st_ino;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buf_name_changed(buf);
|
buf_name_changed(buf);
|
||||||
@@ -2375,7 +2370,7 @@ int otherfile(char_u *ffname)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int otherfile_buf(buf_T *buf, char_u *ffname,
|
static int otherfile_buf(buf_T *buf, char_u *ffname,
|
||||||
FileInfo *file_info_p, bool file_info_valid)
|
FileID *file_id_p, bool file_id_valid)
|
||||||
{
|
{
|
||||||
/* no name is different */
|
/* no name is different */
|
||||||
if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL) {
|
if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL) {
|
||||||
@@ -2384,20 +2379,16 @@ static int otherfile_buf(buf_T *buf, char_u *ffname,
|
|||||||
if (fnamecmp(ffname, buf->b_ffname) == 0) {
|
if (fnamecmp(ffname, buf->b_ffname) == 0) {
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
if (file_info_p != NULL && !file_info_valid) {
|
|
||||||
// file_id not valid, assume files are different.
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
{
|
{
|
||||||
FileInfo file_info;
|
FileID file_id;
|
||||||
|
|
||||||
/* If no struct stat given, get it now */
|
/* If no struct stat given, get it now */
|
||||||
if (file_info_p == NULL) {
|
if (file_id_p == NULL) {
|
||||||
if (!os_get_file_info((char *)ffname, &file_info)) {
|
file_id_p = &file_id;
|
||||||
// could not get file_id to compare, assume files are different.
|
file_id_valid = os_get_file_id((char *)ffname, file_id_p);
|
||||||
return TRUE;
|
}
|
||||||
}
|
if (!file_id_valid) {
|
||||||
file_info_p = &file_info;
|
// file_id not valid, assume files are different.
|
||||||
|
return TRUE;
|
||||||
}
|
}
|
||||||
/* Use dev/ino to check if the files are the same, even when the names
|
/* Use dev/ino to check if the files are the same, even when the names
|
||||||
* are different (possible with links). Still need to compare the
|
* are different (possible with links). Still need to compare the
|
||||||
@@ -2408,40 +2399,34 @@ static int otherfile_buf(buf_T *buf, char_u *ffname,
|
|||||||
* dev/ino again when they appear to match, but not when they appear
|
* dev/ino again when they appear to match, but not when they appear
|
||||||
* to be different: Could skip a buffer when it's actually the same
|
* to be different: Could skip a buffer when it's actually the same
|
||||||
* file. */
|
* file. */
|
||||||
if (buf_same_ino(buf, file_info_p)) {
|
if (buf_same_file_id(buf, file_id_p)) {
|
||||||
buf_setino(buf);
|
buf_set_file_id(buf);
|
||||||
if (buf_same_ino(buf, file_info_p))
|
if (buf_same_file_id(buf, file_id_p))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Set file_id for a buffer.
|
||||||
* Set inode and device number for a buffer.
|
// Must always be called when b_fname is changed!
|
||||||
* Must always be called when b_fname is changed!.
|
void buf_set_file_id(buf_T *buf)
|
||||||
*/
|
|
||||||
void buf_setino(buf_T *buf)
|
|
||||||
{
|
{
|
||||||
FileInfo file_info;
|
FileID file_id;
|
||||||
if (buf->b_fname != NULL
|
if (buf->b_fname != NULL
|
||||||
&& os_get_file_info((char *)buf->b_fname, &file_info)) {
|
&& os_get_file_id((char *)buf->b_fname, &file_id)) {
|
||||||
buf->b_dev_valid = TRUE;
|
buf->file_id_valid = true;
|
||||||
buf->b_dev = file_info.stat.st_dev;
|
buf->file_id = file_id;
|
||||||
buf->b_ino = file_info.stat.st_ino;
|
|
||||||
} else {
|
} else {
|
||||||
buf->b_dev_valid = FALSE;
|
buf->file_id_valid = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// return TRUE if file_id in buffer "buf" matches with "file_id".
|
||||||
* Return TRUE if dev/ino in buffer "buf" matches with "stp".
|
static bool buf_same_file_id(buf_T *buf, FileID *file_id)
|
||||||
*/
|
|
||||||
static int buf_same_ino(buf_T *buf, FileInfo *file_info)
|
|
||||||
{
|
{
|
||||||
return buf->b_dev_valid
|
return buf->file_id_valid
|
||||||
&& file_info->stat.st_dev == buf->b_dev
|
&& os_file_id_equal(&(buf->file_id), file_id);
|
||||||
&& file_info->stat.st_ino == buf->b_ino;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@@ -88,6 +88,9 @@ typedef struct memfile memfile_T;
|
|||||||
// for signlist_T
|
// for signlist_T
|
||||||
#include "nvim/sign_defs.h"
|
#include "nvim/sign_defs.h"
|
||||||
|
|
||||||
|
// for FileID
|
||||||
|
#include "nvim/os/fs_defs.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The taggy struct is used to store the information about a :tag command.
|
* The taggy struct is used to store the information about a :tag command.
|
||||||
*/
|
*/
|
||||||
@@ -471,9 +474,8 @@ struct file_buffer {
|
|||||||
char_u *b_sfname; /* short file name */
|
char_u *b_sfname; /* short file name */
|
||||||
char_u *b_fname; /* current file name */
|
char_u *b_fname; /* current file name */
|
||||||
|
|
||||||
int b_dev_valid; /* TRUE when b_dev has a valid number */
|
bool file_id_valid;
|
||||||
uint64_t b_dev; /* device number */
|
FileID file_id;
|
||||||
uint64_t b_ino; /* inode number */
|
|
||||||
|
|
||||||
int b_fnum; /* buffer number for this file. */
|
int b_fnum; /* buffer number for this file. */
|
||||||
|
|
||||||
|
@@ -3433,10 +3433,11 @@ restore_backup:
|
|||||||
(void)os_setperm(wfname, perm);
|
(void)os_setperm(wfname, perm);
|
||||||
}
|
}
|
||||||
# endif
|
# endif
|
||||||
buf_setino(buf);
|
buf_set_file_id(buf);
|
||||||
} else if (!buf->b_dev_valid)
|
} else if (!buf->file_id_valid) {
|
||||||
/* Set the inode when creating a new file. */
|
// Set the file_id when creating a new file.
|
||||||
buf_setino(buf);
|
buf_set_file_id(buf);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (close(fd) != 0) {
|
if (close(fd) != 0) {
|
||||||
|
Reference in New Issue
Block a user