小编典典
我可以使用两种不同的方法来修改ctime:
更改内核,使其ctime与mtime
编写一个简单(但很笨拙)的shell脚本。
第一种方法:更改内核。
在KERNEL_SRC/fs/attr.c “显式定义”时,此修改将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设置为该值。
第二种方法:简单(但很笨拙)的shell脚本。
简要说明:1)将系统时间更改为目标时间2)在文件上执行chmod,文件ctime现在反映了目标时间3)还原系统时间。
changectime.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权限。使用前,请确保根据需要修改此脚本。
2020-06-07