Я пишу код для управления временем файлов (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
Смущает время файлов Linux ⇐ Linux
1721661113
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
Подробнее здесь: [url]https://stackoverflow.com/questions/78779484/confused-by-linux-file-times[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия