aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/buffer.c
diff options
context:
space:
mode:
authorStefan Hoffmann <stefan991@gmail.com>2014-05-27 19:23:00 +0200
committerStefan Hoffmann <stefan991@gmail.com>2014-06-27 13:59:28 +0200
commita294a0e1c598179409490991de071bd4957ae2e7 (patch)
tree9c51a88c89a5f99124e10d6334a7b73e782fa40f /src/nvim/buffer.c
parentd8ec5ef88d77e4074c7376fda21276c2fc724f5d (diff)
downloadrneovim-a294a0e1c598179409490991de071bd4957ae2e7.tar.gz
rneovim-a294a0e1c598179409490991de071bd4957ae2e7.tar.bz2
rneovim-a294a0e1c598179409490991de071bd4957ae2e7.zip
FileID: refactor buffer.c to use `FileID`
Diffstat (limited to 'src/nvim/buffer.c')
-rw-r--r--src/nvim/buffer.c111
1 files changed, 48 insertions, 63 deletions
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c
index 21e54bda93..2152502c95 100644
--- a/src/nvim/buffer.c
+++ b/src/nvim/buffer.c
@@ -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);
}
/*