summaryrefslogtreecommitdiff
path: root/entry.c
diff options
context:
space:
mode:
Diffstat (limited to 'entry.c')
-rw-r--r--entry.c89
1 files changed, 64 insertions, 25 deletions
diff --git a/entry.c b/entry.c
index 004182c..17a6bcc 100644
--- a/entry.c
+++ b/entry.c
@@ -1,6 +1,7 @@
#include "cache.h"
#include "blob.h"
#include "dir.h"
+#include "streaming.h"
static void create_directories(const char *path, int path_len,
const struct checkout *state)
@@ -91,6 +92,48 @@ static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
return NULL;
}
+static int open_output_fd(char *path, struct cache_entry *ce, int to_tempfile)
+{
+ int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
+ if (to_tempfile) {
+ strcpy(path, symlink
+ ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
+ return mkstemp(path);
+ } else {
+ return create_file(path, !symlink ? ce->ce_mode : 0666);
+ }
+}
+
+static int fstat_output(int fd, const struct checkout *state, struct stat *st)
+{
+ /* use fstat() only when path == ce->name */
+ if (fstat_is_reliable() &&
+ state->refresh_cache && !state->base_dir_len) {
+ fstat(fd, st);
+ return 1;
+ }
+ return 0;
+}
+
+static int streaming_write_entry(struct cache_entry *ce, char *path,
+ struct stream_filter *filter,
+ const struct checkout *state, int to_tempfile,
+ int *fstat_done, struct stat *statbuf)
+{
+ int result = -1;
+ int fd;
+
+ fd = open_output_fd(path, ce, to_tempfile);
+ if (0 <= fd) {
+ result = stream_blob_to_fd(fd, ce->sha1, filter, 1);
+ *fstat_done = fstat_output(fd, state, statbuf);
+ result = close(fd);
+ }
+ if (result && 0 <= fd)
+ unlink(path);
+ return result;
+}
+
static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
{
unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
@@ -101,19 +144,28 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
size_t wrote, newsize = 0;
struct stat st;
+ if (ce_mode_s_ifmt == S_IFREG) {
+ struct stream_filter *filter = get_stream_filter(path, ce->sha1);
+ if (filter &&
+ !streaming_write_entry(ce, path, filter,
+ state, to_tempfile,
+ &fstat_done, &st))
+ goto finish;
+ }
+
switch (ce_mode_s_ifmt) {
case S_IFREG:
case S_IFLNK:
new = read_blob_entry(ce, &size);
if (!new)
- return error("git checkout-index: unable to read sha1 file of %s (%s)",
+ return error("unable to read sha1 file of %s (%s)",
path, sha1_to_hex(ce->sha1));
if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
ret = symlink(new, path);
free(new);
if (ret)
- return error("git checkout-index: unable to create symlink %s (%s)",
+ return error("unable to create symlink %s (%s)",
path, strerror(errno));
break;
}
@@ -128,45 +180,32 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
size = newsize;
}
- if (to_tempfile) {
- if (ce_mode_s_ifmt == S_IFREG)
- strcpy(path, ".merge_file_XXXXXX");
- else
- strcpy(path, ".merge_link_XXXXXX");
- fd = mkstemp(path);
- } else if (ce_mode_s_ifmt == S_IFREG) {
- fd = create_file(path, ce->ce_mode);
- } else {
- fd = create_file(path, 0666);
- }
+ fd = open_output_fd(path, ce, to_tempfile);
if (fd < 0) {
free(new);
- return error("git checkout-index: unable to create file %s (%s)",
+ return error("unable to create file %s (%s)",
path, strerror(errno));
}
wrote = write_in_full(fd, new, size);
- /* use fstat() only when path == ce->name */
- if (fstat_is_reliable() &&
- state->refresh_cache && !to_tempfile && !state->base_dir_len) {
- fstat(fd, &st);
- fstat_done = 1;
- }
+ if (!to_tempfile)
+ fstat_done = fstat_output(fd, state, &st);
close(fd);
free(new);
if (wrote != size)
- return error("git checkout-index: unable to write file %s", path);
+ return error("unable to write file %s", path);
break;
case S_IFGITLINK:
if (to_tempfile)
- return error("git checkout-index: cannot create temporary subproject %s", path);
+ return error("cannot create temporary subproject %s", path);
if (mkdir(path, 0777) < 0)
- return error("git checkout-index: cannot create subproject directory %s", path);
+ return error("cannot create subproject directory %s", path);
break;
default:
- return error("git checkout-index: unknown file mode for %s", path);
+ return error("unknown file mode for %s in index", path);
}
+finish:
if (state->refresh_cache) {
if (!fstat_done)
lstat(ce->name, &st);
@@ -211,7 +250,7 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *t
return 0;
if (!state->force) {
if (!state->quiet)
- fprintf(stderr, "git-checkout-index: %s already exists\n", path);
+ fprintf(stderr, "%s already exists, no checkout\n", path);
return -1;
}