commit 5bb4ff2b1627b8abf239cb4bc54375109b3e3a01 from: Stefan Sperling date: Sat Oct 15 18:41:40 2022 UTC introduce got_path_move_file(); based on code from lib/patch.c ok op@ commit - 15c388a9fbe0ca9bd3fba71581e55dd5ad6d8c33 commit + 5bb4ff2b1627b8abf239cb4bc54375109b3e3a01 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 - e3d7799acfdd15ffc55d19126b7c7e27a696bee6 blob + 890ae033826305afdd0fc0401d8ff424ea07eaf6 --- lib/path.c +++ lib/path.c @@ -537,3 +537,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; +}