对 Linux 文件时代感到困惑

问题描述 投票:0回答:1

我正在编写一些代码来管理 LittleFS 文件系统的文件时间(atime、ctime 和 mtime)。 在我想让这有点像 Linux/Posix 的过程中,我遇到了关于这些时间何时在生命周期中设置的问题。 当您打开文件、读取记录或关闭该文件时,诸如时间是否更新之类的问题。 为了回答这些问题,我启动了 WSL 子系统并编写了一个快速测试。 对结果感到非常惊讶,甚至质疑我对现实的把握。 在程序执行期间时间根本没有改变。 统计数据是否被缓存? 怎么才能刷新呢?

这是测试程序:

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

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
linux timestamp filesystems
1个回答
0
投票
printf("st_atime=%s, st_ctime=%s, st_mtime=%s\n",
           mytime(&statbuf.st_atime),
           mytime(&statbuf.st_ctime), 
           mytime(&statbuf.st_mtime));

现在向离您最近的橡皮鸭解释一下,当

printf
返回时,您期望 mytime 如何打印
三个不同的字符串

static char text[20];

每一次

这就是你的问题。不要使用静态变量。他们会咬你。

© www.soinside.com 2019 - 2024. All rights reserved.