git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 09/17] safe_create_leading_directories(): add new error value SCLD_VANISHED

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Jan 18, 2014, 22:48 UTC
Message-ID
<1390085341-2553-10-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1390085341-2553-1-git-send-email-mhagger@alum.mit.edu>

Add a new possible error result that can be returned by safe_create_leading_directories() and safe_create_leading_directories_const(): SCLD_VANISHED. This value indicates that a file or directory on the path existed at one point (either it already existed or the function created it), but then it disappeared. This probably indicates that another process deleted the directory while we were working. If SCLD_VANISHED is returned, the caller might want to retry the function call, as there is a chance that a new attempt will succeed.

Why doesn't safe_create_leading_directories() do the retrying internally? Because an empty directory isn't really ever safe until it holds a file. So even if safe_create_leading_directories() were absolutely sure that the directory existed before it returned, there would be no guarantee that the directory still existed when the caller tried to write something in it.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 cache.h     | 10 +++++++++-
 sha1_file.c | 11 +++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/cache.h b/cache.h
index 8030e36..c0a7a8a 100644
--- a/cache.h
+++ b/cache.h
@@ -742,12 +742,20 @@ int adjust_shared_perm(const char *path);
  * Create the directory containing the named path, using care to be
  * somewhat safe against races.  Return one of the scld_error values
  * to indicate success/failure.
+ *
+ * SCLD_VANISHED indicates that one of the ancestor directories of the
+ * path existed at one point during the function call and then
+ * suddenly vanished, probably because another process pruned the
+ * directory while we were working.  To be robust against this kind of
+ * race, callers might want to try invoking the function again when it
+ * returns SCLD_VANISHED.
  */
 enum scld_error {
 	SCLD_OK = 0,
 	SCLD_FAILED = -1,
 	SCLD_PERMS = -2,
-	SCLD_EXISTS = -3
+	SCLD_EXISTS = -3,
+	SCLD_VANISHED = -4
 };
 enum scld_error safe_create_leading_directories(char *path);
 enum scld_error safe_create_leading_directories_const(const char *path);
diff --git a/sha1_file.c b/sha1_file.c
index a065308..8b0849f 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -132,6 +132,17 @@ enum scld_error safe_create_leading_directories(char *path)
 			if (errno == EEXIST &&
 			    !stat(path, &st) && S_ISDIR(st.st_mode))
 				; /* somebody created it since we checked */
+			else if (errno == ENOENT)
+				/*
+				 * Either mkdir() failed because
+				 * somebody just pruned the containing
+				 * directory, or stat() failed because
+				 * the file that was in our way was
+				 * just removed.  Either way, inform
+				 * the caller that it might be worth
+				 * trying again:
+				 */
+				ret = SCLD_VANISHED;
 			else
 				ret = SCLD_FAILED;
 		} else if (adjust_shared_perm(path)) {
-- 
1.8.5.2
Previous: Michael HaggertyNext: Michael Haggerty
Message 10 of 18 in “Fix some mkdir/rmdir races”
  1. 00/17 Fix some mkdir/rmdir racesMichael Haggerty, Jan 18, 2014
  2. 01/17 safe_create_leading_directories(): fix format of "if" chainingMichael Haggerty, Jan 18, 2014
  3. 02/17 safe_create_leading_directories(): reduce scope of local variableMichael Haggerty, Jan 18, 2014
  4. 03/17 safe_create_leading_directories(): add explicit "slash" pointerMichael Haggerty, Jan 18, 2014
  5. 04/17 safe_create_leading_directories(): rename local variableMichael Haggerty, Jan 18, 2014
  6. 05/17 safe_create_leading_directories(): split on first of multiple slashesMichael Haggerty, Jan 18, 2014
  7. 06/17 safe_create_leading_directories(): always restore slash at end of loopMichael Haggerty, Jan 18, 2014
  8. 07/17 safe_create_leading_directories(): introduce enum for return valuesMichael Haggerty, Jan 18, 2014
  9. 08/17 cmd_init_db(): when creating directories, handle errors conservativelyMichael Haggerty, Jan 18, 2014
  10. 09/17 safe_create_leading_directories(): add new error value SCLD_VANISHEDMichael Haggerty, Jan 18, 2014
  11. 10/17 lock_ref_sha1_basic(): on SCLD_VANISHED, retryMichael Haggerty, Jan 18, 2014
  12. 11/17 lock_ref_sha1_basic(): if locking fails with ENOENT, retryMichael Haggerty, Jan 18, 2014
  13. 12/17 remove_dir_recurse(): tighten condition for removing unreadable dirMichael Haggerty, Jan 18, 2014
  14. 13/17 remove_dir_recurse(): handle disappearing files and directoriesMichael Haggerty, Jan 18, 2014
  15. 14/17 rename_ref(): extract function rename_tmp_log()Michael Haggerty, Jan 18, 2014
  16. 15/17 rename_tmp_log(): handle a possible mkdir/rmdir raceMichael Haggerty, Jan 18, 2014
  17. 16/17 rename_tmp_log(): limit the number of remote_empty_directories() attemptsMichael Haggerty, Jan 18, 2014
  18. 17/17 rename_tmp_log(): on SCLD_VANISHED, retryMichael Haggerty, Jan 18, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.