Skip to content

Commit

Permalink
cephfs: Add futimes cephfs API
Browse files Browse the repository at this point in the history
Added a new cephfs API named `ceph_futimes`

Fixes: #254

Signed-off-by: Nikhil-Ladha <[email protected]>
  • Loading branch information
Nikhil-Ladha committed May 3, 2023
1 parent cd007bc commit 88f56c2
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
38 changes: 38 additions & 0 deletions cephfs/file_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cephfs
/*
#cgo LDFLAGS: -lcephfs
#cgo CPPFLAGS: -D_FILE_OFFSET_BITS=64
#include <errno.h>
#include <stdlib.h>
#include <cephfs/libcephfs.h>
*/
Expand Down Expand Up @@ -60,3 +61,40 @@ func (mount *MountInfo) Futime(fd int, times *Utime) error {
ret := C.ceph_futime(mount.mount, cFd, uTimeBuf)
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
}

// 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)
}
65 changes: 65 additions & 0 deletions cephfs/file_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,68 @@ func TestFutime(t *testing.T) {
err = mount1.Futime(int(f1.fd), newTime)
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)
}
6 changes: 6 additions & 0 deletions docs/api-status.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.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"
}
]
},
Expand Down
1 change: 1 addition & 0 deletions docs/api-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.Futimes | $NEXT_RELEASE | $NEXT_RELEASE_STABLE |

## Package: cephfs/admin

Expand Down

0 comments on commit 88f56c2

Please sign in to comment.