Skip to content

Commit

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

Fixes: #252

Signed-off-by: Nikhil-Ladha <[email protected]>
  • Loading branch information
Nikhil-Ladha committed Apr 24, 2023
1 parent bc48ab4 commit 5d1d02e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cephfs/file_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,31 @@ func (mount *MountInfo) Mknod(path string, mode uint16, dev uint16) error {
ret := C.ceph_mknod(mount.mount, cPath, C.mode_t(mode), C.dev_t(dev))
return getError(ret)
}

// Utime struct is the equivalent of C.struct_utimbuf
type Utime struct {
// AcTime represents the file's access time in seconds since the Unix epoch.
AcTime int64
// ModTime represents the file's modification time in seconds since the Unix epoch.
ModTime int64
}

// Futime changes file/directory last access and modification times.
//
// Implements:
//
// int ceph_futime(struct ceph_mount_info *cmount, int fd, struct utimbuf *buf);
func (mount *MountInfo) Futime(fd int, times *Utime) error {
if err := mount.validate(); err != nil {
return err
}

cFd := C.int(fd)
uTimeBuf := &C.struct_utimbuf{
actime: C.time_t(times.AcTime),
modtime: C.time_t(times.ModTime),
}

ret := C.ceph_futime(mount.mount, cFd, uTimeBuf)
return getError(ret)
}
39 changes: 39 additions & 0 deletions cephfs/file_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package cephfs

import (
"os"
"syscall"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -52,3 +54,40 @@ func TestMknod(t *testing.T) {
err = mount1.Mknod(file4, uint16(syscall.S_IFCHR), 64)
assert.Error(t, err)
}

func TestFutime(t *testing.T) {
mount := fsConnect(t)
defer fsDisconnect(t, mount)

fname := "futime_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))
}()

currentTime := Timespec{int64(time.Now().Second()), 0}
newTime := &Utime{
AcTime: currentTime.Sec,
ModTime: currentTime.Sec,
}
err = mount.Futime(int(f1.fd), newTime)
assert.NoError(t, err)

sx, err := mount.Statx(fname, StatxBasicStats, 0)
assert.NoError(t, err)
assert.Equal(t, currentTime, sx.Atime)
assert.Equal(t, currentTime, sx.Mtime)

// Test invalid mount value
mount1 := &MountInfo{}
currentTime = Timespec{int64(time.Now().Second()), 0}
newTime = &Utime{
AcTime: currentTime.Sec,
ModTime: currentTime.Sec,
}
err = mount1.Futime(int(f1.fd), newTime)
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 @@ -331,6 +331,12 @@
"comment": "Mknod creates a regular, block or character special file.\n\nImplements:\n\n\tint ceph_mknod(struct ceph_mount_info *cmount, const char *path, mode_t mode,\n\t\t\t\t dev_t rdev);\n",
"added_in_version": "$NEXT_RELEASE",
"expected_stable_version": "$NEXT_RELEASE_STABLE"
},
{
"name": "MountInfo.Futime",
"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"
}
]
},
Expand Down
1 change: 1 addition & 0 deletions docs/api-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Name | Added in Version | Expected Stable Version |
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 |

## Package: cephfs/admin

Expand Down

0 comments on commit 5d1d02e

Please sign in to comment.