cephfs: Add futimes cephfs API

Added a new cephfs API named `ceph_futimes`

Fixes: #254

Signed-off-by: Nikhil-Ladha <nikhilladha1999@gmail.com>
This commit is contained in:
Nikhil-Ladha 2023-04-25 17:02:24 +05:30 committed by mergify[bot]
parent 009a3fa2c1
commit 15ede44c2f
4 changed files with 109 additions and 0 deletions

View File

@ -63,6 +63,14 @@ func (mount *MountInfo) Futime(fd int, times *Utime) error {
return getError(ret)
}
// Timeval struct is the go equivalent of C.struct_timeval type
type Timeval struct {
// Sec represents seconds
Sec int64
// USec represents microseconds
USec int64
}
// 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.
@ -93,3 +101,32 @@ func (mount *MountInfo) Futimens(fd int, times []Timespec) error {
ret := C.ceph_futimens(mount.mount, cFd, &cTimes[0])
return getError(ret)
}
// Futimes changes file/directory last access and modification times, here times param
// is an array of Timeval struct type having length 2, where times[0] represents the access time
// and times[1] represents the modification time.
//
// Implements:
//
// int ceph_futimes(struct ceph_mount_info *cmount, int fd, struct timeval times[2]);
func (mount *MountInfo) Futimes(fd int, times []Timeval) error {
if err := mount.validate(); err != nil {
return err
}
if len(times) != 2 {
return getError(-C.EINVAL)
}
cFd := C.int(fd)
cTimes := []C.struct_timeval{}
for _, val := range times {
cTimes = append(cTimes, C.struct_timeval{
tv_sec: C.time_t(val.Sec),
tv_usec: C.suseconds_t(val.USec),
})
}
ret := C.ceph_futimes(mount.mount, cFd, &cTimes[0])
return getError(ret)
}

View File

@ -135,3 +135,68 @@ func TestFutimens(t *testing.T) {
err = mount.Futimens(int(f1.fd), times)
assert.Error(t, err)
}
func TestFutimes(t *testing.T) {
mount := fsConnect(t)
defer fsDisconnect(t, mount)
fname := "futimes_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},
}
newTimes := []Timeval{}
for _, val := range times {
newTimes = append(newTimes, Timeval{
Sec: val.Sec,
USec: int64(val.Nsec / 1000),
})
}
err = mount.Futimes(int(f1.fd), newTimes)
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},
}
newTimes = []Timeval{}
for _, val := range times {
newTimes = append(newTimes, Timeval{
Sec: val.Sec,
USec: int64(val.Nsec / 1000),
})
}
err = mount1.Futimes(int(f1.fd), newTimes)
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},
}
newTimes = []Timeval{}
for _, val := range times {
newTimes = append(newTimes, Timeval{
Sec: val.Sec,
USec: int64(val.Nsec / 1000),
})
}
err = mount.Futimes(int(f1.fd), newTimes)
assert.Error(t, err)
}

View File

@ -343,6 +343,12 @@
"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"
},
{
"name": "MountInfo.Futimes",
"comment": "Futimes changes file/directory last access and modification times, here times param\nis an array of Timeval struct type having length 2, where times[0] represents the access time\nand times[1] represents the modification time.\n\nImplements:\n\n\tint ceph_futimes(struct ceph_mount_info *cmount, int fd, struct timeval times[2]);\n",
"added_in_version": "$NEXT_RELEASE",
"expected_stable_version": "$NEXT_RELEASE_STABLE"
}
]
},

View File

@ -13,6 +13,7 @@ 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 |
MountInfo.Futimes | $NEXT_RELEASE | $NEXT_RELEASE_STABLE |
## Package: cephfs/admin