diff options
author | Benoit Lamarche <benoitlamarche@google.com> | 2015-02-12 15:39:26 +0100 |
---|---|---|
committer | Benoit Lamarche <benoitlamarche@google.com> | 2015-05-29 11:49:31 +0200 |
commit | b34fb65587616cb9da9fd30affa5bf1f49428dae (patch) | |
tree | 4bbd5ec2ebf3cce5d7a9c35589d44fbf85c8354d /sched/src/com/android/sched | |
parent | 21fe55835a54f27ddb4e239d1b88907874b62feb (diff) | |
download | toolchain_jack-b34fb65587616cb9da9fd30affa5bf1f49428dae.tar.gz toolchain_jack-b34fb65587616cb9da9fd30affa5bf1f49428dae.tar.bz2 toolchain_jack-b34fb65587616cb9da9fd30affa5bf1f49428dae.zip |
Add new getPathFromRoot API in VFS
And use it to remove JayceFileImporter hack.
(cherry picked from commit 7a73a0418d5524240b7c97d9801b7db954aa018c)
Change-Id: I211bc52211d7373c22a02d83be114774ebca7520
Diffstat (limited to 'sched/src/com/android/sched')
20 files changed, 445 insertions, 38 deletions
diff --git a/sched/src/com/android/sched/vfs/BaseVFS.java b/sched/src/com/android/sched/vfs/BaseVFS.java index eb425763..354cc98b 100644 --- a/sched/src/com/android/sched/vfs/BaseVFS.java +++ b/sched/src/com/android/sched/vfs/BaseVFS.java @@ -79,6 +79,12 @@ abstract class BaseVFS<DIR extends BaseVDir, FILE extends BaseVFile> implements abstract boolean isEmpty(@Nonnull DIR dir); + @Nonnull + abstract VPath getPathFromDir(@Nonnull DIR parent, @Nonnull FILE file); + + @Nonnull + abstract VPath getPathFromRoot(@Nonnull FILE file); + // // Location related // diff --git a/sched/src/com/android/sched/vfs/BaseVFile.java b/sched/src/com/android/sched/vfs/BaseVFile.java index 3520eb6c..c2313413 100644 --- a/sched/src/com/android/sched/vfs/BaseVFile.java +++ b/sched/src/com/android/sched/vfs/BaseVFile.java @@ -75,4 +75,10 @@ abstract class BaseVFile extends BaseVElement implements VFile { public void delete() throws CannotDeleteFileException { vfs.delete(this); } + + @Override + @Nonnull + public VPath getPathFromRoot() { + return vfs.getPathFromRoot(this); + } } diff --git a/sched/src/com/android/sched/vfs/CachedDirectFS.java b/sched/src/com/android/sched/vfs/CachedDirectFS.java index 83512be1..ce1f6053 100644 --- a/sched/src/com/android/sched/vfs/CachedDirectFS.java +++ b/sched/src/com/android/sched/vfs/CachedDirectFS.java @@ -111,6 +111,11 @@ public class CachedDirectFS extends BaseVFS<CachedParentVDir, CachedParentVFile> public Collection<? extends BaseVElement> list() { return vfs.list(this); } + + @CheckForNull + public CachedParentVDir getParent() { + return parent; + } } static class CachedParentVFile extends ParentVFile { @@ -405,4 +410,29 @@ public class CachedDirectFS extends BaseVFS<CachedParentVDir, CachedParentVFile> private File getNativeFile(@Nonnull VPath path, @Nonnull String name) { return new File(new File(dir.getFile(), path.getPathAsString(File.separatorChar)), name); } + + @Override + @Nonnull + VPath getPathFromDir(@Nonnull CachedParentVDir parent, @Nonnull CachedParentVFile file) { + StringBuffer path = getPathFromDirInternal(parent, (CachedParentVDir) file.getParent()) + .append(file.getName()); + return new VPath(path.toString(), '/'); + } + + @Nonnull + private static StringBuffer getPathFromDirInternal(@Nonnull CachedParentVDir baseDir, + @Nonnull CachedParentVDir currentDir) { + if (baseDir == currentDir) { + return new StringBuffer(); + } + CachedParentVDir currentParent = currentDir.getParent(); + assert currentParent != null; + return getPathFromDirInternal(baseDir, currentParent).append(currentDir.getName()).append('/'); + } + + @Override + @Nonnull + public VPath getPathFromRoot(@Nonnull CachedParentVFile file) { + return getPathFromDir(root, file); + } } diff --git a/sched/src/com/android/sched/vfs/CaseInsensitiveFS.java b/sched/src/com/android/sched/vfs/CaseInsensitiveFS.java index 18073333..78ce7b28 100644 --- a/sched/src/com/android/sched/vfs/CaseInsensitiveFS.java +++ b/sched/src/com/android/sched/vfs/CaseInsensitiveFS.java @@ -118,6 +118,43 @@ public class CaseInsensitiveFS extends BaseVFS<CaseInsensitiveVDir, CaseInsensit return VPath.ROOT; } } + + @Override + @Nonnull + public BaseVFile getVFile(@Nonnull String name) throws NoSuchFileException, + NotFileException { + return vfs.getVFile(this, name); + } + + @Override + @Nonnull + public BaseVDir getVDir(@Nonnull String name) throws NotDirectoryException, + NoSuchFileException { + return vfs.getVDir(this, name); + } + + @Override + @Nonnull + public BaseVFile createVFile(@Nonnull String name) throws CannotCreateFileException { + return vfs.createVFile(this, name); + } + + @Override + @Nonnull + public BaseVDir createVDir(@Nonnull String name) throws CannotCreateFileException { + return vfs.createVDir(this, name); + } + + @Override + @Nonnull + public Collection<? extends BaseVElement> list() { + return vfs.list(this); + } + + @CheckForNull + public VDir getParent() { + return parent; + } } static class CaseInsensitiveVFile extends ParentVFile { @@ -143,8 +180,10 @@ public class CaseInsensitiveFS extends BaseVFS<CaseInsensitiveVDir, CaseInsensit @Override public void delete() throws CannotDeleteFileException { - super.delete(); - // we need to remove the VFile from the parent dir's map + vfs.delete(this); + } + + public void deleteFromCache() { ((InMemoryVDir) parent).internalDelete(name); } } @@ -340,35 +379,74 @@ public class CaseInsensitiveFS extends BaseVFS<CaseInsensitiveVDir, CaseInsensit @Override @Nonnull - CaseInsensitiveVDir getVDir(@Nonnull CaseInsensitiveVDir parent, @Nonnull String name) { - throw new UnsupportedOperationException(); + CaseInsensitiveVDir getVDir(@Nonnull CaseInsensitiveVDir parent, @Nonnull String name) + throws NotDirectoryException, NoSuchFileException { + BaseVElement element = parent.getFromCache(name); + if (element != null) { + if (element.isVDir()) { + return (CaseInsensitiveVDir) element; + } else { + throw new NotDirectoryException(getVDirLocation(parent, name)); + } + } else { + throw new NoSuchFileException(getVDirLocation(parent, name)); + } } @Override @Nonnull - CaseInsensitiveVFile getVFile(@Nonnull CaseInsensitiveVDir parent, @Nonnull String name) { - throw new UnsupportedOperationException(); + CaseInsensitiveVFile getVFile(@Nonnull CaseInsensitiveVDir parent, @Nonnull String name) + throws NotFileException, NoSuchFileException { + BaseVElement element = parent.getFromCache(name); + if (element != null) { + if (!element.isVDir()) { + return (CaseInsensitiveVFile) element; + } else { + throw new NotFileException(getVFileLocation(parent, name)); + } + } else { + throw new NoSuchFileException(getVFileLocation(parent, name)); + } } @Override @Nonnull - CaseInsensitiveVDir createVDir(@Nonnull CaseInsensitiveVDir parent, @Nonnull String name) { + synchronized CaseInsensitiveVDir createVDir(@Nonnull CaseInsensitiveVDir parent, + @Nonnull String name) throws CannotCreateFileException { assert !isClosed(); - return new CaseInsensitiveVDir(this, parent, name); + try { + return getVDir(parent, name); + + } catch (NoSuchFileException e) { + CaseInsensitiveVDir dir = new CaseInsensitiveVDir(this, parent, name); + parent.putInCache(name, dir); + return dir; + } catch (NotDirectoryException e) { + throw new CannotCreateFileException(getVDirLocation(parent, name)); + } } @Override @Nonnull - CaseInsensitiveVFile createVFile(@Nonnull CaseInsensitiveVDir parent, @Nonnull String name) - throws CannotCreateFileException { + synchronized CaseInsensitiveVFile createVFile(@Nonnull CaseInsensitiveVDir parent, + @Nonnull String name) throws CannotCreateFileException { assert !isClosed(); - CaseInsensitiveVFile original = new CaseInsensitiveVFile(this, parent, name); - BaseVFile encoded = vfs.getRootDir().createVFile(encode(original.getPath())); - original.setEncodedFile(encoded); + try { + return getVFile(parent, name); + + } catch (NoSuchFileException e) { + + CaseInsensitiveVFile original = new CaseInsensitiveVFile(this, parent, name); + BaseVFile encoded = vfs.getRootDir().createVFile(encode(original.getPath())); + original.setEncodedFile(encoded); + parent.putInCache(name, original); - return original; + return original; + } catch (NotFileException e) { + throw new CannotCreateFileException(getVFileLocation(parent, name)); + } } @Override @@ -379,6 +457,7 @@ public class CaseInsensitiveFS extends BaseVFS<CaseInsensitiveVDir, CaseInsensit try { BaseVFile encoded = vfs.getRootDir().getVFile(encode(file.getPath())); vfs.delete(encoded); + file.deleteFromCache(); } catch (NotDirectoryException e) { throw new CannotDeleteFileException(file); } catch (NotFileException e) { @@ -391,12 +470,12 @@ public class CaseInsensitiveFS extends BaseVFS<CaseInsensitiveVDir, CaseInsensit @Override @Nonnull Collection<? extends BaseVElement> list(@Nonnull CaseInsensitiveVDir dir) { - throw new UnsupportedOperationException(); + return dir.getAllFromCache(); } @Override boolean isEmpty(@Nonnull CaseInsensitiveVDir dir) { - throw new UnsupportedOperationException(); + return list(dir).isEmpty(); } // @@ -495,4 +574,29 @@ public class CaseInsensitiveFS extends BaseVFS<CaseInsensitiveVDir, CaseInsensit return array; } + + @Override + @Nonnull + VPath getPathFromDir(@Nonnull CaseInsensitiveVDir parent, @Nonnull CaseInsensitiveVFile file) { + StringBuffer path = getPathFromDirInternal(parent, (CaseInsensitiveVDir) file.getParent()) + .append(file.getName()); + return new VPath(path.toString(), '/'); + } + + @Nonnull + private StringBuffer getPathFromDirInternal(@Nonnull CaseInsensitiveVDir baseDir, + @Nonnull CaseInsensitiveVDir currentDir) { + if (baseDir == currentDir) { + return new StringBuffer(); + } + CaseInsensitiveVDir currentParent = (CaseInsensitiveVDir) currentDir.getParent(); + assert currentParent != null; + return getPathFromDirInternal(baseDir, currentParent).append(currentDir.getName()).append('/'); + } + + @Override + @Nonnull + VPath getPathFromRoot(@Nonnull CaseInsensitiveVFile file) { + return getPathFromDir(root, file); + } } diff --git a/sched/src/com/android/sched/vfs/DeflateFS.java b/sched/src/com/android/sched/vfs/DeflateFS.java index 0179b700..f805c2e7 100644 --- a/sched/src/com/android/sched/vfs/DeflateFS.java +++ b/sched/src/com/android/sched/vfs/DeflateFS.java @@ -198,4 +198,16 @@ public class DeflateFS extends BaseVFS<BaseVDir, BaseVFile> implements VFS{ Location getVDirLocation(@Nonnull BaseVDir parent, @Nonnull VPath path) { return vfs.getVDirLocation(parent, path); } + + @Override + @Nonnull + VPath getPathFromDir(@Nonnull BaseVDir parent, @Nonnull BaseVFile file) { + return vfs.getPathFromDir(parent, file); + } + + @Override + @Nonnull + VPath getPathFromRoot(@Nonnull BaseVFile file) { + return vfs.getPathFromRoot(file); + } } diff --git a/sched/src/com/android/sched/vfs/DirectFS.java b/sched/src/com/android/sched/vfs/DirectFS.java index b280a404..4495439b 100644 --- a/sched/src/com/android/sched/vfs/DirectFS.java +++ b/sched/src/com/android/sched/vfs/DirectFS.java @@ -299,4 +299,29 @@ public class DirectFS extends BaseVFS<ParentVDir, ParentVFile> implements VFS { private File getNativeFile(@Nonnull VPath path, @Nonnull String name) { return new File(new File(dir.getFile(), path.getPathAsString(File.separatorChar)), name); } + + @Override + @Nonnull + VPath getPathFromDir(@Nonnull ParentVDir parent, @Nonnull ParentVFile file) { + StringBuffer path = + getPathFromDirInternal(parent, (ParentVDir) file.getParent()).append(file.getName()); + return new VPath(path.toString(), '/'); + } + + @Nonnull + private static StringBuffer getPathFromDirInternal(@Nonnull ParentVDir baseDir, + @Nonnull ParentVDir currentDir) { + if (baseDir == currentDir) { + return new StringBuffer(); + } + ParentVDir currentParent = (ParentVDir) currentDir.getParent(); + assert currentParent != null; + return getPathFromDirInternal(baseDir, currentParent).append(currentDir.getName()).append('/'); + } + + @Override + @Nonnull + public VPath getPathFromRoot(@Nonnull ParentVFile file) { + return getPathFromDir(root, file); + } } diff --git a/sched/src/com/android/sched/vfs/GenericInputOutputVFile.java b/sched/src/com/android/sched/vfs/GenericInputOutputVFile.java index ec731173..b17adbcc 100644 --- a/sched/src/com/android/sched/vfs/GenericInputOutputVFile.java +++ b/sched/src/com/android/sched/vfs/GenericInputOutputVFile.java @@ -82,4 +82,10 @@ public class GenericInputOutputVFile implements InputOutputVFile { public String getDigest() { return file.getDigest(); } + + @Override + @Nonnull + public VPath getPathFromRoot() { + return file.getPathFromRoot(); + } }
\ No newline at end of file diff --git a/sched/src/com/android/sched/vfs/GenericInputVFS.java b/sched/src/com/android/sched/vfs/GenericInputVFS.java index e1e29c6e..c34835df 100644 --- a/sched/src/com/android/sched/vfs/GenericInputVFS.java +++ b/sched/src/com/android/sched/vfs/GenericInputVFS.java @@ -62,4 +62,10 @@ public class GenericInputVFS extends AbstractVFS implements InputVFS { public String getDigest() { return vfs.getDigest(); } + + @SuppressWarnings({"unchecked", "rawtypes"}) + @Nonnull + public VPath getPathFromRoot(@Nonnull InputVFile file) { + return ((BaseVFS) vfs).getPathFromRoot((BaseVFile) ((GenericInputVFile) file).getVFile()); + } } diff --git a/sched/src/com/android/sched/vfs/GenericInputVFile.java b/sched/src/com/android/sched/vfs/GenericInputVFile.java index b203dbca..74ea3aa3 100644 --- a/sched/src/com/android/sched/vfs/GenericInputVFile.java +++ b/sched/src/com/android/sched/vfs/GenericInputVFile.java @@ -68,4 +68,15 @@ public class GenericInputVFile implements InputVFile { public String getDigest() { return file.getDigest(); } + + @Override + @Nonnull + public VPath getPathFromRoot() { + return file.getPathFromRoot(); + } + + @Nonnull + VFile getVFile() { + return file; + } }
\ No newline at end of file diff --git a/sched/src/com/android/sched/vfs/InputVFile.java b/sched/src/com/android/sched/vfs/InputVFile.java index 55eed907..cc494ed3 100644 --- a/sched/src/com/android/sched/vfs/InputVFile.java +++ b/sched/src/com/android/sched/vfs/InputVFile.java @@ -18,10 +18,14 @@ package com.android.sched.vfs; import com.android.sched.util.file.CannotDeleteFileException; +import javax.annotation.Nonnull; + /** * Virtual file to read from. */ public interface InputVFile extends InputVElement, InputStreamProvider { void delete() throws CannotDeleteFileException; + @Nonnull + VPath getPathFromRoot(); } diff --git a/sched/src/com/android/sched/vfs/MessageDigestFS.java b/sched/src/com/android/sched/vfs/MessageDigestFS.java index 123d607b..25f1b84b 100644 --- a/sched/src/com/android/sched/vfs/MessageDigestFS.java +++ b/sched/src/com/android/sched/vfs/MessageDigestFS.java @@ -385,7 +385,7 @@ public class MessageDigestFS extends BaseVFS<MessageDigestVDir, MessageDigestVFi @Override boolean isEmpty(@Nonnull MessageDigestVDir dir) { - return vfs.isEmpty(dir); + return vfs.isEmpty(dir.getWrappedDir()); } @Override @@ -437,30 +437,42 @@ public class MessageDigestFS extends BaseVFS<MessageDigestVDir, MessageDigestVFi @Override @Nonnull Location getVFileLocation(@Nonnull MessageDigestVDir parent, @Nonnull String name) { - return vfs.getVFileLocation(parent, name); + return vfs.getVFileLocation(parent.getWrappedDir(), name); } @Override @Nonnull Location getVFileLocation(@Nonnull MessageDigestVDir parent, @Nonnull VPath path) { - return vfs.getVFileLocation(parent, path); + return vfs.getVFileLocation(parent.getWrappedDir(), path); } @Override @Nonnull Location getVDirLocation(@Nonnull MessageDigestVDir dir) { - return vfs.getVDirLocation(dir); + return vfs.getVDirLocation(dir.getWrappedDir()); } @Override @Nonnull Location getVDirLocation(@Nonnull MessageDigestVDir parent, @Nonnull String name) { - return vfs.getVDirLocation(parent, name); + return vfs.getVDirLocation(parent.getWrappedDir(), name); } @Override @Nonnull Location getVDirLocation(@Nonnull MessageDigestVDir parent, @Nonnull VPath path) { - return vfs.getVDirLocation(parent, path); + return vfs.getVDirLocation(parent.getWrappedDir(), path); + } + + @Override + @Nonnull + VPath getPathFromDir(@Nonnull MessageDigestVDir parent, @Nonnull MessageDigestVFile file) { + return vfs.getPathFromDir(parent.getWrappedDir(), file.getWrappedFile()); + } + + @Override + @Nonnull + VPath getPathFromRoot(@Nonnull MessageDigestVFile file) { + return vfs.getPathFromRoot(file.getWrappedFile()); } } diff --git a/sched/src/com/android/sched/vfs/MessageDigestInputVFS.java b/sched/src/com/android/sched/vfs/MessageDigestInputVFS.java index 6a3bfc91..bc06970d 100644 --- a/sched/src/com/android/sched/vfs/MessageDigestInputVFS.java +++ b/sched/src/com/android/sched/vfs/MessageDigestInputVFS.java @@ -101,6 +101,13 @@ public class MessageDigestInputVFS extends MessageDigestVFS implements InputVFS // This implementation is obsolete anyway throw new UnsupportedOperationException(); } + + @Override + @Nonnull + public VPath getPathFromRoot() { + // This implementation is obsolete anyway + throw new UnsupportedOperationException(); + } } /** diff --git a/sched/src/com/android/sched/vfs/ParentVDir.java b/sched/src/com/android/sched/vfs/ParentVDir.java index 1345f56e..2e50eb24 100644 --- a/sched/src/com/android/sched/vfs/ParentVDir.java +++ b/sched/src/com/android/sched/vfs/ParentVDir.java @@ -46,4 +46,9 @@ public class ParentVDir extends BaseVDir { return VPath.ROOT; } } + + @CheckForNull + VDir getParent() { + return parent; + } }
\ No newline at end of file diff --git a/sched/src/com/android/sched/vfs/ParentVFile.java b/sched/src/com/android/sched/vfs/ParentVFile.java index d26f7df3..5432fa0e 100644 --- a/sched/src/com/android/sched/vfs/ParentVFile.java +++ b/sched/src/com/android/sched/vfs/ParentVFile.java @@ -36,4 +36,9 @@ public class ParentVFile extends BaseVFile { public VPath getPath() { return parent.getPath().clone().appendPath(new VPath(name, '/')); } + + @Nonnull + VDir getParent() { + return parent; + } }
\ No newline at end of file diff --git a/sched/src/com/android/sched/vfs/PrefixedFS.java b/sched/src/com/android/sched/vfs/PrefixedFS.java index af1c14b2..cc63ea4b 100644 --- a/sched/src/com/android/sched/vfs/PrefixedFS.java +++ b/sched/src/com/android/sched/vfs/PrefixedFS.java @@ -52,7 +52,7 @@ public class PrefixedFS extends BaseVFS<BaseVDir, BaseVFile> implements VFS { } catch (NoSuchFileException e) { rootDir = this.vfs.getRootDir().createVDir(prefix); } - this.rootDir = rootDir; + this.rootDir = changeVFS(rootDir); } @Override @@ -99,35 +99,39 @@ public class PrefixedFS extends BaseVFS<BaseVDir, BaseVFile> implements VFS { @Override @Nonnull Collection<? extends BaseVElement> list(@Nonnull BaseVDir dir) { - return vfs.list(dir); + Collection<? extends BaseVElement> elements = vfs.list(dir); + for (BaseVElement element : elements) { + element.changeVFS(this); + } + return elements; } @Override @Nonnull BaseVFile createVFile(@Nonnull BaseVDir parent, @Nonnull String name) throws CannotCreateFileException { - return vfs.createVFile(parent, name); + return changeVFS(vfs.createVFile(parent, name)); } @Override @Nonnull BaseVDir createVDir(@Nonnull BaseVDir parent, @Nonnull String name) throws CannotCreateFileException { - return vfs.createVDir(parent, name); + return changeVFS(vfs.createVDir(parent, name)); } @Override @Nonnull BaseVDir getVDir(@Nonnull BaseVDir parent, @Nonnull String name) throws NotDirectoryException, NoSuchFileException { - return vfs.getVDir(parent, name); + return changeVFS(vfs.getVDir(parent, name)); } @Override @Nonnull BaseVFile getVFile(@Nonnull BaseVDir parent, @Nonnull String name) throws NotFileException, NoSuchFileException { - return vfs.getVFile(parent, name); + return changeVFS(vfs.getVFile(parent, name)); } @@ -188,4 +192,28 @@ public class PrefixedFS extends BaseVFS<BaseVDir, BaseVFile> implements VFS { Location getVDirLocation(@Nonnull BaseVDir parent, @Nonnull VPath path) { return vfs.getVDirLocation(parent, path); } + + @Override + @Nonnull + VPath getPathFromDir(@Nonnull BaseVDir parent, @Nonnull BaseVFile file) { + return vfs.getPathFromDir(parent, file); + } + + @Override + @Nonnull + VPath getPathFromRoot(@Nonnull BaseVFile file) { + return vfs.getPathFromDir(rootDir, file); + } + + @Nonnull + private BaseVDir changeVFS(@Nonnull BaseVDir dir) { + dir.changeVFS(this); + return dir; + } + + @Nonnull + private BaseVFile changeVFS(@Nonnull BaseVFile file) { + file.changeVFS(this); + return file; + } } diff --git a/sched/src/com/android/sched/vfs/ReadWriteZipFS.java b/sched/src/com/android/sched/vfs/ReadWriteZipFS.java index 2bebec07..0ada64d4 100644 --- a/sched/src/com/android/sched/vfs/ReadWriteZipFS.java +++ b/sched/src/com/android/sched/vfs/ReadWriteZipFS.java @@ -206,4 +206,16 @@ public class ReadWriteZipFS extends BaseVFS<BaseVDir, BaseVFile> implements VFS public void setWorkVFS(@Nonnull VFS workVFS) { vfs.setWorkVFS(workVFS); } + + @Override + @Nonnull + VPath getPathFromDir(@Nonnull BaseVDir parent, @Nonnull BaseVFile file) { + return vfs.getPathFromDir(parent, file); + } + + @Override + @Nonnull + VPath getPathFromRoot(@Nonnull BaseVFile file) { + return getPathFromDir(getRootDir(), file); + } } diff --git a/sched/src/com/android/sched/vfs/ReadZipFS.java b/sched/src/com/android/sched/vfs/ReadZipFS.java index 0c15f4a9..60badadd 100644 --- a/sched/src/com/android/sched/vfs/ReadZipFS.java +++ b/sched/src/com/android/sched/vfs/ReadZipFS.java @@ -19,7 +19,11 @@ package com.android.sched.vfs; import com.google.common.base.Splitter; import com.android.sched.util.file.CannotCreateFileException; +import com.android.sched.util.file.CannotDeleteFileException; import com.android.sched.util.file.InputZipFile; +import com.android.sched.util.file.NoSuchFileException; +import com.android.sched.util.file.NotDirectoryException; +import com.android.sched.util.file.NotFileException; import com.android.sched.util.location.Location; import com.android.sched.util.location.ZipLocation; import com.android.sched.vfs.ReadZipFS.ZipVDir; @@ -66,6 +70,38 @@ public class ReadZipFS extends BaseVFS<ZipVDir, ZipVFile> implements VFS { public VPath getPath() { return new VPath(zipEntry.getName(), '/'); } + + @Override + @Nonnull + public BaseVFile getVFile(@Nonnull String name) throws NoSuchFileException, + NotFileException { + return vfs.getVFile(this, name); + } + + @Override + @Nonnull + public BaseVDir getVDir(@Nonnull String name) throws NotDirectoryException, + NoSuchFileException { + return vfs.getVDir(this, name); + } + + @Override + @Nonnull + public BaseVFile createVFile(@Nonnull String name) throws CannotCreateFileException { + return vfs.createVFile(this, name); + } + + @Override + @Nonnull + public BaseVDir createVDir(@Nonnull String name) throws CannotCreateFileException { + return vfs.createVDir(this, name); + } + + @Override + @Nonnull + public Collection<? extends BaseVElement> list() { + return vfs.list(this); + } } static class ZipVFile extends BaseVFile { @@ -90,6 +126,11 @@ public class ReadZipFS extends BaseVFS<ZipVDir, ZipVFile> implements VFS { public VPath getPath() { return new VPath(zipEntry.getName(), '/'); } + + @Override + public void delete() throws CannotDeleteFileException { + vfs.delete(this); + } } @Nonnull @@ -169,30 +210,70 @@ public class ReadZipFS extends BaseVFS<ZipVDir, ZipVFile> implements VFS { @Override @Nonnull - ZipVDir getVDir(@Nonnull ZipVDir parent, @Nonnull String name) { - throw new UnsupportedOperationException(); + ZipVDir getVDir(@Nonnull ZipVDir parent, @Nonnull String name) throws NotDirectoryException, + NoSuchFileException { + BaseVElement element = parent.getFromCache(name); + if (element != null) { + if (element.isVDir()) { + return (ZipVDir) element; + } else { + throw new NotDirectoryException(getVDirLocation(parent, name)); + } + } else { + throw new NoSuchFileException(getVDirLocation(parent, name)); + } } @Override @Nonnull - ZipVFile getVFile(@Nonnull ZipVDir parent, @Nonnull String name) { - throw new UnsupportedOperationException(); + ZipVFile getVFile(@Nonnull ZipVDir parent, @Nonnull String name) throws NotFileException, + NoSuchFileException { + BaseVElement element = parent.getFromCache(name); + if (element != null) { + if (!element.isVDir()) { + return (ZipVFile) element; + } else { + throw new NotFileException(getVFileLocation(parent, name)); + } + } else { + throw new NoSuchFileException(getVFileLocation(parent, name)); + } } @Override @Nonnull - ZipVDir createVDir(@Nonnull ZipVDir parent, @Nonnull String name) { + synchronized ZipVDir createVDir(@Nonnull ZipVDir parent, @Nonnull String name) + throws CannotCreateFileException { assert !isClosed(); - return new ZipVDir(this, new ZipEntry(parent.getZipEntry().getName() + name + '/'), name); + try { + return getVDir(parent, name); + } catch (NoSuchFileException e) { + ZipVDir vDir = + new ZipVDir(this, new ZipEntry(parent.getZipEntry().getName() + name + '/'), name); + parent.putInCache(name, vDir); + return vDir; + } catch (NotDirectoryException e) { + throw new CannotCreateFileException(getVDirLocation(parent, name)); + } } @Override @Nonnull - ZipVFile createVFile(@Nonnull ZipVDir parent, @Nonnull String name) { + synchronized ZipVFile createVFile(@Nonnull ZipVDir parent, @Nonnull String name) + throws CannotCreateFileException { assert !isClosed(); - return new ZipVFile(this, zipFile.getEntry(parent.getZipEntry().getName() + name), name); + try { + return getVFile(parent, name); + } catch (NoSuchFileException e) { + ZipVFile vFile = + new ZipVFile(this, zipFile.getEntry(parent.getZipEntry().getName() + name), name); + parent.putInCache(name, vFile); + return vFile; + } catch (NotFileException e) { + throw new CannotCreateFileException(getVFileLocation(parent, name)); + } } @Override @@ -204,12 +285,12 @@ public class ReadZipFS extends BaseVFS<ZipVDir, ZipVFile> implements VFS { @Override @Nonnull Collection<? extends BaseVElement> list(@Nonnull ZipVDir dir) { - return dir.list(); + return dir.getAllFromCache(); } @Override boolean isEmpty(@Nonnull ZipVDir dir) { - return dir.list().isEmpty(); + return list(dir).isEmpty(); } // @@ -298,4 +379,20 @@ public class ReadZipFS extends BaseVFS<ZipVDir, ZipVFile> implements VFS { throw new AssertionError(e); } } + + @Override + @Nonnull + VPath getPathFromDir(@Nonnull ZipVDir parent, @Nonnull ZipVFile file) { + String fileEntryPath = file.getZipEntry().getName(); + String parentEntryPath = parent.getZipEntry().getName(); + assert fileEntryPath.startsWith(parentEntryPath); + String newPath = fileEntryPath.substring(parentEntryPath.length()); + return new VPath(newPath, '/'); + } + + @Override + @Nonnull + VPath getPathFromRoot(@Nonnull ZipVFile file) { + return getPathFromDir(root, file); + } } diff --git a/sched/src/com/android/sched/vfs/VFSToVFSWrapper.java b/sched/src/com/android/sched/vfs/VFSToVFSWrapper.java index 322fa48c..0a797576 100644 --- a/sched/src/com/android/sched/vfs/VFSToVFSWrapper.java +++ b/sched/src/com/android/sched/vfs/VFSToVFSWrapper.java @@ -258,4 +258,17 @@ public class VFSToVFSWrapper extends BaseVFS<BaseVDir, BaseVFile> implements VFS public void setWorkVFS(@Nonnull VFS temporaryVFS) { workVFS = (BaseVFS<BaseVDir, BaseVFile>) temporaryVFS; } + + @Override + @Nonnull + VPath getPathFromDir(@Nonnull BaseVDir parent, @Nonnull BaseVFile file) { + return workVFS.getPathFromDir(parent, file); + } + + @Override + @Nonnull + VPath getPathFromRoot(@Nonnull BaseVFile file) { + return workVFS.getPathFromRoot(file); + } + } diff --git a/sched/src/com/android/sched/vfs/VFile.java b/sched/src/com/android/sched/vfs/VFile.java index 46df79f8..75553e59 100644 --- a/sched/src/com/android/sched/vfs/VFile.java +++ b/sched/src/com/android/sched/vfs/VFile.java @@ -32,4 +32,6 @@ public interface VFile extends VElement, InputStreamProvider, OutputStreamProvid String getDigest(); void delete() throws CannotDeleteFileException; + @Nonnull + VPath getPathFromRoot(); } diff --git a/sched/src/com/android/sched/vfs/WriteZipFS.java b/sched/src/com/android/sched/vfs/WriteZipFS.java index dc5845b4..8135220c 100644 --- a/sched/src/com/android/sched/vfs/WriteZipFS.java +++ b/sched/src/com/android/sched/vfs/WriteZipFS.java @@ -331,4 +331,20 @@ public class WriteZipFS extends BaseVFS<ZipVDir, ZipVFile> implements VFS { } } } + + @Override + @Nonnull + VPath getPathFromDir(@Nonnull ZipVDir parent, @Nonnull ZipVFile file) { + String fileEntryPath = file.getZipEntry().getName(); + String parentEntryPath = parent.getZipEntry().getName(); + assert fileEntryPath.contains(parentEntryPath); + String newPath = fileEntryPath.substring(fileEntryPath.indexOf(parentEntryPath)); + return new VPath(newPath, '/'); + } + + @Override + @Nonnull + VPath getPathFromRoot(@Nonnull ZipVFile file) { + return getPathFromDir(root, file); + } } |