我能够用两种不同的方法修改ctime:
>更改内核以使ctime与mtime匹配
>编写一个简单(但hacky)的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);
以下是notify_change函数中对KERNEL_SRC / fs / attr.c的更改:
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脚本.
简要说明:
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权限.确保在使用之前根据需要修改此脚本.