1 /***
2 *
3 */
4 package com.fernsroth.squashfs;
5
6 import java.io.File;
7 import java.io.FileNotFoundException;
8 import java.io.IOException;
9
10 import com.fernsroth.easyio.EasyIORandomAccessFile;
11 import com.fernsroth.easyio.IRandomAccessSource;
12 import com.fernsroth.squashfs.model.BaseFile;
13 import com.fernsroth.squashfs.model.Manifest;
14 import com.fernsroth.squashfs.model.SFSSourceFile;
15
16 /***
17 *
18 * @author Joseph M. Ferner (Near Infinity Corporation)
19 */
20 public class FileSystemDataProvider implements DataProvider {
21
22 /***
23 * the source directory.
24 */
25 private File sourceDir;
26
27 /***
28 * ino counter.
29 */
30 private static int ino = 1;
31
32 /***
33 * @param sourceDir
34 */
35 public FileSystemDataProvider(File sourceDir) {
36 this.sourceDir = sourceDir;
37 }
38
39 /***
40 * {@inheritDoc}
41 */
42 public IRandomAccessSource getData(Manifest source, BaseFile bf)
43 throws IOException {
44 if (bf instanceof SFSSourceFile
45 && ((SFSSourceFile) bf).getSourceFile() != null) {
46 return new EasyIORandomAccessFile(((SFSSourceFile) bf)
47 .getSourceFile(), "r");
48 } else {
49 String path = source.getPath(bf);
50 File f = new File(this.sourceDir, path);
51 return new EasyIORandomAccessFile(f, "r");
52 }
53 }
54
55 /***
56 * {@inheritDoc}
57 */
58 public int getIno(Manifest source, BaseFile bf) {
59 return ino++;
60 }
61
62 /***
63 * {@inheritDoc}
64 */
65 public long getLength(Manifest source, BaseFile bf) throws IOException {
66 if (bf == null || bf.getName() == null) {
67 return 0;
68 }
69 String path = source.getPath(bf);
70 if (path == null) {
71 throw new FileNotFoundException("could not file '" + bf.getName()
72 + "'");
73 }
74 File f = new File(this.sourceDir, path);
75 return f.length();
76 }
77 }