2

For a utility I'm developing it would be expedient if I could set all three the named dates associated with the files in some directory to some date in the past.

I know I can use touch to set the access & modify dates, but I need all three to be the same (used as reference-point/date).

Is there a way I can do this (root-access ok)

Edit:

  1. I found this: http://blog.commandlinekungfu.com/2010/02/episode-80-time-bandits.html which proposes (for ext2/3/4 only):

    # debugfs -w -R 'set_inode_field /pathtofile ctime 200901010101' filedevice

where '200901010101' is a sample date-time; and this https://stackoverflow.com/a/5518031/15161 to find the 'filedevice' needed above:

df /pathtofile | awk 'NR == 2 {print $1}'

I have tried this on a test-file but the debugfs-call took too long and I Ctrl-C'd it. (So I don't really know if this will work...)

  1. Another idea is possibly to use a VM with system-date set to the past date I want, but I don't know how to set that up.

  2. This relates to (2) but does not use a VM, from http://www.shellhacks.com/en/Faking-a-Files-Access-Modify-and-Change-TimeStamps-in-Linux :

As a possible workaround you can set the system time to the ctime you want to impose, then touch the file and then restore the system time.

NOW=$(date) && date -s "2030-08-15 21:30:11" && touch file.txt && date -s "$NOW"

but I'm not sure how safe/risky this would be.

  • See if this helps. To Gilles point, anything you do to change that would be a 'hack' to get it working. I think the better question here would be, why do you want to force change the file's 'change' date as well. What are you hoping to gain / accomplish? – devnull Dec 28 '15 at 12:36
  • What is the baseline you are trying to check? From what it sounds like a database sounds like the better choice for you anyway, if your fear is not having the overhead of a 'database' like mysql or postgres, you can just do something simpler like flat file database where you can stamp it with your arbitrary date and whatever after you are trying to compare against. – devnull Dec 28 '15 at 13:09
2

You can't change the ctime without “cheating”, and that requires root. See How can I change 'change' date of file?

Since you're doing testing, I'd go with the VM approach. Run your program in a virtual machine. You can set the system date inside the virtual machine to whatever you like (just remember not to set up NTP).

Alternatively, if you're testing an application (as opposed to a whole system), you could fake its file time readings through LD_PRELOAD (similar example) or ptrace (more difficult, but works even against statically linked binaries). This isn't appropriate to all scenarios, it really depends what kind of potential bugs you're testing against. libfaketime lets you run a program and make it believe that it's running at a different time; it can also remember faked timestamps, so you may be able to leverage it and not have to write any extra low-level code.

  • My gutfeel is also the VM-route - I'm not very clued-up with VM's, KVM&co specifically - any pointers? links? Ideal would be an "up-n-down" VM that can be started, do the time-stamping and then die - gimf, i know, but knowing what to query for is the bummer – slashmais Dec 29 '15 at 7:49
  • @slashmais Once again it depends how your test scenarios need to run, but a the following strategy should work: prepare a VM image with DHCP and SSH access, and to run a test, clone it and boot it then set the date and run your application and your test code. I think VirtualBox is the easiest to set up but KVM isn't far behind and may be easier to control automatically. I don't have a particular tutorial to recommend. – Gilles Dec 29 '15 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.