Setting/changing the ctime or "Change time" attribute on a file
Asked Answered
N

2

11

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

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);

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.

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

Thank you!

Nan answered 21/4, 2013 at 1:12 Comment(3)
In Linux terminal, for ls output, it shows the last modified time, did you try modifying that?Joashus
Yes. I successfully modified last modified time using the first line of code above.Nan
There is no "change timestamp" because ctime is (supposed to be) the creation time of a file, and not the time it was changed ("lastModifiedTime" is when it was last modified or "changed")Ileneileo
N
19

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

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

1st Method: Changing the kernel.

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."

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

In Linux:

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

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);

Here is the change to KERNEL_SRC/fs/attr.c in the notify_change function:

    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) 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) 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.

2nd method: Simple (but hacky) shell script.

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"

Run this as follows: ./changectime.sh MYFILE

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

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

Nan answered 12/6, 2013 at 13:5 Comment(2)
Need to change script. now=$(date) doesn't (always?) get valid date format for setting. Use: now=$(date +"%Y-%M-%d %T"). That script/method is neat and it works for me.Abortifacient
Nice workaround! .. for my current use case though, I'll just mirror the original partition to the new target drive, than enlarge the filesystem – until I find the time to engage in kernel modding shenanigans.. maybe by that time, someone has come up with a proper syscall instead of this madness 😁..Ahern
A
3

Adapting this answer to your case:

// Warning: Disk must be unmounted before this operation
String disk = "/dev/sda1";
// Update ctime
Runtime.getRuntime().exec("debugfs -w -R 'set_inode_field "+pathToMyFile+" ctime "+myCustomTime+"' "+disk);
// Drop vm cache so ctime update is reflected
Runtime.getRuntime().exec("echo 2 > /proc/sys/vm/drop_caches");

I doubt we will see a convenient method in Standard Java API to do this, as neither Linux (man touch) nor Windows (GetFileTime function on MSDN) give easy access to this field. Native system calls give only access to creation/access/modify timestamps, so does Java.

Ambler answered 21/4, 2013 at 2:48 Comment(1)
Is there an alternate way which allows me to keep the disk mounted?Nan

© 2022 - 2024 — McMap. All rights reserved.