Смущает время файлов LinuxLinux

Ответить Пред. темаСлед. тема
Anonymous
 Смущает время файлов Linux

Сообщение Anonymous »

Я пишу код для управления временем файлов (atime, ctime и mtime) для файловой системы LittleFS. В моем желании сделать это чем-то вроде Linux/Posix я столкнулся с вопросами относительно того, когда эти времена устанавливаются в жизненном цикле. Такие вопросы, как время обновляется, когда вы открываете файл, читаете запись или закрываете этот файл. Чтобы ответить на эти вопросы, я запустил свою подсистему WSL и написал быстрый тест. Весьма удивлён результатами до такой степени, что усомнился в моём понимании реальности. Во время выполнения программы время вообще не менялось. Статистика кэшируется? Как их обновить?
Вот тестовая программа:
#include
#include
#include
#include
#include
#include
#include
#include

char* mytime(const time_t *timep)
{
struct tm *tm = localtime(timep);
static char text[20];
sprintf(text, "%d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
return text;
}

void test_stat(const char *filename)
{
printf("Stat of %s ", filename);
struct stat statbuf = { 0 };
int rc = stat(filename, &statbuf);
if (rc != 0)
{
printf("stat rc=%d, errno=%d, %s\n", rc, errno, strerror(errno));
return;
}
printf("st_atime=%s, st_ctime=%s, st_mtime=%s\n",
mytime(&statbuf.st_atime),
mytime(&statbuf.st_ctime), mytime(&statbuf.st_mtime));
}

int main(int argc, char *argv)
{
static const char *filename = "testfile";
static int perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;

test_stat(filename);

int fh = open(filename, 0, perm);
if (fh < 0)
{
printf("open rc=%d, errno=%d, %s\n", fh, errno, strerror(errno));
}
else
{
test_stat(filename);
}

printf("Open Create\n");
fh = open(filename, O_CREAT, perm);
if (fh < 0)
{
printf("open O_CREAT rc=%d, errno=%d, %s\n", fh, errno,
strerror(errno));
}
else
{
test_stat(filename);
}

printf("Write\n");
char *buffer = "1234567890";
write(fh, buffer, strlen(buffer));
test_stat(filename);

printf("Close fh=%d\n", fh);
int rc = close(fh);
if (rc < 0)
{
printf("close rc=%d, errno=%d, %s\n", rc, errno, strerror(errno));
}

sleep(2);

test_stat(filename);

sleep(2);

printf("Reopen, O_RDWR\n");
fh = open(filename, O_RDWR);
if (fh < 0)
{
printf("open rc=%d, errno=%d, %s\n", fh, errno, strerror(errno));
}
else
{
test_stat(filename);
}

printf("Write more\n");
write(fh, buffer, strlen(buffer));
test_stat(filename);

printf("Close\n");
rc = close(fh);
if (fh < 0)
{
printf("close rc=%d, errno=%d, %s\n", fh, errno, strerror(errno));
}

test_stat(filename);

sleep(2);

printf("Reopen, O_WRONLY\n");
fh = open(filename, O_WRONLY);
if (fh < 0)
{
printf("open rc=%d, errno=%d, %s\n", fh, errno, strerror(errno));
}
else
{
test_stat(filename);
}

printf("Write more\n");
write(fh, buffer, strlen(buffer));
test_stat(filename);

printf("Close\n");
rc = close(fh);
if (fh < 0)
{
printf("close rc=%d, errno=%d, %s\n", fh, errno, strerror(errno));
}

test_stat(filename);

sleep(2);
}

И результаты:
waynej@SV3273:~/testTimes$ rm -f testfile; cc -o testTimes testTimes.c && ./testTimes
Stat of testfile stat rc=-1, errno=2, No such file or directory
open rc=-1, errno=2, No such file or directory
Open Create
Stat of testfile st_atime=10:06:48, st_ctime=10:06:48, st_mtime=10:06:48
Write
Stat of testfile st_atime=10:06:48, st_ctime=10:06:48, st_mtime=10:06:48
Close fh=3
Stat of testfile st_atime=10:06:48, st_ctime=10:06:48, st_mtime=10:06:48
Reopen, O_RDWR
Stat of testfile st_atime=10:06:48, st_ctime=10:06:48, st_mtime=10:06:48
Write more
Stat of testfile st_atime=10:06:48, st_ctime=10:06:48, st_mtime=10:06:48
Close
Stat of testfile st_atime=10:06:48, st_ctime=10:06:48, st_mtime=10:06:48
Reopen, O_WRONLY
Stat of testfile st_atime=10:06:48, st_ctime=10:06:48, st_mtime=10:06:48
Write more
Stat of testfile st_atime=10:06:48, st_ctime=10:06:48, st_mtime=10:06:48
Close
Stat of testfile st_atime=10:06:48, st_ctime=10:06:48, st_mtime=10:06:48


Подробнее здесь: https://stackoverflow.com/questions/787 ... file-times
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Linux»