Project

General

Profile

Bug #65043 ยป test.c

Sachin Prabhu, 03/21/2024 03:08 PM

 
/*
* gcc -o test -D_FILE_OFFSET_BITS=64 -lcephfs test.c
*/

#include <cephfs/libcephfs.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>

int ceph_set_time(struct ceph_mount_info *cmount, char *filename, time_t t)
{
struct ceph_statx stx = {0};
int fd, ret;
int mask = CEPH_STATX_BASIC_STATS|CEPH_STATX_BTIME;

stx.stx_mtime.tv_sec = t;
stx.stx_atime.tv_sec = t;
stx.stx_ctime.tv_sec = t;

printf("cephfs: Set time: %lu\n", t);
fd = ceph_open(cmount, filename, O_RDONLY, 0);
if(fd == -1) { printf("error open"); return 1;}
ret = ceph_fsetattrx(cmount, fd, &stx, mask);
//ret = ceph_futimens(cmount, fd, ts);
if(ret < 0) { printf("ceph_futimens error: %d\n", ret); };
ceph_close(cmount, fd);

return 0;
}

int ceph_get_time(struct ceph_mount_info *cmount, char *filename)
{
struct ceph_statx stx;
int fd;

fd = ceph_open(cmount, filename, O_RDONLY, 0);
ceph_fstatx(cmount, fd, &stx,CEPH_STATX_BASIC_STATS|CEPH_STATX_BTIME, 0);
printf("cephfs: Get time: %lu\n", stx.stx_mtime.tv_sec);
ceph_close(cmount, fd);

return 0;
}

int ceph_test_time(struct ceph_mount_info *cmount, char *filename, time_t t)
{
ceph_set_time(cmount, filename, t);
ceph_get_time(cmount, filename);
}

int local_set_time(char *filename, time_t t)
{
struct timespec ts[2] = {0,0};
int fd;

ts[0].tv_sec = t;
ts[1].tv_sec = t;

printf("local: Set time: %lu\n", t);
fd = open(filename, O_RDONLY);
if(fd == -1) { printf("error open"); return 1;}
if (futimens(fd, ts) == -1) { printf("error futimens: %s\n", strerror(errno)); return 1;}
close(fd);

return 0;
}

int local_get_time(char *filename)
{
struct stat stx;
int fd;

fd = open(filename, O_RDONLY, 0);
if(fd == -1) { printf("error open"); return 1;}
if (fstat(fd, &stx) == -1){ printf("error fstat: %s\n", strerror(errno)); return 1;}
printf("local: Get time: %lu\n", stx.st_mtime);
close(fd);

return 0;
}

int local_test_time(char *filename, time_t t)
{
local_set_time(filename, t);
local_get_time(filename);
}


int main()
{
struct ceph_mount_info *cmount = NULL;
char *filename = "testfile";
int fd;
char *teststr = "Hello World!";

ceph_create(&cmount, NULL);
ceph_conf_read_file(cmount, "/etc/ceph/ceph.conf");
ceph_mount(cmount, NULL);

printf("Ceph: Create a new file\n");
fd = ceph_open(cmount, filename, O_WRONLY|O_CREAT, 0644);
ceph_write(cmount, fd, teststr, sizeof(teststr), 0);
ceph_close(cmount, fd);
ceph_get_time(cmount, filename);
printf("\n");

printf("local: Create a new file\n");
fd = open(filename, O_WRONLY|O_CREAT, 06444);
write(fd, teststr, sizeof(teststr));
local_get_time(filename);
close(fd);
printf("\n");

printf("Set time to UINT32_MAX\n");
ceph_test_time(cmount, filename, (time_t)UINT32_MAX);
local_test_time(filename, (time_t)UINT32_MAX);
printf("\n");

printf("Set time to UINT32_MAX+1\n");
ceph_test_time(cmount, filename, (time_t)UINT32_MAX+1);
local_test_time(filename, (time_t)UINT32_MAX+1);
printf("\n");

printf("Set time to UINT32_MAX+2\n");
ceph_test_time(cmount, filename, (time_t)UINT32_MAX+2);
local_test_time(filename, (time_t)UINT32_MAX+2);
printf("\n");


ceph_shutdown(cmount);

return 0;
}
    (1-1/1)