summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2012-10-31 11:26:12 -0700
committerRussell Belfer <rb@github.com>2012-10-31 11:26:12 -0700
commitc8b511f3cdb3f7fa63600b36bb2412099998a805 (patch)
tree493476084ca1c80ad27e3a00ef4cb7b2334ea7e7 /src
parent744cc03e2b6d77712bfcb504c272d2e646da650c (diff)
downloadlibgit2-c8b511f3cdb3f7fa63600b36bb2412099998a805.tar.gz
Better naming for file timestamp/size checker
Diffstat (limited to 'src')
-rw-r--r--src/attr.c16
-rw-r--r--src/attr_file.h2
-rw-r--r--src/fileops.c30
-rw-r--r--src/fileops.h40
4 files changed, 57 insertions, 31 deletions
diff --git a/src/attr.c b/src/attr.c
index 50caa1e1b..4993e3a8f 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -261,13 +261,13 @@ bool git_attr_cache__is_cached(
static int load_attr_file(
const char **data,
- git_futils_stat_sig *sig,
+ git_futils_file_stamp *stamp,
const char *filename)
{
int error;
git_buf content = GIT_BUF_INIT;
- error = git_futils_stat_sig_needs_reload(sig, filename);
+ error = git_futils_file_stamp_has_changed(stamp, filename);
if (error < 0)
return error;
@@ -380,7 +380,7 @@ int git_attr_cache__push_file(
git_attr_cache *cache = git_repository_attr_cache(repo);
git_attr_file *file = NULL;
git_blob *blob = NULL;
- git_futils_stat_sig sig;
+ git_futils_file_stamp stamp;
assert(filename && stack);
@@ -402,12 +402,10 @@ int git_attr_cache__push_file(
/* if not in cache, load data, parse, and cache */
if (source == GIT_ATTR_FILE_FROM_FILE) {
- if (file)
- memcpy(&sig, &file->cache_data.sig, sizeof(sig));
- else
- memset(&sig, 0, sizeof(sig));
+ git_futils_file_stamp_set(
+ &stamp, file ? &file->cache_data.stamp : NULL);
- error = load_attr_file(&content, &sig, filename);
+ error = load_attr_file(&content, &stamp, filename);
} else {
error = load_attr_blob_from_index(&content, &blob,
repo, file ? &file->cache_data.oid : NULL, relfile);
@@ -442,7 +440,7 @@ int git_attr_cache__push_file(
if (blob)
git_oid_cpy(&file->cache_data.oid, git_object_id((git_object *)blob));
else
- memcpy(&file->cache_data.sig, &sig, sizeof(sig));
+ git_futils_file_stamp_set(&file->cache_data.stamp, &stamp);
finish:
/* push file onto vector if we found one*/
diff --git a/src/attr_file.h b/src/attr_file.h
index ecd64866f..7f6932fb7 100644
--- a/src/attr_file.h
+++ b/src/attr_file.h
@@ -61,7 +61,7 @@ typedef struct {
bool pool_is_allocated;
union {
git_oid oid;
- git_futils_stat_sig sig;
+ git_futils_file_stamp stamp;
} cache_data;
} git_attr_file;
diff --git a/src/fileops.c b/src/fileops.c
index c22622c72..524ba4e77 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -671,27 +671,37 @@ int git_futils_cp_r(
return error;
}
-int git_futils_stat_sig_needs_reload(
- git_futils_stat_sig *sig, const char *path)
+int git_futils_file_stamp_has_changed(
+ git_futils_file_stamp *stamp, const char *path)
{
struct stat st;
- /* if the sig is NULL, then alway reload */
- if (sig == NULL)
+ /* if the stamp is NULL, then always reload */
+ if (stamp == NULL)
return 1;
if (p_stat(path, &st) < 0)
return GIT_ENOTFOUND;
- if ((git_time_t)st.st_mtime == sig->seconds &&
- (git_off_t)st.st_size == sig->size &&
- (unsigned int)st.st_ino == sig->ino)
+ if (stamp->mtime == (git_time_t)st.st_mtime &&
+ stamp->size == (git_off_t)st.st_size &&
+ stamp->ino == (unsigned int)st.st_ino)
return 0;
- sig->seconds = (git_time_t)st.st_mtime;
- sig->size = (git_off_t)st.st_size;
- sig->ino = (unsigned int)st.st_ino;
+ stamp->mtime = (git_time_t)st.st_mtime;
+ stamp->size = (git_off_t)st.st_size;
+ stamp->ino = (unsigned int)st.st_ino;
return 1;
}
+void git_futils_file_stamp_set(
+ git_futils_file_stamp *target, const git_futils_file_stamp *source)
+{
+ assert(target);
+
+ if (source)
+ memcpy(target, source, sizeof(*target));
+ else
+ memset(target, 0, sizeof(*target));
+}
diff --git a/src/fileops.h b/src/fileops.h
index 6437ccc45..ac0659d3f 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -267,26 +267,44 @@ extern int git_futils_find_system_file(git_buf *path, const char *filename);
*/
extern int git_futils_fake_symlink(const char *new, const char *old);
-
+/**
+ * A file stamp represents a snapshot of information about a file that can
+ * be used to test if the file changes. This portable implementation is
+ * based on stat data about that file, but it is possible that OS specific
+ * versions could be implemented in the future.
+ */
typedef struct {
- git_time_t seconds;
+ git_time_t mtime;
git_off_t size;
unsigned int ino;
-} git_futils_stat_sig;
+} git_futils_file_stamp;
/**
* Compare stat information for file with reference info.
*
- * Use this as a way to track if a file has changed on disk. This will
- * return GIT_ENOTFOUND if the file doesn't exist, 0 if the file is up-to-date
- * with regards to the signature, and 1 if the file needs to reloaded. When
- * a 1 is returned, the signature will also be updated with the latest data.
+ * This function updates the file stamp to current data for the given path
+ * and returns 0 if the file is up-to-date relative to the prior setting or
+ * 1 if the file has been changed. (This also may return GIT_ENOTFOUND if
+ * the file doesn't exist.)
*
- * @param sig stat signature structure
- * @param path path to be statted
+ * @param stamp File stamp to be checked
+ * @param path Path to stat and check if changed
* @return 0 if up-to-date, 1 if out-of-date, <0 on error
*/
-extern int git_futils_stat_sig_needs_reload(
- git_futils_stat_sig *sig, const char *path);
+extern int git_futils_file_stamp_has_changed(
+ git_futils_file_stamp *stamp, const char *path);
+
+/**
+ * Set or reset file stamp data
+ *
+ * This writes the target file stamp. If the source is NULL, this will set
+ * the target stamp to values that will definitely be out of date. If the
+ * source is not NULL, this copies the source values to the target.
+ *
+ * @param tgt File stamp to write to
+ * @param src File stamp to copy from or NULL to clear the target
+ */
+extern void git_futils_file_stamp_set(
+ git_futils_file_stamp *tgt, const git_futils_file_stamp *src);
#endif /* INCLUDE_fileops_h__ */