Replace a file without a partial write
Write the new contents to a temporary file beside the old one, then rename it over the old one. On Unix, a program that reads the file at the same time sees the old contents or the new ones, never half of each, and a write that fails, or a crash, leaves the old file as it was.
func replaceFile(path: string, text: string): Result[void, Error] {
let tmp = try File.tempFile(".${Path.base(path)}-*", dir: Path.dir(path))
defer { File.remove(tmp) }
let out = try File.create(tmp)
defer { out.close() }
try out.writeText(text)
try out.sync()
try out.close()
File.rename(tmp, path)
}
try File.mkdir("config")
try File.write("config/settings.txt", "theme=light\n")
try replaceFile("config/settings.txt", "theme=dark\n")
print(try File.read("config/settings.txt"))
println(try File.list("config"))theme=dark
["settings.txt"]- Create the temporary file with File.tempFile in the directory of the target, which Path.dir gives. A rename across file systems is an
Err, so do not use the host's temporary directory. Start the pattern with a.and the name from Path.base, so a glob such as*does not match it. - Remove the temporary file in a defer right after creating it, so an
Errfrom a later step does not leave it behind. After the rename, File.remove finds nothing there and itsErris discarded. - Open the temporary file with File.create, close it in a
defer, and write the contents with File.writeText. - Call File.sync before the rename. Closing a file hands its data to the OS but does not wait for the disk, so after a crash or power loss the new name could point at an empty or partial file.
synchas the OS write it to storage, so it survives a crash. Then close it and check the result. - Rename the temporary file over the target with File.rename. On Unix the rename replaces the target in one step. On other systems it is not promised to be atomic.
When the path may be a symbolic link, File.rename replaces the link itself. To replace the file it points at, resolve the path first with File.realPath and use the result for both Path.dir and the rename.
File.list shows that no temporary file is left in the directory.
Keep the mode of the old file
File.tempFile creates the file with mode 0o600, and the rename puts that file in place, so on Unix the new file is readable only by you. That suits a file that holds a secret, such as a token. To keep the mode of the file you replace, read it with File.stat and set it on the temporary file with File.chmod before the rename. Skip it with File.exists when there is no old file yet:
func replaceFile(path: string, text: string): Result[void, Error] {
let tmp = try File.tempFile(".${Path.base(path)}-*", dir: Path.dir(path))
defer { File.remove(tmp) }
if File.exists(path) {
let info = try File.stat(path)
try File.chmod(tmp, info.mode)
}
let out = try File.create(tmp)
defer { out.close() }
try out.writeText(text)
try out.sync()
try out.close()
File.rename(tmp, path)
}
try File.write("deploy.sh", "echo old\n")
try File.chmod("deploy.sh", 0o755)
try replaceFile("deploy.sh", "echo new\n")
let info = try File.stat("deploy.sh")
println(String.fromInt(info.mode, base: 8))755The new file also belongs to the user who runs the program. Run as root, File.chown gives it the old file's owner from the same info: try File.chown(tmp, info.uid ?? -1, info.gid ?? -1) next to the chmod.
Write the new file a line at a time
To write output in many small pieces, open the temporary file with File.create and write through File.buffered. Sync and then close the handle with File.close, and check the result before the rename: a failed sync or close means the file may be incomplete, and the old file must stay. On a buffered handle sync writes out the buffer first. The deferred close covers the paths that leave early and does nothing after the explicit one.
func writeLines(path: string, lines: Array[string]): Result[void, Error] {
let tmp = try File.tempFile(".${Path.base(path)}-*", dir: Path.dir(path))
defer { File.remove(tmp) }
let out = File.buffered(try File.create(tmp))
defer { out.close() }
for line in lines {
try out.writeLine(line)
}
try out.sync()
try out.close()
File.rename(tmp, path)
}
try writeLines("squares.txt", ["1", "4", "9"])
print(try File.read("squares.txt"))1
4
9File.writeLine writes each line to the buffer.
Never replace an existing file
To create a file only when nothing is at the path, pass replace: false to File.write. The check and the creation happen in one step, so another program cannot slip a file in between. When something is there, File.write returns an Err and changes nothing:
try File.write("report.txt", "first\n", replace: false)
let saved = File.write("report.txt", "second\n", replace: false)
if saved is Err {
println("kept: ${saved.error.message}")
}
print(try File.read("report.txt"))kept: open report.txt: file exists
firstFile.writeBytes, File.create and File.copy take replace: false too.
See also
File.tempFile · File.rename · File.sync · File.writeText · File.write · File.remove · File.stat · File.chmod · File.chown · File.exists · File.create · File.buffered · File.close · File.writeLine · File.read · File.list · File.mkdir · File.realPath · Path.dir · Path.base · String.fromInt · Use a temporary file or directory · Read and write a whole file
