Skip to content

Commit fb1e71b

Browse files
committed
FileHandle.truncateFile should reposition on truncate
- Accepting truncating to any position, not just 0. - Reposition to the truncate offset. - Darwin throws exceptions on error so we use fatalError() instead of silently failing.
1 parent 062649a commit fb1e71b

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

Foundation/FileHandle.swift

+4-5
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,12 @@ open class FileHandle : NSObject, NSSecureCoding {
142142
open func seek(toFileOffset offset: UInt64) {
143143
lseek(_fd, off_t(offset), SEEK_SET)
144144
}
145-
145+
146146
open func truncateFile(atOffset offset: UInt64) {
147-
if lseek(_fd, off_t(offset), SEEK_SET) == 0 {
148-
ftruncate(_fd, off_t(offset))
149-
}
147+
if lseek(_fd, off_t(offset), SEEK_SET) < 0 { fatalError("lseek() failed.") }
148+
if ftruncate(_fd, off_t(offset)) < 0 { fatalError("ftruncate() failed.") }
150149
}
151-
150+
152151
open func synchronizeFile() {
153152
fsync(_fd)
154153
}

0 commit comments

Comments
 (0)