1997-04-29 21:48:10 +04:00
|
|
|
/*
|
|
|
|
* ls.c --- list directories
|
2008-08-28 07:07:54 +04:00
|
|
|
*
|
1997-04-29 21:48:10 +04:00
|
|
|
* Copyright (C) 1997 Theodore Ts'o. This file may be redistributed
|
|
|
|
* under the terms of the GNU Public License.
|
|
|
|
*/
|
|
|
|
|
2011-09-19 01:34:37 +04:00
|
|
|
#include "config.h"
|
1997-04-29 21:48:10 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
2002-01-03 12:55:25 +03:00
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
2008-08-28 07:07:54 +04:00
|
|
|
#else
|
2002-01-03 12:55:25 +03:00
|
|
|
extern int optind;
|
|
|
|
extern char *optarg;
|
|
|
|
#endif
|
1997-04-29 21:48:10 +04:00
|
|
|
|
|
|
|
#include "debugfs.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* list directory
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LONG_OPT 0x0001
|
2012-10-14 14:34:09 +04:00
|
|
|
#define PARSE_OPT 0x0002
|
1997-04-29 21:48:10 +04:00
|
|
|
|
|
|
|
struct list_dir_struct {
|
|
|
|
FILE *f;
|
|
|
|
int col;
|
|
|
|
int options;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *monstr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
2008-08-28 07:07:54 +04:00
|
|
|
|
2003-12-07 09:28:50 +03:00
|
|
|
static int list_dir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
|
2002-01-03 12:55:25 +03:00
|
|
|
int entry,
|
|
|
|
struct ext2_dir_entry *dirent,
|
2003-12-07 09:28:50 +03:00
|
|
|
int offset EXT2FS_ATTR((unused)),
|
|
|
|
int blocksize EXT2FS_ATTR((unused)),
|
|
|
|
char *buf EXT2FS_ATTR((unused)),
|
2002-01-03 12:55:25 +03:00
|
|
|
void *private)
|
1997-04-29 21:48:10 +04:00
|
|
|
{
|
|
|
|
struct ext2_inode inode;
|
2002-01-03 12:55:25 +03:00
|
|
|
ext2_ino_t ino;
|
1997-04-29 21:48:10 +04:00
|
|
|
struct tm *tm_p;
|
|
|
|
time_t modtime;
|
2007-03-21 22:09:15 +03:00
|
|
|
char name[EXT2_NAME_LEN + 1];
|
2002-01-03 12:55:25 +03:00
|
|
|
char tmp[EXT2_NAME_LEN + 16];
|
1997-04-29 21:48:10 +04:00
|
|
|
char datestr[80];
|
2002-01-03 12:55:25 +03:00
|
|
|
char lbr, rbr;
|
|
|
|
int thislen;
|
1997-04-29 21:48:10 +04:00
|
|
|
struct list_dir_struct *ls = (struct list_dir_struct *) private;
|
2012-10-14 14:34:09 +04:00
|
|
|
struct ext2_dir_entry_tail *t = (struct ext2_dir_entry_tail *) dirent;
|
1997-04-29 21:48:10 +04:00
|
|
|
|
2013-06-08 19:26:14 +04:00
|
|
|
thislen = ext2fs_dirent_name_len(dirent);
|
1997-04-29 21:48:10 +04:00
|
|
|
strncpy(name, dirent->name, thislen);
|
|
|
|
name[thislen] = '\0';
|
2002-01-03 12:55:25 +03:00
|
|
|
ino = dirent->inode;
|
|
|
|
|
|
|
|
if (entry == DIRENT_DELETED_FILE) {
|
|
|
|
lbr = '<';
|
|
|
|
rbr = '>';
|
|
|
|
ino = 0;
|
|
|
|
} else {
|
|
|
|
lbr = rbr = ' ';
|
|
|
|
}
|
2008-01-04 23:28:51 +03:00
|
|
|
if (ls->options & PARSE_OPT) {
|
2011-09-25 08:49:58 +04:00
|
|
|
if (ino) {
|
|
|
|
if (debugfs_read_inode(ino, &inode, name))
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
memset(&inode, 0, sizeof(struct ext2_inode));
|
2008-01-04 23:28:51 +03:00
|
|
|
fprintf(ls->f,"/%u/%06o/%d/%d/%s/",ino,inode.i_mode,inode.i_uid, inode.i_gid,name);
|
|
|
|
if (LINUX_S_ISDIR(inode.i_mode))
|
|
|
|
fprintf(ls->f, "/");
|
|
|
|
else
|
2011-07-09 03:54:57 +04:00
|
|
|
fprintf(ls->f, "%lld/", EXT2_I_SIZE(&inode));
|
2008-01-04 23:28:51 +03:00
|
|
|
fprintf(ls->f, "\n");
|
2011-09-25 08:49:58 +04:00
|
|
|
} else if (ls->options & LONG_OPT) {
|
2002-01-03 12:55:25 +03:00
|
|
|
if (ino) {
|
|
|
|
if (debugfs_read_inode(ino, &inode, name))
|
2002-01-03 13:10:04 +03:00
|
|
|
return 0;
|
2002-01-03 12:55:25 +03:00
|
|
|
modtime = inode.i_mtime;
|
|
|
|
tm_p = localtime(&modtime);
|
|
|
|
sprintf(datestr, "%2d-%s-%4d %02d:%02d",
|
|
|
|
tm_p->tm_mday, monstr[tm_p->tm_mon],
|
|
|
|
1900 + tm_p->tm_year, tm_p->tm_hour,
|
|
|
|
tm_p->tm_min);
|
|
|
|
} else {
|
|
|
|
strcpy(datestr, " ");
|
|
|
|
memset(&inode, 0, sizeof(struct ext2_inode));
|
|
|
|
}
|
2012-10-14 14:34:09 +04:00
|
|
|
fprintf(ls->f, "%c%6u%c %6o ", lbr, ino, rbr, inode.i_mode);
|
|
|
|
if (entry == DIRENT_CHECKSUM) {
|
|
|
|
fprintf(ls->f, "(dirblock checksum: 0x%08x)\n",
|
|
|
|
t->det_checksum);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-08 19:26:14 +04:00
|
|
|
fprintf(ls->f, "(%d) %5d %5d ",
|
|
|
|
ext2fs_dirent_file_type(dirent),
|
2007-05-08 08:10:54 +04:00
|
|
|
inode_uid(inode), inode_gid(inode));
|
2002-01-03 12:55:25 +03:00
|
|
|
if (LINUX_S_ISDIR(inode.i_mode))
|
|
|
|
fprintf(ls->f, "%5d", inode.i_size);
|
|
|
|
else
|
2011-07-09 03:54:57 +04:00
|
|
|
fprintf(ls->f, "%5llu", EXT2_I_SIZE(&inode));
|
2002-01-03 12:55:25 +03:00
|
|
|
fprintf (ls->f, " %s %s\n", datestr, name);
|
|
|
|
} else {
|
2012-10-14 14:34:09 +04:00
|
|
|
if (entry == DIRENT_CHECKSUM)
|
|
|
|
sprintf(tmp, "%c%u%c (dirblock checksum: 0x%08x) ",
|
|
|
|
lbr, dirent->inode, rbr, t->det_checksum);
|
|
|
|
else
|
|
|
|
sprintf(tmp, "%c%u%c (%d) %s ",
|
|
|
|
lbr, dirent->inode, rbr,
|
|
|
|
dirent->rec_len, name);
|
2002-01-03 12:55:25 +03:00
|
|
|
thislen = strlen(tmp);
|
|
|
|
|
|
|
|
if (ls->col + thislen > 80) {
|
|
|
|
fprintf(ls->f, "\n");
|
|
|
|
ls->col = 0;
|
|
|
|
}
|
|
|
|
fprintf(ls->f, "%s", tmp);
|
|
|
|
ls->col += thislen;
|
|
|
|
}
|
1997-04-29 21:48:10 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_list_dir(int argc, char *argv[])
|
|
|
|
{
|
2001-01-11 18:26:39 +03:00
|
|
|
ext2_ino_t inode;
|
|
|
|
int retval;
|
2002-01-03 12:55:25 +03:00
|
|
|
int c;
|
2012-10-14 14:34:09 +04:00
|
|
|
int flags = DIRENT_FLAG_INCLUDE_EMPTY;
|
1997-04-29 21:48:10 +04:00
|
|
|
struct list_dir_struct ls;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-04-29 21:48:10 +04:00
|
|
|
ls.options = 0;
|
|
|
|
if (check_fs_open(argv[0]))
|
|
|
|
return;
|
|
|
|
|
util.c, ls.c, logdump.c, htree.c, dump.c, debugfs.h, debugfs.c, ChangeLog:
util.c (reset_getopt), debugfs.c (do_open_filesys,
do_show_super_stats), ls.c (do_list_dir), dump.c (do_dump),
htree.c (do_htree_dump, do_dx_hash), logdump.c (do_logdump):
Define and use a new function, reset_getopt(), which does whatever
is necessary to reset getopt() again. This is different for
different implementations, so the portabilty issues are a bit of a
nightmare. (Addresses Debian bug #192834)
2003-05-14 07:03:43 +04:00
|
|
|
reset_getopt();
|
2012-10-14 14:34:09 +04:00
|
|
|
while ((c = getopt (argc, argv, "cdlp")) != EOF) {
|
2002-01-03 12:55:25 +03:00
|
|
|
switch (c) {
|
2012-10-14 14:34:09 +04:00
|
|
|
case 'c':
|
|
|
|
flags |= DIRENT_FLAG_INCLUDE_CSUM;
|
|
|
|
break;
|
2002-01-03 12:55:25 +03:00
|
|
|
case 'l':
|
|
|
|
ls.options |= LONG_OPT;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2012-10-14 14:34:09 +04:00
|
|
|
flags |= DIRENT_FLAG_INCLUDE_REMOVED;
|
2002-01-03 12:55:25 +03:00
|
|
|
break;
|
2008-01-04 23:28:51 +03:00
|
|
|
case 'p':
|
|
|
|
ls.options |= PARSE_OPT;
|
|
|
|
break;
|
2006-04-28 04:59:42 +04:00
|
|
|
default:
|
|
|
|
goto print_usage;
|
2002-01-03 12:55:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > optind+1) {
|
2006-04-28 04:59:42 +04:00
|
|
|
print_usage:
|
2008-01-04 23:28:51 +03:00
|
|
|
com_err(0, 0, "Usage: ls [-l] [-d] [-p] file");
|
2002-01-03 12:55:25 +03:00
|
|
|
return;
|
1997-04-29 21:48:10 +04:00
|
|
|
}
|
|
|
|
|
2002-01-03 12:55:25 +03:00
|
|
|
if (argc == optind)
|
1997-04-29 21:48:10 +04:00
|
|
|
inode = cwd;
|
|
|
|
else
|
2002-01-03 12:55:25 +03:00
|
|
|
inode = string_to_inode(argv[optind]);
|
1997-04-29 21:48:10 +04:00
|
|
|
if (!inode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ls.f = open_pager();
|
|
|
|
ls.col = 0;
|
2002-01-03 12:55:25 +03:00
|
|
|
|
|
|
|
retval = ext2fs_dir_iterate2(current_fs, inode, flags,
|
1997-04-29 21:48:10 +04:00
|
|
|
0, list_dir_proc, &ls);
|
|
|
|
fprintf(ls.f, "\n");
|
|
|
|
close_pager(ls.f);
|
|
|
|
if (retval)
|
2005-12-11 05:50:30 +03:00
|
|
|
com_err(argv[1], retval, 0);
|
1997-04-29 21:48:10 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|