cephfs: Add futimens cephfs API

Added a new cephfs API named `ceph_futimens`

Fixes: #253

Signed-off-by: Nikhil-Ladha <nikhilladha1999@gmail.com>
This commit is contained in:
Nikhil-Ladha 2023-04-25 15:29:36 +05:30 committed by mergify[bot]
parent 71cf02d67f
commit 39336dae84
4 changed files with 84 additions and 0 deletions

View File

@ -6,12 +6,14 @@ package cephfs
/*
#cgo LDFLAGS: -lcephfs
#cgo CPPFLAGS: -D_FILE_OFFSET_BITS=64
#include <errno.h>
#include <stdlib.h>
#include <cephfs/libcephfs.h>
*/
import "C"
import (
ts "github.com/ceph/go-ceph/internal/timespec"
"unsafe"
)
@ -60,3 +62,34 @@ func (mount *MountInfo) Futime(fd int, times *Utime) error {
ret := C.ceph_futime(mount.mount, cFd, uTimeBuf)
return getError(ret)
}
// Futimens changes file/directory last access and modification times, here times param
// is an array of Timespec struct having length 2, where times[0] represents the access time
// and times[1] represents the modification time.
//
// Implements:
//
// int ceph_futimens(struct ceph_mount_info *cmount, int fd, struct timespec times[2]);
func (mount *MountInfo) Futimens(fd int, times []Timespec) error {
if err := mount.validate(); err != nil {
return err
}
if len(times) != 2 {
return getError(-C.EINVAL)
}
cFd := C.int(fd)
cTimes := []C.struct_timespec{}
for _, val := range times {
cTs := &C.struct_timespec{}
ts.CopyToCStruct(
ts.Timespec(val),
ts.CTimespecPtr(cTs),
)
cTimes = append(cTimes, *cTs)
}
ret := C.ceph_futimens(mount.mount, cFd, &cTimes[0])
return getError(ret)
}

View File

@ -91,3 +91,47 @@ func TestFutime(t *testing.T) {
err = mount1.Futime(int(f1.fd), newTime)
assert.Error(t, err)
}
func TestFutimens(t *testing.T) {
mount := fsConnect(t)
defer fsDisconnect(t, mount)
fname := "futimens_file.txt"
f1, err := mount.Open(fname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
assert.NoError(t, err)
assert.NotNil(t, f1)
defer func() {
assert.NoError(t, f1.Close())
assert.NoError(t, mount.Unlink(fname))
}()
times := []Timespec{
{int64(time.Now().Second()), 0},
{int64(time.Now().Second()), 0},
}
err = mount.Futimens(int(f1.fd), times)
assert.NoError(t, err)
sx, err := mount.Statx(fname, StatxBasicStats, 0)
assert.NoError(t, err)
assert.Equal(t, times[0], sx.Atime)
assert.Equal(t, times[1], sx.Mtime)
// Test invalid mount value
mount1 := &MountInfo{}
times = []Timespec{
{int64(time.Now().Second()), 0},
{int64(time.Now().Second()), 0},
}
err = mount1.Futimens(int(f1.fd), times)
assert.Error(t, err)
// Test times array length more than 2
times = []Timespec{
{int64(time.Now().Second()), 0},
{int64(time.Now().Second()), 0},
{int64(time.Now().Second()), 0},
}
err = mount.Futimens(int(f1.fd), times)
assert.Error(t, err)
}

View File

@ -337,6 +337,12 @@
"comment": "Futime changes file/directory last access and modification times.\n\nImplements:\n\n\tint ceph_futime(struct ceph_mount_info *cmount, int fd, struct utimbuf *buf);\n",
"added_in_version": "$NEXT_RELEASE",
"expected_stable_version": "$NEXT_RELEASE_STABLE"
},
{
"name": "MountInfo.Futimens",
"comment": "Futimens changes file/directory last access and modification times, here times param\nis an array of Timespec struct having length 2, where times[0] represents the access time\nand times[1] represents the modification time.\n\nImplements:\n\n\tint ceph_futimens(struct ceph_mount_info *cmount, int fd, struct timespec times[2]);\n",
"added_in_version": "$NEXT_RELEASE",
"expected_stable_version": "$NEXT_RELEASE_STABLE"
}
]
},

View File

@ -12,6 +12,7 @@ MountInfo.SelectFilesystem | v0.20.0 | v0.22.0 |
MountInfo.MakeDirs | v0.21.0 | v0.23.0 |
MountInfo.Mknod | $NEXT_RELEASE | $NEXT_RELEASE_STABLE |
MountInfo.Futime | $NEXT_RELEASE | $NEXT_RELEASE_STABLE |
MountInfo.Futimens | $NEXT_RELEASE | $NEXT_RELEASE_STABLE |
## Package: cephfs/admin