设置/改变ctime或“更改时间”。属性在文件上 [英] Setting/changing the ctime or "Change time" attribute on a file

查看:385
本文介绍了设置/改变ctime或“更改时间”。属性在文件上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用 java.nio.Files 类更改Java中文件的时间戳元数据。

I wish to change the timestamp metadata on files in Java using the java.nio.Files class.

我想更改所有3个Linux / ext4时间戳(最后修改,访问和更改)。

I would like to change all 3 Linux/ext4 timestamps (last modified, access, and changed).

我可以更改前两个时间戳字段,如下所示:

I am able to change the first two timestamp fields as follows:

Files.setLastModifiedTime(pathToMyFile, myCustomTime);
Files.setAttribute(pathToMyFile, "basic:lastAccessTime", myCustomTime);

但是,我无法修改文件上的最后更改:时间。另外,关于文档。最接近的可用属性是 creationTime ,我试过没有任何成功。

However, I am unable modify the last Change: time on the file. Also, it is concerning that there is no change timestamp mentioned in the documentation. The closest available attribute is creationTime, which I tried without any success.

关于如何修改<的任何想法code>根据Java中的自定义时间戳更改:文件的元数据?

Any ideas on how to modify the Change: metadata for a file according to a custom timestamp in Java?

谢谢!

推荐答案

我能够用两种不同的方法修改ctime:

I was able to modify the ctime with two different methods:


  1. 更改内核以便 ctime 匹配 mtime

  2. 写作一个简单的(但是hacky)shell脚本。

  1. Changing the kernel so that ctime matches the mtime
  2. Writing a simple (but hacky) shell script.

第一种方法:更改内核。

我在中调整了几行KERNEL_SRC / fs / attr.c 这个修改更新了ctime,以便在mtime时匹配mtime是明确定义的。

I tweaked just a few lines in KERNEL_SRC/fs/attr.c This modification updates the ctime to match the mtime whenever the mtime is "explicitly defined."

有很多方法可以明确定义mtime,例如:

There are many ways to "explicitly define" the the mtime, for example:

在Linux中:

touch -m --date="Wed Jun 12 14:00:00 IDT 2013" filename

在Java中(使用Java 6或7,可能还有其他人):

In Java (using Java 6 or 7, and presumably others):

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。因此,我们评论这一行。 (这不是强制性的)

(1) This line, uncommented, would update the ctime to the current clock time upon a change to the file. We don't want that, since we want to set the ctime ourselves. Thus, we comment this line out. (This isn't mandatory)

(2)这实际上是解决方案的关键。 notify_change 函数在文件更改后执行,其中需要更新时间元数据。如果未指定mtime,则将mtime设置为当前时间。否则,如果将mtime设置为特定值,我们还将ctime和atime设置为该值。

(2) This is really the crux of the solution. The notify_change function is executed after a file has been changed, where the time metadata needs to be updated. If no mtime was specified, then the mtime is set to the current time. Else, if the mtime was set to a specific value, we also set the ctime and the atime to that value.

第二种方法:简单(但是hacky) shell脚本。

简要说明:
1)将系统时间更改为目标时间
2)执行chmod文件,文件ctime现在反映目标时间
3)恢复系统时间。

Brief explanation: 1) Change the system time to your target time 2) Perform a chmod on the file, file ctime now reflects target time 3) Revert the system time back.

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

Run this as follows: ./changectime.sh MYFILE

文件的ctime现在将反映出来文件中的时间。

The file's ctime will now reflect the time in the file.

当然,您可能不希望该文件具有777权限。确保在使用之前根据需要修改此脚本。

Of course, you probably don't want the file with 777 permissions. Ensure that you modify this script to your needs before using it.

这篇关于设置/改变ctime或“更改时间”。属性在文件上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆