import e from"node:fs";import t from"node:path";import{randomUUID as n}from"node:crypto";function r(t){try{e.chmodSync(t,384)}catch{}}function i(n){let r;try{r=e.openSync(t.dirname(n),`r`),e.fsyncSync(r)}catch{}finally{if(r!==void 0)try{e.closeSync(r)}catch{}}}function a(n){let r=e.readlinkSync(n);return t.resolve(t.dirname(n),r)}function o(t){let n=t,r=new Set,i=!1;for(;;){let o;try{o=e.lstatSync(n)}catch(e){if(e.code!==`ENOENT`)throw e;return{targetPath:n,followsSymlink:i}}if(!o.isSymbolicLink())return{targetPath:n,followsSymlink:i};if(r.has(n)){let e=Error(`Too many symlink levels while resolving ${t}`);throw e.code=`ELOOP`,e}r.add(n),i=!0,n=a(n)}}function s(t,n){try{e.renameSync(t,n);return}catch(r){let i=r.code;if(i===`EPERM`||i===`EEXIST`){e.copyFileSync(t,n),e.rmSync(t,{force:!0});return}throw r}}function c(t,n){let r=e.openSync(t,`w`,384);try{e.writeFileSync(r,n,`utf8`),e.fsyncSync(r)}finally{e.closeSync(r)}}function l(t){try{let n=e.readFileSync(t,`utf8`);return JSON.parse(n)}catch{return}}function u(a,l){let{targetPath:u,followsSymlink:d}=o(a),f=`${u}.${n()}.tmp`,p=`${JSON.stringify(l,null,2)}\n`;d||e.mkdirSync(t.dirname(u),{recursive:!0,mode:448});try{c(f,p),r(f),s(f,u),r(u),i(u)}finally{try{e.rmSync(f,{force:!0})}catch{}}}export{u as n,l as t};