Skip to contentSkip to conversation

erofs-utils: lib: create Windows temporary files atomically

PATCH New Gao Xiang 1 participant 1 message Nov 4 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/1 erofs-utils: lib: create Windows temporary files atomically

GetTempFileNameA() creates and closes a file before it is reopened with FILE_FLAG_DELETE_ON_CLOSE. Exiting between these calls leaves the temporary file behind.

Use CREATE_NEW with FILE_FLAG_DELETE_ON_CLOSE to create temporary files with cleanup enabled from the start. Generate random names and retry collisions without opening existing files.

Also honor TMPDIR and preserve errors on descriptor allocation failure. Since erofs_os_tmpfile() returns negative errors directly, propagate its return value in the OCI caller rather than relying on errno.

Fixes f9ceb52fa646 ("erofs-utils: add native Windows support")
---
 lib/remotes/oci.c    |  8 ++++--
 lib/windows_compat.c | 67 +++++++++++++++++++++++++++-----------------
 2 files changed, 47 insertions(+), 28 deletions(-)
lib/remotes/oci.c +5 -3
index 3243ccc..948d699 100644
@@ -1260,12 +1260,14 @@ static int ocierofs_extract_layer(struct ocierofs_ctx *ctx,
 
 	stream = (struct ocierofs_stream) {
 		.digest = digest,
-		.blobfd = erofs_os_tmpfile(),
 	};
-	if (stream.blobfd < 0) {
+
+	ret = erofs_os_tmpfile();
+	if (ret < 0) {
 		erofs_err("failed to create temporary file for %s", digest);
-		return -errno;
+		return ret;
 	}
+	stream.blobfd = ret;
 
 	ret = ocierofs_download_blob_to_fd(ctx, digest, auth_header, stream.blobfd);
 	if (ret)
lib/windows_compat.c +42 -25

80 lines not on this page · raw message

Signature · 1 line
2.54.0 (Apple Git-157)