@@ -57,13 +57,15 @@ public static void registerCommands(CommandFactory factory) {
5757 private static final String OPTION_ATIME = "u" ;
5858 private static final String OPTION_SIZE = "S" ;
5959 private static final String OPTION_ECPOLICY = "e" ;
60+ private static final String OPTION_PRINTINODEID = "i" ;
6061
6162 public static final String NAME = "ls" ;
6263 public static final String USAGE = "[-" + OPTION_PATHONLY + "] [-" +
6364 OPTION_DIRECTORY + "] [-" + OPTION_HUMAN + "] [-" +
6465 OPTION_HIDENONPRINTABLE + "] [-" + OPTION_RECURSIVE + "] [-" +
6566 OPTION_MTIME + "] [-" + OPTION_SIZE + "] [-" + OPTION_REVERSE + "] [-" +
66- OPTION_ATIME + "] [-" + OPTION_ECPOLICY +"] [<path> ...]" ;
67+ OPTION_ATIME + "] [-" + OPTION_ECPOLICY +"] [-" +
68+ OPTION_PRINTINODEID + "] [<path> ...]" ;
6769
6870 public static final String DESCRIPTION =
6971 "List the contents that match the specified file pattern. If " +
@@ -96,7 +98,9 @@ public static void registerCommands(CommandFactory factory) {
9698 " Use time of last access instead of modification for\n " +
9799 " display and sorting.\n " +
98100 " -" + OPTION_ECPOLICY +
99- " Display the erasure coding policy of files and directories.\n " ;
101+ " Display the erasure coding policy of files and directories.\n " +
102+ " -" + OPTION_PRINTINODEID +
103+ " Print the inode id for files and directories.\n " ;
100104
101105 protected final SimpleDateFormat dateFormat =
102106 new SimpleDateFormat ("yyyy-MM-dd HH:mm" );
@@ -110,6 +114,7 @@ public static void registerCommands(CommandFactory factory) {
110114 private boolean orderSize ;
111115 private boolean useAtime ;
112116 private boolean displayECPolicy ;
117+ private boolean printInodeId ;
113118 private Comparator <PathData > orderComparator ;
114119
115120 protected boolean humanReadable = false ;
@@ -135,7 +140,8 @@ protected void processOptions(LinkedList<String> args)
135140 CommandFormat cf = new CommandFormat (0 , Integer .MAX_VALUE ,
136141 OPTION_PATHONLY , OPTION_DIRECTORY , OPTION_HUMAN ,
137142 OPTION_HIDENONPRINTABLE , OPTION_RECURSIVE , OPTION_REVERSE ,
138- OPTION_MTIME , OPTION_SIZE , OPTION_ATIME , OPTION_ECPOLICY );
143+ OPTION_MTIME , OPTION_SIZE , OPTION_ATIME , OPTION_ECPOLICY ,
144+ OPTION_PRINTINODEID );
139145 cf .parse (args );
140146 pathOnly = cf .getOpt (OPTION_PATHONLY );
141147 dirRecurse = !cf .getOpt (OPTION_DIRECTORY );
@@ -147,6 +153,7 @@ protected void processOptions(LinkedList<String> args)
147153 orderSize = !orderTime && cf .getOpt (OPTION_SIZE );
148154 useAtime = cf .getOpt (OPTION_ATIME );
149155 displayECPolicy = cf .getOpt (OPTION_ECPOLICY );
156+ printInodeId = cf .getOpt (OPTION_PRINTINODEID );
150157 if (args .isEmpty ()) args .add (Path .CUR_DIR );
151158
152159 initialiseOrderComparator ();
@@ -289,9 +296,21 @@ protected void processPath(PathData item) throws IOException {
289296 return ;
290297 }
291298 FileStatus stat = item .stat ;
299+ // Using String as fileId type since we are parsing it from a String
300+ String fileIdStr = null ;
301+ if (printInodeId ) {
302+ final String fileIdKey = "fileId" ;
303+ fileIdStr = CommandUtils .getValueFromFileStatusString (
304+ item .stat .toString (), fileIdKey );
305+ if (fileIdStr == null ) {
306+ // fileId field not found, set it to "0"
307+ fileIdStr = "0" ;
308+ }
309+ }
292310 if (displayECPolicy ) {
293311 ContentSummary contentSummary = item .fs .getContentSummary (item .path );
294312 String line = String .format (lineFormat ,
313+ printInodeId ? fileIdStr + " " : "" ,
295314 (stat .isDirectory () ? "d" : "-" ),
296315 stat .getPermission () + (stat .hasAcl () ? "+" : " " ),
297316 (stat .isFile () ? stat .getReplication () : "-" ),
@@ -306,6 +325,7 @@ protected void processPath(PathData item) throws IOException {
306325 out .println (line );
307326 } else {
308327 String line = String .format (lineFormat ,
328+ printInodeId ? fileIdStr + " " : "" ,
309329 (stat .isDirectory () ? "d" : "-" ),
310330 stat .getPermission () + (stat .hasAcl () ? "+" : " " ),
311331 (stat .isFile () ? stat .getReplication () : "-" ),
@@ -334,7 +354,7 @@ private void adjustColumnWidths(PathData items[]) throws IOException {
334354 }
335355
336356 StringBuilder fmt = new StringBuilder ();
337- fmt .append ("%s%s" ) // permission string
357+ fmt .append ("%s%s%s " ) // inode id and permission string
338358 .append ("%" + maxRepl + "s " )
339359 .append ((maxOwner > 0 ) ? "%-" + maxOwner + "s " : "%s" )
340360 .append ((maxGroup > 0 ) ? "%-" + maxGroup + "s " : "%s" );
0 commit comments