DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ext4 Crtime (Creation Time) On Linux

08.25.2010
| 5991 views |
  • submit to reddit
        This is a very quick-and-dirty implementation of a simple script which allows you to access ext4's crtime on Linux ...

Might be useful, especially if your GNU coreuitls does not have this functionality available in its version of stat yet ...

!#/usr/bin/env ruby

DEBUGFS_BINARY='/sbin/debugfs'  # Change this when needed ...

STDERR.reopen(STDOUT)

STDOUT.sync = true
STDERR.sync = true

def die(message); puts message; exit(1); end

unless File.exists?(DEBUGFS_BINARY)
  die "#{$0}: #{DEBUGFS_BINARY} not found. Aborting ..."
end

if ARGV.length < 1 or ARGV.first == '-'
  die <<-EOS

Usage:

  #{$0} <FILE>

  Specify <FILE> for which you wish to display its crtime (Creation Time).

  Please note that this is for ext4 file system only ...

  EOS
end

file = ARGV.shift

unless File.exists?(file)
  die "#{$0}: file `#{file}' does not exists ..."
end

unless (Process.uid or Process.euid).zero?
  die "#{$0}: you have to be super-user to run this script ..."
end

device_lookup = Dir['/dev/*'].inject({}) { |h, v|
  h.update(File.stat(v).rdev => v)
}

stat = File.stat(file)

this_process = \
  "-R \"stat <#{stat.ino}>\" #{device_lookup[stat.dev].first} 2>/dev/null"

this_process = "#{DEBUGFS_BINARY} #{this_process}"

IO.popen(this_process).each do |p|
  (puts "#{file}: #{p.split('--').last.strip}"; break) if p.match(/^crtime/)
end

exit(0)

Please note that due to its dependence on the /dev file system to perform device lookup it may not be portable enough ...