gpt4 book ai didi

macos - 在 OSX 上更改文件的 "date added"属性

转载 作者:行者123 更新时间:2023-12-05 06:41:41 24 4
gpt4 key购买 nike

我一直在使用终端更改文件的“添加日期”属性,但没有成功。

我正在做的是:

  1. 显示文件的属性:

    mdls 文件名

  2. 显示“添加日期”属性:

    xattr -p "kMDItemDateAdded"文件名

  3. 改变它:

    sudo xattr -w "kMDItemDateAdded""201111111111.11"文件名

第三条指令不起作用!而触摸具有相同日期值的文件更改修改日期。

我正在运行优胜美地。来自社区的任何想法?

最佳答案

感谢Ken Thomases above为我指出 setattrlist 的方向;以下代码适用于 macOS Monterey 12.5.1。

#include <stdlib.h>
#include <string.h>
#include <sys/attr.h>
#include <unistd.h>

/*
* Get kMDItemDateAdded of path.
*
* Returns:
* • 0 on success
* • 1 if a system call failed: check errno
* • 2 if something else went wrong
*/
int get_date_added(const char* path, struct timespec * out) {
attrgroup_t request_attrs = ATTR_CMN_RETURNED_ATTRS | ATTR_CMN_ADDEDTIME;

struct attrlist request;
memset(&request, 0, sizeof(request));
request.bitmapcount = ATTR_BIT_MAP_COUNT;
request.commonattr = request_attrs;

typedef struct {
u_int32_t length;
attribute_set_t returned;
struct timespec added;
} __attribute__((aligned(4), packed)) response_buf_t;

response_buf_t response;

int err = getattrlist(path, &request, &response, sizeof(response), 0);
if (err != 0) {
return 1;
}
if (response.length != sizeof(response)) {
// Need a different-sized buffer; but provided one of exactly required
// size?!
return 2;
}
if (response.returned.commonattr != request_attrs) {
// Didn’t get back all requested common attributes
return 2;
}

out->tv_sec = response.added.tv_sec;
out->tv_nsec = response.added.tv_nsec;

return 0;
}

/*
* Set kMDItemDateAdded of path.
*
* Returns:
* • 0 on success
* • 1 if a system call failed: check errno
*/
int set_date_added(const char* path, struct timespec in) {
attrgroup_t request_attrs = ATTR_CMN_ADDEDTIME;

struct attrlist request;
memset(&request, 0, sizeof(request));
request.bitmapcount = ATTR_BIT_MAP_COUNT;
request.commonattr = request_attrs;

typedef struct {
struct timespec added;
} __attribute__((aligned(4), packed)) request_buf_t;

request_buf_t request_buf;
request_buf.added.tv_sec = in.tv_sec;
request_buf.added.tv_nsec = in.tv_nsec;

int err = setattrlist(path, &request, &request_buf, sizeof(request_buf), 0);
if (err != 0) {
return 1;
}

return 0;
}

关于macos - 在 OSX 上更改文件的 "date added"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39685113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com