Oops, preserve the size invariant.

geesefs-0-30-9
Aaron Jacobs 2015-03-06 05:56:12 +11:00
parent 9dc1d04623
commit 910d3b2f9f
2 changed files with 11 additions and 1 deletions

View File

@ -377,14 +377,19 @@ func (inode *inode) SetAttributes(size *uint64) {
// Update the modification time.
inode.attributes.Mtime = inode.clock.Now()
// Do we need to truncate?
// Truncate?
if size != nil {
intSize := int(*size)
// Update contents.
if intSize <= len(inode.contents) {
inode.contents = inode.contents[:intSize]
} else {
padding := make([]byte, intSize-len(inode.contents))
inode.contents = append(inode.contents, padding...)
}
// Update attributes.
inode.attributes.Size = *size
}
}

View File

@ -875,6 +875,11 @@ func (t *MemFSTest) Truncate_Smaller() {
err = f.Truncate(2)
AssertEq(nil, err)
// Stat it.
fi, err := f.Stat()
AssertEq(nil, err)
ExpectEq(2, fi.Size())
// Read the contents.
contents, err := ioutil.ReadFile(fileName)
AssertEq(nil, err)