MCPcopy
hub / github.com/just-js/just / writeFile

Function writeFile

lib/fs.js:108–132  ·  view source on GitHub ↗
(path, buf, flags = fs.O_WRONLY | fs.O_CREAT | fs.O_TRUNC)

Source from the content-addressed store, hash-verified

106}
107
108function writeFile (path, buf, flags = fs.O_WRONLY | fs.O_CREAT | fs.O_TRUNC) {
109 const len = buf.byteLength
110 if (!len) return -1
111 const fd = fs.open(path, flags)
112 if (fd < 0) throw new Error(`Error Opening File ${path}: ${sys.errno()}`)
113 const chunks = Math.ceil(len / 4096)
114 let total = 0
115 let bytes = 0
116 for (let i = 0, off = 0; i < chunks; ++i, off += 4096) {
117 const towrite = Math.min(len - off, 4096)
118 bytes = net.write(fd, buf, towrite, off)
119 if (bytes <= 0) break
120 total += bytes
121 }
122 if (bytes < 0) {
123 const errno = sys.errno()
124 throw new Error(`Error Writing to File: ${errno} (${sys.strerror(errno)})`)
125 }
126 if (bytes === 0) {
127 throw new Error(`Zero Bytes Written: ${sys.errno()}`)
128 }
129 const r = net.close(fd)
130 if (r < 0) throw new Error(`Error Closing File: ${sys.errno()}`)
131 return total
132}
133
134function simpleStats (fd) {
135 const stat = new BigUint64Array(20)

Callers 3

runFunction · 0.85
safeWriteFunction · 0.85
runFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected