Commit Diff


commit - f96b13c8aa83acca1d0eea6e9b5bb27a2de82866
commit + 301ae139af015c305161aeb8e394094a512c6c04
blob - a223de14baf9eab1c747904cbc10cbaf30003d8c
blob + 7cb114b362ef4f4a6d3f385fb37066d5a97691bd
--- include/got_path.h
+++ include/got_path.h
@@ -132,3 +132,10 @@ const struct got_error *got_path_find_prog(char **, co
 
 /* Create a new file at a specified path, with optional content. */
 const struct got_error *got_path_create_file(const char *, const char *);
+
+/*
+ * Attempt to move an existing file to a new path, creating missing parent
+ * directories at the destination path if necessary.
+ * (Cross-mount-point moves are not yet implemented.)
+ */
+const struct got_error *got_path_move_file(const char *, const char *);
blob - 1617ced887ec4ac9d33c92a536325e8c06a1db2e
blob + a5a852ddc8aa3801899972c08a4acfbca60a20dc
--- lib/path.c
+++ lib/path.c
@@ -539,3 +539,24 @@ done:
 		err = got_error_from_errno("close");
 	return err;
 }
+
+const struct got_error *
+got_path_move_file(const char *oldpath, const char *newpath)
+{
+	const struct got_error *err;
+
+	if (rename(oldpath, newpath) != -1)
+		return NULL;
+
+	if (errno != ENOENT)
+		return got_error_from_errno3("rename", oldpath, newpath);
+
+	err = make_parent_dirs(newpath);
+	if (err)
+		return err;
+
+	if (rename(oldpath, newpath) == -1)
+		return got_error_from_errno3("rename", oldpath, newpath);
+
+	return NULL;
+}