gitfo_close(file->fd);
file->fd = -1;
- error = gitfo_move_file(file->path_lock, file->path_original);
+ error = gitfo_mv(file->path_lock, file->path_original);
cleanup:
git_filebuf_cleanup(file);
obj->data = NULL;
}
-int gitfo_move_file(char *from, char *to)
+int gitfo_mv(const char *from, const char *to)
{
#ifdef GIT_WIN32
/*
#endif
}
+int gitfo_mv_force(const char *from, const char *to)
+{
+ const int mode = 0755; /* or 0777 ? */
+ int error = GIT_SUCCESS;
+ char target_folder_path[GIT_PATH_MAX];
+
+ error = git__dirname_r(target_folder_path, sizeof(target_folder_path), to);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ /* Does the containing folder exist? */
+ if (gitfo_isdir(target_folder_path)) {
+ git__joinpath(target_folder_path, target_folder_path, ""); /* Ensure there's a trailing slash */
+
+ /* Let's create the tree structure */
+ error = gitfo_mkdir_recurs(target_folder_path, mode);
+ if (error < GIT_SUCCESS)
+ return error;
+ }
+
+ return gitfo_mv(from, to);
+}
+
int gitfo_map_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
{
if (git__mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin) < GIT_SUCCESS)
extern int gitfo_read_file(gitfo_buf *obj, const char *path);
extern void gitfo_free_buf(gitfo_buf *obj);
-extern int gitfo_move_file(char *from, char *to);
+
+/* Move (rename) a file; this operation is atomic */
+extern int gitfo_mv(const char *from, const char *to);
+
+/* Move a file (forced); this will create the destination
+ * path if it doesn't exist */
+extern int gitfo_mv_force(const char *from, const char *to);
#define gitfo_stat(p,b) stat(p, b)
#define gitfo_fstat(f,b) fstat(f, b)
gitfo_close(fd);
gitfo_chmod(temp, 0444);
- if (gitfo_move_file(temp, file) < 0) {
+ if (gitfo_mv(temp, file) < 0) {
gitfo_unlink(temp);
return GIT_EOSERR;
}
git__joinpath(old_path, ref->owner->path_repository, old_name);
git__joinpath(new_path, ref->owner->path_repository, ref->name);
- error = gitfo_move_file(old_path, new_path);
+ error = gitfo_mv_force(old_path, new_path);
if (error < GIT_SUCCESS)
goto cleanup;
git__joinpath(new_path, ref->owner->path_repository, old_name);
/* No error checking. We'll return the initial error */
- gitfo_move_file(old_path, new_path);
+ gitfo_mv_force(old_path, new_path);
/* restore the old name */
free(ref->name);