FileID: refactor buffer.c to use FileID

This commit is contained in:
Stefan Hoffmann 2014-05-27 19:23:00 +02:00
parent d8ec5ef88d
commit a294a0e1c5
3 changed files with 58 additions and 70 deletions

View File

@ -70,9 +70,6 @@
#include "nvim/window.h"
#include "nvim/os/os.h"
// Todo(stefan991): remove this macro
#define INVALID_DEVICE_ID UINT64_MAX
#define HAVE_BUFLIST_MATCH
#ifdef INCLUDE_GENERATED_DECLARATIONS
@ -1294,12 +1291,12 @@ buflist_new (
*/
/* We can use inode numbers when the file exists. Works better
* for hard links. */
FileInfo file_info;
bool file_info_valid = (sfname != NULL &&
os_get_file_info((char *)sfname, &file_info));
FileID file_id;
bool file_id_valid = (sfname != NULL &&
os_get_file_id((char *)sfname, &file_id));
if (ffname != NULL && !(flags & BLN_DUMMY)
&& (buf = buflist_findname_file_info(ffname, &file_info,
file_info_valid)) != NULL) {
&& (buf = buflist_findname_file_id(ffname, &file_id,
file_id_valid)) != NULL) {
free(ffname);
if (lnum != 0)
buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
@ -1426,12 +1423,11 @@ buflist_new (
hash_init(&buf->b_s.b_keywtab_ic);
buf->b_fname = buf->b_sfname;
if (!file_info_valid) {
buf->b_dev_valid = FALSE;
if (!file_id_valid) {
buf->file_id_valid = false;
} else {
buf->b_dev_valid = TRUE;
buf->b_dev = file_info.stat.st_dev;
buf->b_ino = file_info.stat.st_ino;
buf->file_id_valid = true;
buf->file_id = file_id;
}
buf->b_u_synced = TRUE;
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)
{
FileInfo file_info;
bool file_info_valid = os_get_file_info((char *)ffname, &file_info);
return buflist_findname_file_info(ffname, &file_info, file_info_valid);
FileID file_id;
bool file_id_valid = os_get_file_id((char *)ffname, &file_id);
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.
* Returns NULL if not found.
*/
static buf_T *buflist_findname_file_info(char_u *ffname, FileInfo *file_info,
bool file_info_valid)
static buf_T *buflist_findname_file_id(char_u *ffname, FileID *file_id,
bool file_id_valid)
{
buf_T *buf;
for (buf = firstbuf; buf != NULL; buf = buf->b_next) {
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;
}
}
@ -2185,8 +2181,8 @@ setfname (
)
{
buf_T *obuf = NULL;
FileInfo file_info;
bool file_info_valid = false;
FileID file_id;
bool file_id_valid = false;
if (ffname == NULL || *ffname == NUL) {
/* Removing the name. */
@ -2204,9 +2200,9 @@ setfname (
* - if the buffer is loaded, fail
* - 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)) {
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->b_ml.ml_mfp != NULL) { /* it's loaded, fail */
@ -2232,12 +2228,11 @@ setfname (
buf->b_sfname = sfname;
}
buf->b_fname = buf->b_sfname;
if (!file_info_valid) {
buf->b_dev_valid = FALSE;
if (!file_id_valid) {
buf->file_id_valid = false;
} else {
buf->b_dev_valid = TRUE;
buf->b_dev = file_info.stat.st_dev;
buf->b_ino = file_info.stat.st_ino;
buf->file_id_valid = true;
buf->file_id = file_id;
}
buf_name_changed(buf);
@ -2375,7 +2370,7 @@ int otherfile(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 */
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) {
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 (file_info_p == NULL) {
if (!os_get_file_info((char *)ffname, &file_info)) {
// could not get file_id to compare, assume files are different.
return TRUE;
}
file_info_p = &file_info;
if (file_id_p == NULL) {
file_id_p = &file_id;
file_id_valid = os_get_file_id((char *)ffname, file_id_p);
}
if (!file_id_valid) {
// 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
* 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
* to be different: Could skip a buffer when it's actually the same
* file. */
if (buf_same_ino(buf, file_info_p)) {
buf_setino(buf);
if (buf_same_ino(buf, file_info_p))
if (buf_same_file_id(buf, file_id_p)) {
buf_set_file_id(buf);
if (buf_same_file_id(buf, file_id_p))
return FALSE;
}
}
return TRUE;
}
/*
* Set inode and device number for a buffer.
* Must always be called when b_fname is changed!.
*/
void buf_setino(buf_T *buf)
// Set file_id for a buffer.
// Must always be called when b_fname is changed!
void buf_set_file_id(buf_T *buf)
{
FileInfo file_info;
FileID file_id;
if (buf->b_fname != NULL
&& os_get_file_info((char *)buf->b_fname, &file_info)) {
buf->b_dev_valid = TRUE;
buf->b_dev = file_info.stat.st_dev;
buf->b_ino = file_info.stat.st_ino;
&& os_get_file_id((char *)buf->b_fname, &file_id)) {
buf->file_id_valid = true;
buf->file_id = file_id;
} else {
buf->b_dev_valid = FALSE;
buf->file_id_valid = false;
}
}
/*
* Return TRUE if dev/ino in buffer "buf" matches with "stp".
*/
static int buf_same_ino(buf_T *buf, FileInfo *file_info)
// return TRUE if file_id in buffer "buf" matches with "file_id".
static bool buf_same_file_id(buf_T *buf, FileID *file_id)
{
return buf->b_dev_valid
&& file_info->stat.st_dev == buf->b_dev
&& file_info->stat.st_ino == buf->b_ino;
return buf->file_id_valid
&& os_file_id_equal(&(buf->file_id), file_id);
}
/*

View File

@ -88,6 +88,9 @@ typedef struct memfile memfile_T;
// for signlist_T
#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.
*/
@ -471,9 +474,8 @@ struct file_buffer {
char_u *b_sfname; /* short file name */
char_u *b_fname; /* current file name */
int b_dev_valid; /* TRUE when b_dev has a valid number */
uint64_t b_dev; /* device number */
uint64_t b_ino; /* inode number */
bool file_id_valid;
FileID file_id;
int b_fnum; /* buffer number for this file. */

View File

@ -3433,10 +3433,11 @@ restore_backup:
(void)os_setperm(wfname, perm);
}
# endif
buf_setino(buf);
} else if (!buf->b_dev_valid)
/* Set the inode when creating a new file. */
buf_setino(buf);
buf_set_file_id(buf);
} else if (!buf->file_id_valid) {
// Set the file_id when creating a new file.
buf_set_file_id(buf);
}
#endif
if (close(fd) != 0) {