Title: add fs.mkstemp (lib_uv recently landed uv_fs_mkstemp #21) · Issue #33890 · nodejs/node · GitHub
Open Graph Title: add fs.mkstemp (lib_uv recently landed uv_fs_mkstemp #21) · Issue #33890 · nodejs/node
X Title: add fs.mkstemp (lib_uv recently landed uv_fs_mkstemp #21) · Issue #33890 · nodejs/node
Description: Node.js JavaScript runtime ✨🐢🚀✨. Contribute to nodejs/node development by creating an account on GitHub.
Open Graph Description: Node.js JavaScript runtime ✨🐢🚀✨. Contribute to nodejs/node development by creating an account on GitHub.
X Description: Node.js JavaScript runtime ✨🐢🚀✨. Contribute to nodejs/node development by creating an account on GitHub.
Opengraph URL: https://github.com/nodejs/node
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"add fs.mkstemp (lib_uv recently landed uv_fs_mkstemp #21)","articleBody":"\u003c!--\r\nThank you for suggesting an idea to make Node.js better.\r\n\r\nPlease fill in as much of the template below as you're able.\r\n-->\r\n\r\n\r\n\r\n**Is your feature request related to a problem? Please describe.**\r\nPlease describe the problem you are trying to solve.\r\ni have a build-system (in debian-docker under windows) that async-updates files w/ multiple child_processes doing `fs.writeFile, fs.unlink, fs.link`.\r\nthere are occasional race-conditions (i can't discern how) where one child_process writing to a file while another child_process unlinking-and-hardlinking ends up with file unlinked.\r\n\r\n\r\n\r\n**Describe the solution you'd like**\r\nPlease describe the desired behavior.\r\nmkstemp would make file-writes \"atomic\" by writing to tmpfile and then \"atomically\" rename it to destination. this might resolve the race condition from my multiple child_processes and allow the final hardlink to succeed.\r\n\r\nideal solution of atomic file-write with mkstemp:\r\n```js\r\n/*jslint browser, node*/\r\n/*global os*/\r\nfunction fsWriteFileWithMkdirpAtomic(pathname, data, onError) {\r\n/*\r\n * this file with async-atomically write \u003cdata> to \u003cpathname> with auto mkdir -p\r\n */\r\n let fs;\r\n let path;\r\n let throwOnError;\r\n throwOnError = function (err) {\r\n /*\r\n * this function will throw \u003cerr> if exists\r\n * we typically want program to crash on \u003cerr> in ci/build use-case\r\n */\r\n if (err) {\r\n throw err;\r\n }\r\n };\r\n try {\r\n fs = require(\"fs\");\r\n path = require(\"path\");\r\n // win32-compat\r\n pathname = path.resolve(pathname);\r\n } catch (ignore) {\r\n // do nothing in browser-env\r\n onError();\r\n return;\r\n }\r\n fs.mkstemp(os.tmpdir(), function (err, tmpfile) {\r\n throwOnError(err);\r\n fs.writeFile(tmpfile, data, function (err) {\r\n throwOnError(err);\r\n // fs.rename attempt #1\r\n fs.rename(tmpfile, pathname, function (err) {\r\n if (!err) {\r\n onError();\r\n return;\r\n }\r\n // mkdir -p\r\n fs.mkdirp(path.dirname(pathname), {\r\n recursive: true\r\n }, function (ignore) {\r\n // fs.rename attempt #2\r\n fs.rename(tmpfile, pathname, function (err) {\r\n throwOnError(err);\r\n onError();\r\n });\r\n });\r\n });\r\n });\r\n });\r\n}\r\nfsWriteFileWithMkdirpAtomic(\"aa/bb/cc/dd/foo\", \"hello world\", function () {\r\n console.log(\"successfully wrote file atomically with no race-condition\");\r\n});\r\n```\r\n\r\n\r\n**Describe alternatives you've considered**\r\nPlease describe alternative solutions or features you have considered.\r\n\r\n* use a random generator to create tmpfile in os.tmpdir(), which is not idead for obvious reasons.\r\n* or use `fs.mkdtemp` and deal with cleaninup tmpdir after atomic-write, which again, not ideal.\r\n","author":{"url":"https://github.com/kaizhu256","@type":"Person","name":"kaizhu256"},"datePublished":"2020-06-15T08:57:47Z","url":"https://github.com/nodejs/node/issues/33890"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:b112944c-c29c-9b81-bcd4-abf05f87bde0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 811E:3DE7C3:14AA6D:1A2A96:6B0339BF |
| html-safe-nonce | 8d40c6f9c31ba3b2e06c00358e2565451a1af9b520eac6c734e5e5f08aa36e97 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MTFFOjNERTdDMzoxNEFBNkQ6MUEyQTk2OjZCMDMzOUJGIiwidmlzaXRvcl9pZCI6IjMzODk2MDg5NjY1NTUzODQyNTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | cc5a1f5b6b44036fbf37dfb74d8611858724e60d0bb3bbaf906218655a868f32 |
| hovercard-subject-tag | repository:27193779 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/nodejs/node/issues/33890 |
| twitter:image | https://opengraph.githubassets.com/419c8eede4acea769cde62d931cd57dc40c83a62e950fae7609f453d5580b92a/nodejs/node |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/419c8eede4acea769cde62d931cd57dc40c83a62e950fae7609f453d5580b92a/nodejs/node |
| og:image:alt | Node.js JavaScript runtime ✨🐢🚀✨. Contribute to nodejs/node development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-cache |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width