我能够使用两种不同的方法修改 ctime:
- 更改内核以
ctime
匹配mtime
- 编写一个简单(但很老套)的 shell 脚本。
第一种方法:更改内核。
我在此修改中只调整了几行,KERNEL_SRC/fs/attr.c
只要“明确定义”了 mtime,就会更新 ctime 以匹配 mtime。
有很多方法可以“显式定义”mtime,例如:
在 Linux 中:
touch -m --date="Wed Jun 12 14:00:00 IDT 2013" filename
在 Java 中(使用 Java 6 或 7,可能还有其他):
long newModificationTime = TIME_IN_MILLIS_SINCE_EPOCH;
File myFile = new File(myPath);
newmeta.setLastModified(newModificationTime);
KERNEL_SRC/fs/attr.c
这是函数中的更改notify_change
:
now = current_fs_time(inode->i_sb);
//attr->ia_ctime = now; (1) Comment this out
if (!(ia_valid & ATTR_ATIME_SET))
attr->ia_atime = now;
if (!(ia_valid & ATTR_MTIME_SET)) {
attr->ia_mtime = now;
}
else { //mtime is modified to a specific time. (2) Add these lines
attr->ia_ctime = attr->ia_mtime; //Sets the ctime
attr->ia_atime = attr->ia_mtime; //Sets the atime (optional)
}
(1) 未注释的这一行将在文件更改时将 ctime 更新为当前时钟时间。我们不希望这样,因为我们想自己设置 ctime。因此,我们将这一行注释掉。(这不是强制性的)
(2) 这才是真正解决问题的症结所在。该notify_change
函数在文件更改后执行,其中时间元数据需要更新。如果未指定 mtime,则将 mtime 设置为当前时间。否则,如果 mtime 设置为特定值,我们还将 ctime 和 atime 设置为该值。
第二种方法:简单(但 hacky)的 shell 脚本。
简要说明:
- 将系统时间更改为您的目标时间
- 对文件执行 chmod,文件 ctime 现在反映目标时间
- 恢复系统时间。
更改ctime.sh
#!/bin/sh
now=$(date)
echo $now
sudo date --set="Sat May 11 06:00:00 IDT 2013"
chmod 777 $1
sudo date --set="$now"
按如下方式运行:./changectime.sh MYFILE
文件的 ctime 现在将反映文件中的时间。
当然,您可能不想要具有 777 权限的文件。确保在使用之前根据需要修改此脚本。