嵌入式Linux系统编程 — 3.5 utime、utimes、futimens、utimensat函数修改文件时间属性

目录

1 文件的时间属性简介

2 utime()函数

2.1 utime()函数简介

2.2 示例程序

3 utimes()函数

3.1 utimes()函数简介

3.2 示例程序 

4  futimens()函数

4.1 futimens()函数简介

4.2 示例程序

5 utimensat()函数

5.1 utimensat()函数简介

5.2 示例程序


1 文件的时间属性简介

在Linux系统中,文件的时间属性通常与文件的状态信息一起存储在文件的inode中。在<sys/stat.h>头文件中,结构体struct stat用于存储文件的状态信息,包括时间属性。以下是struct stat中与时间属性相关的成员变量:

st_atime文件的最后访问时间(Access Time),访问指的是读取文件内容,文件内容最后一次被读取的时间。
st_mtime表示文件的最后修改时间(Modification Time),文件内容发生改变,譬如使用 write()函数写入数据到文件中。
st_ctime文件的最后状态改变时间(Status Change Time),状态更改指的是该文件的 inode 节点最后一次被修改的时间,譬如更改文件的访问权限、更改文件的用户 ID、用户组 ID、更改链接数等。

下面表列出了一些系统调用或 C 库函数对文件时间属性的影响,有些操作并不仅仅只会影响文件本身的时间属性,还会影响到其父目录的相关时间属性。

不同函数对文件时间属性的影响

2 utime()函数

2.1 utime()函数简介

utime()函数在Linux系统中用于设置文件的访问时间(access time)和修改时间(modification time)。这个函数允许你指定文件的访问时间和修改时间,而不是依赖系统自动更新的时间。函数原型如下:

#include <utime.h> 

int utime(const char *filename, const struct utimbuf *times);
  • filename:指定要修改时间属性的文件的路径。
  • times:指向struct utimbuf结构的指针,该结构包含了要设置的访问时间和修改时间。如果timesNULL,则系统将当前时间作为访问时间和修改时间。

struct utimbuf定义如下:

struct utimbuf { 
    time_t actime; // 访问时间 
    time_t modtime; // 修改时间 
};

该结构体中包含了两个 time_t 类型的成员,分别用于表示访问时间和内容修改时间, time_t 类型其实就是 long int 类型,所以这两个时间是以秒为单位的,所以由此可知, utime()函数设置文件的时间属性精度只能到秒。

同样对于文件来说,时间属性也是文件非常重要的属性之一,对文件时间属性的修改也不是任何用户都可以随便修改的, 只有以下两种进程可对其进行修改:

  • 超级用户进程(以 root 身份运行的进程) 。
  • 有效用户 ID 与该文件用户 ID(文件所有者)相匹配的进程。
  • 在参数 times 等于 NULL 的情况下,对文件拥有写权限的进程。

2.2 示例程序

下面的示例程序,接受一个命令行参数作为文件名,并尝试更新这个文件的时间属性:

#include <stdio.h>
#include <stdlib.h>
#include <utime.h>
#include <time.h>
#include <string.h>

int main(int argc, char *argv[]) {
    // 检查命令行参数数量
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    // 获取命令行参数中的文件名
    const char *filename = argv[1];

    // 获取当前时间
    time_t current_time = time(NULL);

    // 创建utimbuf结构体并设置时间
    struct utimbuf new_times;
    new_times.actime = new_times.modtime = current_time;

    // 使用utime()函数更新文件时间
    if (utime(filename, &new_times) == -1) {
        // 如果utime()调用失败,打印错误信息
        perror("Error updating file times");
        exit(EXIT_FAILURE);
    }

    printf("File '%s' access and modification times have been updated to: %s", filename, ctime(&current_time));

    return 0;
}

程序首先检查命令行参数的数量,如果参数数量不正确,程序将打印正确的用法并退出。进一步获取当前时间,设置utimbuf结构体,并调用utime()函数来更新文件的时间属性。如果utime()调用失败,程序将使用perror()打印错误消息并退出。如果调用成功,程序将打印一条消息,告知用户文件的时间属性已被更新。运行结果如下:

utimes()函数

3.1 utimes()函数简介

utimes()函数用于设置文件的访问时间(access time)和修改时间(modification time)的函数。与utime()函数不同,utimes()允许你指定更精确的时间,包括纳秒级别的精度。函数原型如下:

#include <sys/time.h> 

int utimes(const char *filename, const struct timeval times[2]);
  • filename:指定要修改时间属性的文件的路径。
  • times:指向包含两个struct timeval结构的数组的指针。第一个struct timeval用于设置访问时间,第二个用于设置修改时间。如果timesNULL,则系统将当前时间作为访问时间和修改时间。

struct timeval定义如下:

struct timeval { 
    time_t tv_sec; // 时间的秒部分 
    suseconds_t tv_usec; // 时间的微秒部分 
};

3.2 示例程序 

下面是一个使用utimes()函数的示例程序:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <filename> <time>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    const char *filename = argv[1];
    const char *time_str = argv[2];

    // 将字符串时间转换为timeval结构
    struct timeval new_times[2];
    if (sscanf(time_str, "%ld.%ld", &new_times[0].tv_sec, &new_times[0].tv_usec) != 2) {
        fprintf(stderr, "Invalid time format.\n");
        exit(EXIT_FAILURE);
    }
    new_times[1] = new_times[0]; // 访问时间和修改时间相同

    // 使用utimes()函数更新文件时间
    if (utimes(filename, new_times) == -1) {
        perror("Error updating file times");
        exit(EXIT_FAILURE);
    }

    printf("File '%s' access and modification times have been updated.\n", filename);

    return 0;
}

示例程序首先检查命令行参数的数量,确保用户提供了文件名和时间字符串。然后将时间字符串转换为timeval结构,这里假设时间字符串的格式为秒和微秒的组合,例如"1234567890.987654"。接着,我们使用utimes()函数来更新文件的访问时间和修改时间。如果转换失败或utimes()调用失败,程序将打印错误消息并退出。运行结果如下:

4  futimens()函数

除了上面给大家介绍了两个系统调用外,futimens()和 utimensat()函数功能与 utime()和 utimes()函数功能一样,用于显式修改文件时间戳,这两个系统调用相对于 utime 和 utimes 函数有以下三个优点:

  • 高精度时间设置futimens()utimensat()允许以纳秒为单位设置文件的时间戳,而utimes()只提供微秒级的精度,这是对时间设置精度的一个显著提升。

  • 单独设置时间戳:这两个系统调用可以独立地设置访问时间或修改时间,用户可以只更改其中一个时间戳而保持另一个不变。使用utime()utimes()时,如果要单独设置一个时间戳,需要先使用stat()获取另一个时间戳的当前值,然后再进行设置。

  • 灵活设置当前时间futimens()utimensat()可以单独将任一时间戳设置为当前时间,而使用utime()utimes()时,如果将times参数设置为NULL,则会将所有时间戳都设置为当前时间,无法只更新其中一个。

4.1 futimens()函数简介

futimens()函数是用于设置文件的时间属性,特别是针对已经打开的文件描述符。这个函数允许你为文件的访问时间(access time)和修改时间(modification time)设置精确到纳秒的时间戳。函数原型如下:

#include <sys/stat.h> 

int futimens(int fd, const struct timespec times[2]);
  • fd:文件描述符,是一个整数值,表示要设置时间属性的打开文件。
  • times:指向包含两个struct timespec结构的数组的指针。第一个struct timespec用于设置访问时间,第二个用于设置修改时间。如果timesNULL,则系统将当前时间作为访问时间和修改时间。

struct timespec定义如下:

struct timespec { 
    time_t tv_sec; // 时间的秒部分 
    long tv_nsec; // 时间的纳秒部分 
};

4.2 示例程序

下面是一个使用futimens()函数的示例程序:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>  // 包含time.h头文件

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    const char *filename = argv[1];
    int fd;

    // 打开文件以获取文件描述符
    fd = open(filename, O_RDONLY);
    if (fd == -1) {
        perror("Error opening file");
        exit(EXIT_FAILURE);
    }

    // 创建timespec结构体并设置当前时间
    struct timespec new_times[2];
    new_times[0].tv_sec = time(NULL);  // 获取当前时间的秒数
    new_times[0].tv_nsec = 0;          // 纳秒部分设置为0
    new_times[1] = new_times[0];       // 修改时间和访问时间设置为相同

    // 使用futimens()函数更新文件时间
    if (futimens(fd, new_times) == -1) {
        perror("Error updating file times");
        close(fd);
        exit(EXIT_FAILURE);
    }

    printf("File '%s' access and modification times have been updated.\n", filename);

    // 关闭文件描述符
    close(fd);

    return 0;
}

程序首先检查命令行参数的数量,确保用户提供了文件名。然后,我们使用open()函数以只读模式打开文件,并获取文件描述符fd。接着创建了两个timespec结构体实例,并将它们设置为当前时间。然后调用futimens()函数来更新文件的访问时间和修改时间。程序运行结果如下:

5 utimensat()函数

5.1 utimensat()函数简介

utimensat()函数允许以纳秒级的精度来设置文件或目录的时间戳。这个函数提供了比传统的utime()utimes()函数更高的时间设置精度,并且具有更多的灵活性。函数原型如下:

#include <sys/stat.h> 

int utimensat(int dirfd, const char *pathname, const struct timespec times[2], int flags);
  • dirfd:该参数可以是一个目录的文件描述符,也可以是特殊值 AT_FDCWD;如果 pathname 参数指定的是文件的绝对路径,则此参数会被忽略。
  • pathname:这是要修改时间戳的文件或目录的路径。
  • times:这是一个指向struct timespec数组的指针,包含两个时间戳,分别用于设置访问时间(times[0])和修改时间(times[1])。如果timesNULL,则时间戳将被设置为当前时间。
  • flags:这是一些标志位,可以是以下值的组合:
    • AT_SYMLINK_NOFOLLOW:如果pathname是一个符号链接,则utimensat()将修改链接指向的目标而不是链接本身。

5.2 示例程序

下面的使用utimensat()函数示例程序接受一个文件名和一个时间字符串作为命令行参数,并尝试将该文件的访问和修改时间设置为指定的时间。时间字符串的格式应该是秒.纳秒

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <filename> <timestamp>\n", argv[0]);
        fprintf(stderr, "Timestamp format: seconds.nanoseconds\n");
        exit(EXIT_FAILURE);
    }

    const char *filename = argv[1];
    const char *timestamp_str = argv[2];
    char *dot;
   unsigned long seconds, nanoseconds;

    // 解析时间字符串
    seconds = strtoul(timestamp_str, &dot, 10);
    if (*dot != '.') {
        fprintf(stderr, "Invalid timestamp format.\n");
        exit(EXIT_FAILURE);
    }
    // 跳过点号,解析纳秒部分
    nanoseconds = strtoul(dot + 1, NULL, 10);

    // 创建timespec结构体
    struct timespec new_times[2];
    new_times[0].tv_sec = seconds;  // 访问时间
    new_times[0].tv_nsec = nanoseconds;  // 访问时间的纳秒部分
    new_times[1].tv_sec = seconds;  // 修改时间
    new_times[1].tv_nsec = nanoseconds;  // 修改时间的纳秒部分

    // 使用utimensat()函数更新文件时间
    if (utimensat(AT_FDCWD, filename, new_times, 0) == -1) {
        perror("Error updating file times");
        exit(EXIT_FAILURE);
    }

    printf("File '%s' access and modification times have been updated to: %lu.%09lu\n",
           filename, seconds, nanoseconds);

    return 0;
}

程序首先检查命令行参数的数量是否正确。接,我们解析时间字符串以获取秒和纳秒部分,然后创建timespec结构体并设置相应的时间。最后使用utimensat()函数尝试更新文件的访问和修改时间。如果utimensat()调用失败,程序将打印错误消息并退出。运行结果如下:

 

  • 33
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

几度春风里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值