private void assertPath(final FsPath path, final FsMountPoint mountPoint, final FsEntryName entryName) { if (null != mountPoint) assertThat(path.getMountPoint(), equalTo(mountPoint)); else assertThat(path.getMountPoint(), nullValue()); assertThat(path.getEntryName(), equalTo(entryName)); assertThat(path.toString(), equalTo(path.toUri().toString())); assertThat(FsPath.create(path.toUri()), equalTo(path)); assertThat(FsPath.create(path.toUri()).hashCode(), equalTo(path.hashCode())); }
/** * Returns the parent of the given file system path. * * @param path a file system path. * @return The parent file system path. * @throws URISyntaxException */ static @Nullable FsPath parent(FsPath path) throws URISyntaxException { FsMountPoint mp = path.getMountPoint(); FsEntryName en = path.getEntryName(); if (en.isRoot()) { if (null == mp) return null; path = mp.getPath(); if (null != path) return parent(path); URI mpu = mp.toUri(); URI pu = mpu.resolve(DOT_DOT_URI); if (mpu.getRawPath().length() <= pu.getRawPath().length()) return null; return new FsPath(pu); } else { URI pu = en.toUri().resolve(DOT_URI); en = new FsEntryName(pu, CANONICALIZE); return new FsPath(mp, en); } } }
this.detector = detector; final FsMountPoint mp = path.getMountPoint(); final FsPath mpp = mp.getPath(); final FsEntryName en; assert !path.toUri().isOpaque(); this.enclArchive = null; this.enclEntryName = null; this.innerArchive = null; } else if ((en = path.getEntryName()).isRoot()) { assert path.toUri().isOpaque(); if (mpp.toUri().isOpaque()) { this.enclArchive = new TFile(mpp.getMountPoint(), detector); this.enclEntryName = mpp.getEntryName(); } else { this.enclArchive = null; assert path.toUri().isOpaque(); this.enclArchive = new TFile(mp, detector); this.enclEntryName = en;
/** * Returns {@code true} if and only if this {@code TPath} addresses an * archive file. * Whether or not this is true solely depends on the * {@link TArchiveDetector} which was used to construct this {@code TPath} * - no file system tests are performed by this method! * * @return {@code true} if and only if this {@code TPath} addresses an * archive file. * @see #isEntry */ public boolean isArchive() { final FsPath address = getAddress(); final boolean root = address.getEntryName().isRoot(); final FsMountPoint parent = address.getMountPoint().getParent(); return root && null != parent; }
@Test public void testSpaces() { for (final String[] params : new String[][] { { "foo:bar:baz:/%20!/%20/%20!/%20/%20", " ", " / ", " / ", }, { "foo:bar:baz:/%20a%20!/%20b%20!/%20c%20", " a ", " b ", " c ", }, }) { FsPath path = FsPath.create(URI.create(params[0])); for (int i = params.length; 0 < --i; ) { assertThat(path.getEntryName().getPath(), equalTo(params[i])); path = path.getMountPoint().getPath(); } } }
@Test public void testResolve() { for (final String[] params : new String[][] { { "foo:bar:/baz?plonk!/", "", "baz", "foo:bar:/baz?plonk!/" }, { "foo:bar:/bäz?bööm!/", "bäng?plönk", "bäz/bäng?plönk", "foo:bar:/bäz?bööm!/bäng?plönk" }, { "foo:bar:/baz!/", "bang?boom", "baz/bang?boom", "foo:bar:/baz!/bang?boom" }, { "foo:bar:/baz!/", "bang", "baz/bang", "foo:bar:/baz!/bang" }, { "foo:bar:/baz!/", "", "baz", "foo:bar:/baz!/" }, { "foo:bar:/baz?plonk!/", "bang?boom", "baz/bang?boom", "foo:bar:/baz?plonk!/bang?boom" }, { "foo:bar:/baz?plonk!/", "bang", "baz/bang", "foo:bar:/baz?plonk!/bang" }, { "foo:/bar/", "baz?bang", null, "foo:/bar/baz?bang" }, { "foo:/bar/", "baz", null, "foo:/bar/baz" }, { "foo:/bar/", "", null, "foo:/bar/" }, { "foo:/bar/", "baz", null, "foo:/bar/baz" }, }) { final FsMountPoint mountPoint = FsMountPoint.create(URI.create(params[0])); final FsEntryName entryName = FsEntryName.create(URI.create(params[1])); final FsEntryName parentEntryName = null == params[2] ? null : FsEntryName.create(URI.create(params[2])); final FsPath path = FsPath.create(URI.create(params[3])); if (null != parentEntryName) assertThat(mountPoint.getPath().resolve(entryName).getEntryName(), equalTo(parentEntryName)); assertThat(mountPoint.resolve(entryName), equalTo(path)); assertThat(mountPoint.resolve(entryName).toUri().isAbsolute(), is(true)); } }
final FsEntryName entryName = FsEntryName.create(URI.create(params[2])); final FsEntryName parentEntryName = FsEntryName.create(URI.create(params[3])); final FsPath path = FsPath.create(URI.create(params[4])); FsModel parent = newModel(parentMountPoint); FsModel model = newModel(mountPoint, parent); assertThat(model.getMountPoint().getPath().resolve(entryName).getEntryName(), equalTo(parentEntryName)); assertThat(model.getMountPoint().resolve(entryName), equalTo(path)); assertThat(model.isMounted(), is(false));
/** * Tests issue #TRUEZIP-154. * * @see <a href="http://java.net/jira/browse/TRUEZIP-154">ServiceConfigurationError: Unknown file system scheme for path without a suffix</a> */ @Test public void testIssue154() { for (String param : new String[] { "mok:file:/foo!/", "mok:mok:file:/foo!/bar!/", }) { FsPath path = FsPath.create(URI.create(param)); try { assertIssue154(new TFile(path)); assertIssue154(new TFile(path.toUri())); } catch (ServiceConfigurationError error) { throw new AssertionError(param, error); } } }
public void testConstructorWithInvalidUri() throws URISyntaxException { try { FsPath.create((URI) null); fail(); } catch (NullPointerException expected) { new FsPath((URI) null); fail(); } catch (NullPointerException expected) { FsPath.create((URI) null, NULL); fail(); } catch (NullPointerException expected) { new FsPath((URI) null, NULL); fail(); } catch (NullPointerException expected) { FsPath.create((URI) null, CANONICALIZE); fail(); } catch (NullPointerException expected) { new FsPath((URI) null, CANONICALIZE); fail(); } catch (NullPointerException expected) { new FsPath((FsMountPoint) null, null); fail(); } catch (NullPointerException expected) {
if (men.isRoot() || (ppu = pp.toUri()).isOpaque() || !ppu.isAbsolute()) { mp = pp.resolve(men); } else { final String pup = ppu.getPath(); if (!pup.endsWith(SEPARATOR)) ppu = new UriBuilder(ppu).path(pup + SEPARATOR_CHAR).getUri(); mp = new FsPath(new FsMountPoint(ppu), men); mp = new FsPath(new FsMountPoint(s, mp), ROOT); return mp;
private TFileSystemProvider(final String scheme, final URI root) { assert null != scheme; this.scheme = scheme; this.root = FsPath.create(root); }
@Test public void testHierarchicalize() { for (final String[] params : new String[][] { { "foo:bar:baz:/x/bö%20m?plö%20k!/bä%20g?zö%20k!/", "baz:/x/bö%20m/bä%20g?zö%20k" }, { "bar:baz:/x/bö%20m?plö%20k!/bä%20g?zö%20k", "baz:/x/bö%20m/bä%20g?zö%20k" }, { "foo:bar:baz:/x/bööm?plönk!/bäng?zönk!/", "baz:/x/bööm/bäng?zönk" }, { "bar:baz:/x/bööm?plönk!/bäng?zönk", "baz:/x/bööm/bäng?zönk" }, { "foo:bar:baz:/boom?plonk!/bang?zonk!/", "baz:/boom/bang?zonk" }, { "bar:baz:/boom?plonk!/bang?zonk", "baz:/boom/bang?zonk" }, { "bar:baz:/boom?plonk!/?zonk", "baz:/boom/?zonk" }, { "bar:baz:/boom?plonk!/bang", "baz:/boom/bang" }, { "bar:baz:/boom?plonk!/", "baz:/boom?plonk" }, { "foo:bar:baz:/boom!/bang!/", "baz:/boom/bang" }, { "bar:baz:/boom!/bang", "baz:/boom/bang" }, { "foo:bar:/baz?boom!/", "bar:/baz?boom" }, { "bar:/baz?boom", "bar:/baz?boom" }, { "foo:bar:/baz!/", "bar:/baz" }, { "bar:/baz", "bar:/baz" }, { "foo:/bar/?boom", "foo:/bar/?boom" }, { "bar?boom", "bar?boom" }, { "foo:/bar/", "foo:/bar/" }, { "bar", "bar" }, }) { final FsPath path = FsPath.create(URI.create(params[0])); final URI hierarchical = path.toHierarchicalUri(); assertThat(hierarchical, equalTo(URI.create(params[1]))); } } }
return new FsMountPoint( scheme, new FsPath( new FsMountPoint(enclArchive.toURI(), CANONICALIZE), enclEntryName)).toUri(); } else { return new FsMountPoint(scheme, new FsPath(file)).toUri(); return new FsPath( new FsMountPoint(enclArchive.toURI(), CANONICALIZE), enclEntryName).toUri(); } else { return file.toURI();
private static void assertScan(final String... params) throws URISyntaxException { final FsPath parent = new FsPath(new URI(params[0])); final URI member = new URI(params[1]); final FsPath path = new FsPath(new URI(params[2])); final FsMountPoint mountPoint = null == params[3] ? null : new FsMountPoint(new URI(params[3])); final FsPath result = new TPathScanner( TConfig.get().getArchiveDetector()) .scan(parent, member); assertThat(result, equalTo(path)); assertThat(result.getMountPoint(), is(mountPoint)); }
/** * Returns the file system mount point for this path. * * @return The file system mount point for this path. */ FsMountPoint getMountPoint() { return getAddress().getMountPoint(); }
@Override public FsEntry getEntry(final FsEntryName name) throws IOException { try { return delegate.getEntry(name); } catch (final ControlFlowException ex) { if (!name.isRoot() || null == findKeyException(ex)) throw ex; Entry entry = getParent().getEntry( getModel() .getMountPoint() .getPath() .resolve(name) .getEntryName()); // We're not holding any locks, so it's possible that someone else // has concurrently modified the parent file system. if (null == entry) return null; // The entry is inaccessible for some reason. // This may be because the cipher key is not available. // Now mask the entry as a special file. while (entry instanceof FsCovariantEntry<?>) entry = ((FsCovariantEntry<?>) entry).getEntry(); final FsCovariantEntry<FsArchiveEntry> special = new FsCovariantEntry<FsArchiveEntry>(ROOT_PATH); special.put(SPECIAL, driver.newEntry(ROOT_PATH, SPECIAL, entry)); return special; } }
final int mpl = pathPrefixLength(member); if (0 < mpl) { final URI pu = parent.toHierarchicalUri().resolve(SEPARATOR_URI); final String ma = member.getAuthority(); final String p = null != ma || mp.startsWith(SEPARATOR) ? mp.substring(0, mpl) : pu.getPath() + mp.substring(0, mpl); this.root = new FsPath( new UriBuilder() .scheme(pu.getScheme())
if (null != enclArchive) { assert null != enclEntryName; return new FsPath( new FsMountPoint( scheme, new FsPath( new FsMountPoint(enclArchive.toURI(), CANONICALIZE), enclEntryName)), ROOT); } else { return new FsPath( new FsMountPoint(scheme, new FsPath(file)), ROOT); return new FsPath( new FsMountPoint(enclArchive.toURI(), CANONICALIZE), enclEntryName); } else { return new FsPath(file);
/** * Returns the file system entry name for this path. * * @return The file system entry name for this path. */ FsEntryName getEntryName() { return getAddress().getEntryName(); }