cephfs: add Truncate implementing ceph_truncate

Add a Truncate method that operates on paths.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
This commit is contained in:
John Mulligan 2020-11-11 16:26:27 -05:00 committed by mergify[bot]
parent e2de2b6e58
commit 7cbacf9331
2 changed files with 68 additions and 0 deletions

View File

@ -173,3 +173,22 @@ func (mount *MountInfo) Rename(from, to string) error {
ret := C.ceph_rename(mount.mount, cFrom, cTo) ret := C.ceph_rename(mount.mount, cFrom, cTo)
return getError(ret) return getError(ret)
} }
// Truncate sets the size of the specified file.
//
// Implements:
// int ceph_truncate(struct ceph_mount_info *cmount, const char *path, int64_t size);
func (mount *MountInfo) Truncate(path string, size int64) error {
if err := mount.validate(); err != nil {
return err
}
cPath := C.CString(path)
defer C.free(unsafe.Pointer(cPath))
ret := C.ceph_truncate(
mount.mount,
cPath,
C.int64_t(size),
)
return getError(ret)
}

View File

@ -399,3 +399,52 @@ func TestRename(t *testing.T) {
assert.NoError(t, mount.RemoveDir(n2)) assert.NoError(t, mount.RemoveDir(n2))
}) })
} }
func TestTruncate(t *testing.T) {
mount := fsConnect(t)
defer fsDisconnect(t, mount)
fname := "TestTruncate.txt"
defer mount.Unlink(fname)
// "touch" the file
f, err := mount.Open(fname, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
assert.NoError(t, err)
assert.NoError(t, f.Close())
t.Run("invalidMount", func(t *testing.T) {
m := &MountInfo{}
err := m.Truncate(fname, 0)
assert.Error(t, err)
})
t.Run("invalidSize", func(t *testing.T) {
err := mount.Truncate(fname, -1)
assert.Error(t, err)
})
t.Run("invalidPath", func(t *testing.T) {
err := mount.Truncate(".Non~Existant~", 0)
assert.Error(t, err)
})
t.Run("valid", func(t *testing.T) {
err := mount.Truncate(fname, 1024)
assert.NoError(t, err)
st, err := mount.Statx(fname, StatxBasicStats, 0)
if assert.NoError(t, err) {
assert.NotNil(t, st)
assert.EqualValues(t, 1024, st.Size)
}
err = mount.Truncate(fname, 0)
assert.NoError(t, err)
st, err = mount.Statx(fname, StatxBasicStats, 0)
if assert.NoError(t, err) {
assert.NotNil(t, st)
assert.EqualValues(t, 0, st.Size)
}
})
}