Search in sources :

Example 26 with VirtualFile

use of org.eclipse.che.api.vfs.VirtualFile in project che by eclipse.

the class MemoryVirtualFile method moveTo.

@Override
public VirtualFile moveTo(VirtualFile parent, String newName, boolean overwrite, String lockToken) throws ForbiddenException, ConflictException, ServerException {
    checkExistence();
    MemoryVirtualFile memoryParent = (MemoryVirtualFile) parent;
    memoryParent.checkExistence();
    if (isRoot()) {
        throw new ForbiddenException("Unable move root folder");
    }
    if (!parent.isFolder()) {
        throw new ForbiddenException("Unable move item. Item specified as parent is not a folder");
    }
    if (newName == null || newName.trim().isEmpty()) {
        newName = this.getName();
    }
    final boolean isFile = isFile();
    final Path myPath = getPath();
    final Path newParentPath = parent.getPath();
    final boolean folder = isFolder();
    if (folder) {
        if (newParentPath.isChild(myPath)) {
            throw new ForbiddenException(String.format("Unable move item %s to %s. Item may not have itself as parent", myPath, newParentPath));
        }
        final List<VirtualFile> lockedFiles = new LockedFileFinder(this).findLockedFiles();
        if (!lockedFiles.isEmpty()) {
            throw new ForbiddenException(String.format("Unable move item '%s'. Child items '%s' are locked", getName(), lockedFiles));
        }
    } else if (fileIsLockedAndLockTokenIsInvalid(lockToken)) {
        throw new ForbiddenException(String.format("Unable move item %s. Item is locked", myPath));
    }
    if (overwrite) {
        MemoryVirtualFile existedItem = memoryParent.children.get(newName);
        if (existedItem != null) {
            existedItem.delete();
        }
    }
    if (memoryParent.children.containsKey(newName)) {
        throw new ConflictException(String.format("Item '%s' already exists", parent.getPath().newPath(newName)));
    }
    this.parent.children.remove(name);
    memoryParent.children.put(newName, this);
    this.parent = memoryParent;
    this.name = newName;
    lock = null;
    deleteFromSearcher(myPath, isFile);
    addInSearcher(this);
    return this;
}
Also used : Path(org.eclipse.che.api.vfs.Path) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) LockedFileFinder(org.eclipse.che.api.vfs.LockedFileFinder) ConflictException(org.eclipse.che.api.core.ConflictException)

Example 27 with VirtualFile

use of org.eclipse.che.api.vfs.VirtualFile in project che by eclipse.

the class DefaultFileWatcherNotificationHandler method convertToVirtualFile.

private VirtualFile convertToVirtualFile(File root, String subPath, boolean isDir) {
    try {
        LocalVirtualFileSystem virtualFileSystem = (LocalVirtualFileSystem) virtualFileSystemProvider.getVirtualFileSystem(true);
        Path vfsPath = Path.of(subPath);
        VirtualFile virtualFile = virtualFileSystem.getRoot().getChild(vfsPath);
        if (virtualFile == null) {
            virtualFile = new DeletedLocalVirtualFile(new File(root, subPath), ROOT.newPath(vfsPath), virtualFileSystem, isDir);
        }
        return virtualFile;
    } catch (ServerException e) {
        LOG.warn(e.getMessage());
    }
    return null;
}
Also used : Path(org.eclipse.che.api.vfs.Path) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) ServerException(org.eclipse.che.api.core.ServerException) File(java.io.File) VirtualFile(org.eclipse.che.api.vfs.VirtualFile)

Example 28 with VirtualFile

use of org.eclipse.che.api.vfs.VirtualFile in project che by eclipse.

the class IndexedFileUpdateConsumer method accept.

@Override
public void accept(Path path) {
    try {
        VirtualFileSystem virtualFileSystem = vfsProvider.getVirtualFileSystem();
        SearcherProvider searcherProvider = virtualFileSystem.getSearcherProvider();
        Searcher searcher = searcherProvider.getSearcher(virtualFileSystem);
        Path innerPath = root.toPath().relativize(path);
        org.eclipse.che.api.vfs.Path vfsPath = org.eclipse.che.api.vfs.Path.of(innerPath.toString());
        VirtualFile child = virtualFileSystem.getRoot().getChild(vfsPath);
        if (child != null) {
            searcher.update(child);
        }
    } catch (ServerException e) {
        LOG.error("Issue happened during updating modified file in index", e);
    }
}
Also used : Path(java.nio.file.Path) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) ServerException(org.eclipse.che.api.core.ServerException) VirtualFileSystem(org.eclipse.che.api.vfs.VirtualFileSystem) Searcher(org.eclipse.che.api.vfs.search.Searcher) SearcherProvider(org.eclipse.che.api.vfs.search.SearcherProvider)

Example 29 with VirtualFile

use of org.eclipse.che.api.vfs.VirtualFile in project che by eclipse.

the class LocalVirtualFileSystem method doGetChildren.

private List<VirtualFile> doGetChildren(LocalVirtualFile parent, FilenameFilter ioFileFilter, VirtualFileFilter vfsFilter) throws ServerException {
    if (ioFileFilter == null) {
        ioFileFilter = IoUtil.ANY_FILTER;
    }
    final String[] names = parent.toIoFile().list(ioFileFilter);
    if (names == null) {
        throw new ServerException(String.format("Unable get children of '%s'", parent.getPath()));
    }
    if (vfsFilter == null) {
        vfsFilter = VirtualFileFilter.ACCEPT_ALL;
    }
    final List<VirtualFile> children = newArrayListWithCapacity(names.length);
    for (String name : names) {
        final Path childPath = parent.getPath().newPath(name);
        final LocalVirtualFile child = new LocalVirtualFile(new File(ioRoot, toIoPath(childPath)), childPath, this);
        if (vfsFilter.accept(child)) {
            children.add(child);
        }
    }
    return children;
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) Path(org.eclipse.che.api.vfs.Path) ServerException(org.eclipse.che.api.core.ServerException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Example 30 with VirtualFile

use of org.eclipse.che.api.vfs.VirtualFile in project che by eclipse.

the class LocalVirtualFileSystem method doDelete.

private void doDelete(LocalVirtualFile virtualFile, String lockToken) throws ForbiddenException, ServerException {
    if (virtualFile.isFolder()) {
        final List<VirtualFile> lockedFiles = new LockedFileFinder(virtualFile).findLockedFiles();
        if (!lockedFiles.isEmpty()) {
            throw new ForbiddenException(String.format("Unable delete folder '%s'. Child items '%s' are locked", virtualFile.getPath(), lockedFiles));
        }
    } else if (fileIsLockedAndLockTokenIsInvalid(virtualFile, lockToken)) {
        throw new ForbiddenException(String.format("Unable delete file '%s'. File is locked", virtualFile.getPath()));
    }
    cleanUpCaches();
    final File fileLockIoFile = getFileLockIoFile(virtualFile.getPath());
    if (fileLockIoFile.delete()) {
        if (fileLockIoFile.exists()) {
            LOG.error("Unable delete lock file {}", fileLockIoFile);
            throw new ServerException(String.format("Unable delete item '%s'", virtualFile.getPath()));
        }
    }
    final File metadataIoFile = getMetadataIoFile(virtualFile.getPath());
    if (metadataIoFile.delete()) {
        if (metadataIoFile.exists()) {
            LOG.error("Unable delete metadata file {}", metadataIoFile);
            throw new ServerException(String.format("Unable delete item '%s'", virtualFile.getPath()));
        }
    }
    if (!deleteRecursive(virtualFile.toIoFile())) {
        LOG.error("Unable delete file {}", virtualFile.toIoFile());
        throw new ServerException(String.format("Unable delete item '%s'", virtualFile.getPath()));
    }
}
Also used : VirtualFile(org.eclipse.che.api.vfs.VirtualFile) LockedFileFinder(org.eclipse.che.api.vfs.LockedFileFinder) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) VirtualFile(org.eclipse.che.api.vfs.VirtualFile) File(java.io.File)

Aggregations

VirtualFile (org.eclipse.che.api.vfs.VirtualFile)321 Test (org.junit.Test)267 Path (org.eclipse.che.api.vfs.Path)62 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)54 ByteArrayInputStream (java.io.ByteArrayInputStream)32 ConflictException (org.eclipse.che.api.core.ConflictException)28 VirtualFileSystem (org.eclipse.che.api.vfs.VirtualFileSystem)27 MemoryVirtualFileSystem (org.eclipse.che.api.vfs.impl.memory.MemoryVirtualFileSystem)22 QueryExpression (org.eclipse.che.api.vfs.search.QueryExpression)22 ServerException (org.eclipse.che.api.core.ServerException)18 Test (org.testng.annotations.Test)18 File (java.io.File)17 Archiver (org.eclipse.che.api.vfs.Archiver)14 InputStream (java.io.InputStream)10 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)7 OutputStream (java.io.OutputStream)6 LockedFileFinder (org.eclipse.che.api.vfs.LockedFileFinder)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Map (java.util.Map)5