Skip to contentSkip to conversation

lsattr: list file creation time

PATCH Applied Li Zefan 1 participant 1 message Aug 6, 2008

lore
Share & tools
lore

Li Zefan

author
#0 lore raw

PATCH 3/3 lsattr: list file creation time

Add option '-c' to list files' creation time:

$ lsattr -c tmp
2008-08-06 15:36:24.762780702 +0800 -------------e- tmp
---
 lib/e2p/Makefile.in  |    4 +-
 lib/e2p/e2p.h        |    2 +-
 lib/e2p/fgetcrtime.c |   67 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/ext2fs/ext2_fs.h |    1 +
 misc/lsattr.1.in     |    3 ++
 misc/lsattr.c        |   24 +++++++++++++++++-
 6 files changed, 97 insertions(+), 4 deletions(-)
 create mode 100644 lib/e2p/fgetcrtime.c
lib/e2p/Makefile.in +2 -2
index 6557480..4df3b9a 100644
@@ -19,7 +19,7 @@ all::	e2p.pc
 OBJS=		feature.o fgetflags.o fsetflags.o fgetversion.o fsetversion.o \
 		getflags.o getversion.o hashstr.o iod.o ls.o mntopts.o \
 		parse_num.o pe.o pf.o ps.o setflags.o setversion.o uuid.o \
-		ostype.o percent.o
+		ostype.o percent.o fgetcrtime.o
 
 SRCS=		$(srcdir)/feature.c $(srcdir)/fgetflags.c \
 		$(srcdir)/fsetflags.c $(srcdir)/fgetversion.c \
@@ -28,7 +28,7 @@ SRCS=		$(srcdir)/feature.c $(srcdir)/fgetflags.c \
 		$(srcdir)/ls.c $(srcdir)/mntopts.c $(srcdir)/parse_num.c \
 		$(srcdir)/pe.c $(srcdir)/pf.c $(srcdir)/ps.c \
 		$(srcdir)/setflags.c $(srcdir)/setversion.c $(srcdir)/uuid.c \
-		$(srcdir)/ostype.c $(srcdir)/percent.c
+		$(srcdir)/ostype.c $(srcdir)/percent.c $(srcdir)/fgetcrtime.c
 HFILES= e2p.h
 
 LIBRARY= libe2p
lib/e2p/e2p.h +1 -1
index 06e2120..644248a 100644
@@ -15,9 +15,9 @@
 
 #define PFOPT_LONG  1 /* Must be 1 for compatibility with `int long_format'. */
 
-
 int fgetflags (const char * name, unsigned long * flags);
 int fgetversion (const char * name, unsigned long * version);
+int fgetcrtime (const char * name, struct timespec * crtime);
 int fsetflags (const char * name, unsigned long flags);
 int fsetversion (const char * name, unsigned long version);
 int getflags (int fd, unsigned long * flags);
lib/e2p/fgetcrtime.c new file +67 -0

67 lines not on this page · raw message

lib/ext2fs/ext2_fs.h +1 -0
index 554c708..55a56f6 100644
@@ -316,6 +316,7 @@ struct ext4_new_group_input {
 #define EXT2_IOC_GROUP_EXTEND		_IOW('f', 7, unsigned long)
 #define EXT2_IOC_GROUP_ADD		_IOW('f', 8,struct ext2_new_group_input)
 #define EXT4_IOC_GROUP_ADD		_IOW('f', 8,struct ext4_new_group_input)
+#define EXT4_IOC_GETCRTIME		_IOR('f', 9, struct timespec)
 
 /*
  * Structure of an inode on the disk
misc/lsattr.1.in +3 -0
index 7798a34..e733b68 100644
@@ -26,6 +26,9 @@ Display the program version.
 .B \-a
 List all files in directories, including files that start with `.'.
 .TP
+.B \-c
+List the file's creation time.
+.TP
 .B \-d
 List directories like other files, rather than listing their contents.
 .TP
misc/lsattr.c +23 -1
index 093f50f..63ce5c1 100644
@@ -37,6 +37,7 @@ extern char *optarg;
 #include <string.h>
 #include <sys/param.h>
 #include <sys/stat.h>
+#include <time.h>
 
 #include "ext2fs/ext2_fs.h"
 #include "et/com_err.h"
@@ -59,6 +60,7 @@ static unsigned pf_options;
 static int recursive;
 static int verbose;
 static int generation_opt;
+static int crtime_opt;
 
 #ifdef _LFS64_LARGEFILE
 #define LSTAT		lstat64
@@ -78,12 +80,29 @@ static void list_attributes (const char * name)
 {
 	unsigned long flags;
 	unsigned long generation;
+	struct timespec crtime;
+	struct tm *tm;
+	char str[1024];
+	size_t p;
 
 	if (fgetflags (name, &flags) == -1) {
 		com_err (program_name, errno, _("While reading flags on %s"),
 			 name);
 		return;
 	}
+	if (crtime_opt) {
+		if (fgetcrtime (name, &crtime) == -1) {
+			com_err (program_name, errno,
+				 _("While reading creation time on %s"),
+				 name);
+			return;
+		}
+		tm = localtime (&crtime.tv_sec);
+		p = strftime (str, 1024, "%Y-%m-%d %H:%M:%S", tm);
+		p += sprintf (str+p, ".%ld", crtime.tv_nsec);
+		strftime (str+p, 1024-p, " %z", tm);
+		printf ("%s ", str);
+	}
 	if (generation_opt) {
 		if (fgetversion (name, &generation) == -1) {
 			com_err (program_name, errno,
@@ -164,7 +183,7 @@ int main (int argc, char ** argv)
 #endif
 	if (argc && *argv)
 		program_name = *argv;
-	while ((c = getopt (argc, argv, "RVadlv")) != EOF)
+	while ((c = getopt (argc, argv, "RVadlvc")) != EOF)
 		switch (c)
 		{
 			case 'R':
@@ -185,6 +204,9 @@ int main (int argc, char ** argv)
 			case 'v':
 				generation_opt = 1;
 				break;
+			case 'c':
+				crtime_opt = 1;
+				break;
 			default:
 				usage();
 		}
Signature · 1 line
1.5.4.rc3