use of org.eclipse.che.api.vfs.LockedFileFinder 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;
}
use of org.eclipse.che.api.vfs.LockedFileFinder 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()));
}
}
use of org.eclipse.che.api.vfs.LockedFileFinder in project che by eclipse.
the class LocalVirtualFileSystem method rename.
LocalVirtualFile rename(LocalVirtualFile virtualFile, String newName, String lockToken) throws ForbiddenException, ConflictException, ServerException {
checkName(newName);
if (virtualFile.isRoot()) {
throw new ForbiddenException("Unable rename root folder");
}
if (virtualFile.isFile()) {
if (fileIsLockedAndLockTokenIsInvalid(virtualFile, lockToken)) {
throw new ForbiddenException(String.format("Unable rename file '%s'. File is locked", virtualFile.getPath()));
}
} else {
final List<VirtualFile> lockedFiles = new LockedFileFinder(virtualFile).findLockedFiles();
if (!lockedFiles.isEmpty()) {
throw new ForbiddenException(String.format("Unable rename folder '%s'. Child items '%s' are locked", virtualFile.getPath(), lockedFiles));
}
}
if (newName.equals(virtualFile.getName())) {
return virtualFile;
} else {
final Path newPath = virtualFile.getPath().getParent().newPath(newName);
final LocalVirtualFile newVirtualFile = new LocalVirtualFile(new File(ioRoot, toIoPath(newPath)), newPath, this);
if (newVirtualFile.exists()) {
throw new ConflictException(String.format("Item '%s' already exists", newVirtualFile.getName()));
}
doCopy(virtualFile, newVirtualFile);
addInSearcher(newVirtualFile);
final Path path = virtualFile.getPath();
final boolean isFile = virtualFile.isFile();
doDelete(virtualFile, lockToken);
deleteInSearcher(path, isFile);
return newVirtualFile;
}
}
use of org.eclipse.che.api.vfs.LockedFileFinder in project che by eclipse.
the class LocalVirtualFileSystem method move.
LocalVirtualFile move(LocalVirtualFile virtualFile, LocalVirtualFile parent, String name, boolean overwrite, String lockToken) throws ForbiddenException, ConflictException, ServerException {
if (virtualFile.isRoot()) {
throw new ForbiddenException("Unable move root folder");
}
if (virtualFile.getPath().equals(parent.getPath())) {
throw new ForbiddenException("Item cannot be moved to itself");
}
if (!parent.isFolder()) {
throw new ForbiddenException("Unable move. Item specified as parent is not a folder");
}
final Path sourcePath = virtualFile.getPath();
final Path parentPath = parent.getPath();
if (virtualFile.isFolder() && parent.getPath().isChild(virtualFile.getPath())) {
throw new ForbiddenException(String.format("Unable move item '%s' to '%s'. Item may not have itself as parent", sourcePath, parentPath));
}
if (virtualFile.isFile()) {
if (fileIsLockedAndLockTokenIsInvalid(virtualFile, lockToken)) {
throw new ForbiddenException(String.format("Unable move file '%s'. File is locked", sourcePath));
}
} else {
final List<VirtualFile> lockedFiles = new LockedFileFinder(virtualFile).findLockedFiles();
if (!lockedFiles.isEmpty()) {
throw new ForbiddenException(String.format("Unable move folder '%s'. Child items '%s' are locked", virtualFile, lockedFiles));
}
}
String newName = isNullOrEmpty(name) ? virtualFile.getName() : name;
final Path newPath = parent.getPath().newPath(newName);
LocalVirtualFile newVirtualFile = new LocalVirtualFile(new File(ioRoot, toIoPath(newPath)), newPath, this);
if (newVirtualFile.exists()) {
if (overwrite) {
delete(newVirtualFile, null);
} else {
throw new ConflictException(String.format("Item '%s' already exists", newPath));
}
}
doCopy(virtualFile, newVirtualFile);
addInSearcher(newVirtualFile);
final Path path = virtualFile.getPath();
final boolean isFile = virtualFile.isFile();
doDelete(virtualFile, lockToken);
deleteInSearcher(path, isFile);
return newVirtualFile;
}
use of org.eclipse.che.api.vfs.LockedFileFinder in project che by eclipse.
the class MemoryVirtualFile method rename.
@Override
public VirtualFile rename(String newName, String lockToken) throws ForbiddenException, ConflictException, ServerException {
checkExistence();
checkName(newName);
boolean isFile = isFile();
if (isRoot()) {
throw new ForbiddenException("We were unable to rename a root folder.");
}
final Path myPath = getPath();
final boolean isFolder = isFolder();
if (isFolder) {
final List<VirtualFile> lockedFiles = new LockedFileFinder(this).findLockedFiles();
if (!lockedFiles.isEmpty()) {
throw new ForbiddenException(String.format("Unable rename item '%s'. Child items '%s' are locked", getName(), lockedFiles));
}
} else {
if (fileIsLockedAndLockTokenIsInvalid(lockToken)) {
throw new ForbiddenException(String.format("We were unable to rename an item '%s'." + " The item is currently locked by the system", getPath()));
}
}
if (parent.children.get(newName) != null) {
throw new ConflictException(String.format("Item '%s' already exists", newName));
}
parent.children.remove(name);
parent.children.put(newName, this);
name = newName;
lock = null;
lastModificationDate = System.currentTimeMillis();
deleteFromSearcher(myPath, isFile);
addInSearcher(this);
return this;
}
Aggregations