View Javadoc
1   /***
2    * 
3    */
4   package com.fernsroth.squashfs;
5   
6   import java.io.File;
7   import java.io.IOException;
8   import java.util.Arrays;
9   import java.util.LinkedList;
10  import java.util.Queue;
11  
12  import com.fernsroth.easyio.EasyIORandomAccessFile;
13  import com.fernsroth.easyio.exception.EasyIOException;
14  
15  /***
16   * 
17   * @author Joseph M. Ferner (Near Infinity Corporation)
18   */
19  public final class Unsquashfs {
20  
21      /***
22       * default destination.
23       */
24      private static final String DEFAULT_DESTINATION = "squashfs-root";
25  
26      /***
27       * hide constructor. 
28       */
29      private Unsquashfs() {
30          // empty.
31      }
32  
33      /***
34       * main entry point.
35       * @param args the command line arguments.
36       * @throws Exception 
37       */
38      public static void main(String args[]) throws Exception {
39          String dest = null;
40          boolean lsonly = false;
41  
42          Queue<String> argQueue = new LinkedList<String>(Arrays.asList(args));
43          while (argQueue.size() > 1) {
44              if (argQueue.peek().equalsIgnoreCase("-dest")) {
45                  argQueue.remove();
46                  if (argQueue.isEmpty()) {
47                      printUsage("'-dest' requires an argument");
48                      return;
49                  }
50                  dest = argQueue.remove();
51              } else if (argQueue.peek().equalsIgnoreCase("-ls")) {
52                  argQueue.remove();
53                  lsonly = true;
54              } else {
55                  printUsage("invalid command line argument '" + argQueue.peek()
56                          + "'");
57                  return;
58              }
59          }
60          if (argQueue.isEmpty()) {
61              printUsage("no source file specified");
62              return;
63          }
64          if (lsonly && dest != null) {
65              printUsage("cannot specify '-ls' and '-dest' at the same time.");
66              return;
67          }
68  
69          String source = argQueue.remove();
70          File sourceFile = new File(source);
71          if (!sourceFile.canRead()) {
72              System.err.println("cannot read source file '"
73                      + sourceFile.toString() + "'");
74              return;
75          }
76  
77          File destFile = null;
78          if (!lsonly) {
79              if (dest == null) {
80                  dest = DEFAULT_DESTINATION;
81              }
82              if (dest != null) {
83                  destFile = new File(dest);
84                  destFile.mkdirs();
85              }
86          }
87  
88          SquashFSReader reader;
89          try {
90              reader = new SquashFSReader(new EasyIORandomAccessFile(sourceFile,
91                      "r"));
92          } catch (EasyIOException e) {
93              throw new Exception("could not read source '" + sourceFile + "'", e);
94          } catch (IOException e) {
95              throw new Exception("could not read source '" + sourceFile + "'", e);
96          }
97  
98          if (lsonly) {
99              SquashFSUtils.walk(reader.getRootDirectory(), new LSWalkHandler());
100         } else {
101             OutputWalkHandler out = new OutputWalkHandler(reader, destFile);
102             out.setIncludeMTimeInManfest(false);
103             SquashFSUtils.walk(reader.getRootDirectory(), out);
104             out.writeManifest(new File(destFile, "manifest.xml"));
105         }
106     }
107 
108     /***
109      * prints the usage.
110      * @param message the message to print in the help.
111      */
112     private static void printUsage(String message) {
113         if (message != null) {
114             System.err.println(message);
115         }
116         System.err.println("SYNTAX: " + Unsquashfs.class.getName()
117                 + " [-ls | -dest] filesystem");
118         System.err.println("\t-ls              list filesystem only");
119         System.err
120                 .println("\t-dest <pathname> unsquash to <pathname>, default \""
121                         + DEFAULT_DESTINATION + "\"");
122     }
123 
124 }