summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 211fb4f)
raw | patch | inline | side by side (parent: 211fb4f)
author | Junio C Hamano <junkio@cox.net> | |
Sat, 12 May 2007 05:11:07 +0000 (22:11 -0700) | ||
committer | Junio C Hamano <junkio@cox.net> | |
Sat, 12 May 2007 05:11:07 +0000 (22:11 -0700) |
When we are applying a patch that creates a blob at a path, or
when we are switching from a branch that does not have a blob at
the path to another branch that has one, we need to make sure
that there is nothing at the path in the working tree, as such a
file is a local modification made by the user that would be lost
by the operation.
Normally, lstat() on the path and making sure ENOENT is returned
is good enough for that purpose. However there is a twist. We
may be creating a regular file arch/x86_64/boot/Makefile, while
removing an existing symbolic link at arch/x86_64/boot that
points at existing ../i386/boot directory that has Makefile in
it. We always first check without touching filesystem and then
perform the actual operation, so when we verify the new file,
arch/x86_64/boot/Makefile, does not exist, we haven't removed
the symbolic link arc/x86_64/boot symbolic link yet. lstat() on
the file sees through the symbolic link and reports the file is
there, which is not what we want.
The function has_symlink_leading_path() function takes a path,
and sees if any of the leading directory component is a symbolic
link.
When files in a new directory are created, we tend to process
them together because both index and tree are sorted. The
function takes advantage of this and allows the caller to cache
and reuse which symbolic link on the filesystem caused the
function to return true.
The calling sequence would be:
char last_symlink[PATH_MAX];
*last_symlink = '\0';
for each index entry {
if (!lose)
continue;
if (lstat(it))
if (errno == ENOENT)
; /* happy */
else
error;
else if (has_symlink_leading_path(it, last_symlink))
; /* happy */
else
error; /* would lose local changes */
unlink_entry(it, last_symlink);
}
Signed-off-by: Junio C Hamano <junkio@cox.net>
when we are switching from a branch that does not have a blob at
the path to another branch that has one, we need to make sure
that there is nothing at the path in the working tree, as such a
file is a local modification made by the user that would be lost
by the operation.
Normally, lstat() on the path and making sure ENOENT is returned
is good enough for that purpose. However there is a twist. We
may be creating a regular file arch/x86_64/boot/Makefile, while
removing an existing symbolic link at arch/x86_64/boot that
points at existing ../i386/boot directory that has Makefile in
it. We always first check without touching filesystem and then
perform the actual operation, so when we verify the new file,
arch/x86_64/boot/Makefile, does not exist, we haven't removed
the symbolic link arc/x86_64/boot symbolic link yet. lstat() on
the file sees through the symbolic link and reports the file is
there, which is not what we want.
The function has_symlink_leading_path() function takes a path,
and sees if any of the leading directory component is a symbolic
link.
When files in a new directory are created, we tend to process
them together because both index and tree are sorted. The
function takes advantage of this and allows the caller to cache
and reuse which symbolic link on the filesystem caused the
function to return true.
The calling sequence would be:
char last_symlink[PATH_MAX];
*last_symlink = '\0';
for each index entry {
if (!lose)
continue;
if (lstat(it))
if (errno == ENOENT)
; /* happy */
else
error;
else if (has_symlink_leading_path(it, last_symlink))
; /* happy */
else
error; /* would lose local changes */
unlink_entry(it, last_symlink);
}
Signed-off-by: Junio C Hamano <junkio@cox.net>
cache.h | patch | blob | history | |
symlinks.c | [new file with mode: 0644] | patch | blob |
index 8e76152645dac5ab5b0b5b54a2c87aa36d51bcff..aaeb04a1b9f756d946b06cad8561625ead9bb8a9 100644 (file)
--- a/cache.h
+++ b/cache.h
};
extern int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath);
+extern int has_symlink_leading_path(const char *name, char *last_symlink);
extern struct alternate_object_database {
struct alternate_object_database *next;
diff --git a/symlinks.c b/symlinks.c
--- /dev/null
+++ b/symlinks.c
@@ -0,0 +1,48 @@
+#include "cache.h"
+
+int has_symlink_leading_path(const char *name, char *last_symlink)
+{
+ char path[PATH_MAX];
+ const char *sp, *ep;
+ char *dp;
+
+ sp = name;
+ dp = path;
+
+ if (last_symlink && *last_symlink) {
+ size_t last_len = strlen(last_symlink);
+ size_t len = strlen(name);
+ if (last_len < len &&
+ !strncmp(name, last_symlink, last_len) &&
+ name[last_len] == '/')
+ return 1;
+ *last_symlink = '\0';
+ }
+
+ while (1) {
+ size_t len;
+ struct stat st;
+
+ ep = strchr(sp, '/');
+ if (!ep)
+ break;
+ len = ep - sp;
+ if (PATH_MAX <= dp + len - path + 2)
+ return 0; /* new name is longer than that??? */
+ memcpy(dp, sp, len);
+ dp[len] = 0;
+
+ if (lstat(path, &st))
+ return 0;
+ if (S_ISLNK(st.st_mode)) {
+ if (last_symlink)
+ strcpy(last_symlink, path);
+ return 1;
+ }
+
+ dp[len++] = '/';
+ dp = dp + len;
+ sp = ep + 1;
+ }
+ return 0;
+}