@Override public void open(final Host host, final Path workdir) throws AccessDeniedException { throw new LocalAccessDeniedException("Disabled"); } }
protected InputStream getInputStream(final String path) throws LocalAccessDeniedException { try { final FileChannel channel = FileChannel.open(Paths.get(path), StandardOpenOption.READ); return new SeekableByteChannelInputStream(channel); } catch(IOException e) { throw new LocalAccessDeniedException(e.getMessage(), e); } }
@Override public void trash(Local file) throws LocalAccessDeniedException { try { Files.delete(Paths.get(file.getAbsolute())); } catch(IOException e) { throw new LocalAccessDeniedException(String.format("Failed to move %s to Trash", file.getName()), e); } } }
public void setModificationDate(final long timestamp) throws AccessDeniedException { if(timestamp < 0) { return; } try { Files.setLastModifiedTime(Paths.get(path), FileTime.fromMillis(timestamp)); } catch(IOException e) { throw new LocalAccessDeniedException(String.format("Cannot change timestamp of %s", path), e); } }
public void setPermission(final Permission permission) throws AccessDeniedException { if(FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) { try { Files.setPosixFilePermissions(Paths.get(path), PosixFilePermissions.fromString(permission.getSymbol())); } catch(IllegalArgumentException | IOException e) { throw new LocalAccessDeniedException(String.format("Cannot change permissions of %s", path), e); } } }
protected OutputStream getOutputStream(final String path, final boolean append) throws LocalAccessDeniedException { try { final Set<OpenOption> options = new HashSet<>(); options.add(StandardOpenOption.WRITE); if(!this.exists()) { options.add(StandardOpenOption.CREATE); } if(append) { options.add(StandardOpenOption.APPEND); } else { options.add(StandardOpenOption.TRUNCATE_EXISTING); } final FileChannel channel = FileChannel.open(Paths.get(path), options); return Channels.newOutputStream(channel); } catch(IOException e) { throw new LocalAccessDeniedException(e.getMessage(), e); } }
@Override public BackgroundException map(final IOException e) { final StringBuilder buffer = new StringBuilder(); this.append(buffer, e.getMessage()); if(e instanceof NoSuchFileException) { return new NotfoundException(buffer.toString(), e); } if(e instanceof FileSystemException) { return new LocalAccessDeniedException(buffer.toString(), e); } return this.wrap(e, buffer); } }
@Override public void mkdir(final Local file) throws AccessDeniedException { try { Files.createDirectories(Paths.get(file.getAbsolute())); } catch(IOException e) { throw new LocalAccessDeniedException(MessageFormat.format(LocaleFactory.localizedString( "Cannot create folder {0}", "Error"), file.getName()), e); } } }
/** * Delete the file */ public void delete() throws AccessDeniedException, NotfoundException { try { Files.delete(Paths.get(path)); } catch(NoSuchFileException e) { throw new LocalNotfoundException(String.format("Delete %s failed", path), e); } catch(IOException e) { throw new LocalAccessDeniedException(MessageFormat.format( LocaleFactory.localizedString("Cannot delete {0}", "Error"), this.getName()), e); } }
@Override public void removeAll() throws LocalAccessDeniedException { if(log.isDebugEnabled()) { log.debug(String.format("Remove all our added items in %s", this.forList(list))); } if(!this.removeAllItems(this.forList(list))) { throw new LocalAccessDeniedException(String.format("Failure removing all items from %s", this.forList(list))); } }
@Override public void add(final Local file) throws LocalAccessDeniedException { if(log.isDebugEnabled()) { log.debug(String.format("Add %s to %s", file, this.forList(list))); } if(!this.addItem(file.getAbsolute(), this.forList(list))) { throw new LocalAccessDeniedException(String.format("Failure adding %s to %s", file, this.forList(list))); } }
@Override public void remove(final Local file) throws LocalAccessDeniedException { if(log.isDebugEnabled()) { log.debug(String.format("Remove %s in %s", file, this.forList(list))); } if(!this.removeItem(file.getAbsolute(), this.forList(list))) { throw new LocalAccessDeniedException(String.format("Failure removing %s from %s", file, this.forList(list))); } }
@Override protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException { NSDictionary serialized = NSDictionary.dictionaryWithContentsOfFile(file.getAbsolute()); if(null == serialized) { throw new LocalAccessDeniedException(String.format("Invalid bookmark file %s", file)); } this.parse(protocols, serialized); }
/** * Set the kMDItemWhereFroms on the file. * * @param dataUrl Href where the file was downloaded from */ @Override public void setWhereFrom(final Local file, final String dataUrl) throws LocalAccessDeniedException { synchronized(lock) { if(StringUtils.isBlank(dataUrl)) { log.warn("No data url given"); return; } if(!this.setWhereFrom(file.getAbsolute(), dataUrl)) { throw new LocalAccessDeniedException(file.getAbsolute()); } } }
public void rename(final Local renamed) throws AccessDeniedException { try { try { Files.move(Paths.get(path), Paths.get(renamed.getAbsolute()), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); } // Catch generic exception due to a bug in IKVM moving files containing special characters // in conjunction with the atomic move option catch(FileSystemException e) { // Copying file to different disk is not possible with atomic move. // Moving directory to an already existing target will throw exists exception with atomic move flag. Files.move(Paths.get(path), Paths.get(renamed.getAbsolute()), StandardCopyOption.REPLACE_EXISTING); } } catch(IOException e) { throw new LocalAccessDeniedException(MessageFormat.format( LocaleFactory.localizedString("Cannot rename {0}", "Error"), this.getName()), e); } path = renamed.getAbsolute(); }
@Override public void symlink(final Local file, final String target) throws AccessDeniedException { final ObjCObjectByReference error = new ObjCObjectByReference(); final boolean success = NSFileManager.defaultManager().createSymbolicLinkAtPath_withDestinationPath_error( file.getAbsolute(), target, error); if(!success) { final NSError f = error.getValueAs(NSError.class); if(null == f) { throw new LocalAccessDeniedException(String.format("%s %s", LocaleFactory.localizedString("Cannot create file", "Error"), file.getAbsolute())); } throw new LocalAccessDeniedException(String.format("%s", f.localizedDescription())); } if(log.isDebugEnabled()) { log.debug(String.format("Created symbolic link %s with target %s", file, target)); } } }
/** * @param interactive Prompt to resolve bookmark of file outside of sandbox with choose panel */ @Override public NSURL lock(final boolean interactive) throws AccessDeniedException { final NSURL resolved = resolver.resolve(this, interactive); if(null == resolved) { // Ignore failure resolving path return null; // NSURL.fileURLWithPath(this.getAbsolute()); } if(resolved.respondsToSelector(Foundation.selector("startAccessingSecurityScopedResource"))) { if(!resolved.startAccessingSecurityScopedResource()) { throw new LocalAccessDeniedException(String.format("Failure accessing security scoped resource for %s", this)); } } return resolved; }
@Override public void write(final S item, final Local file) throws AccessDeniedException { if(!item.<NSDictionary>serialize(SerializerFactory.get()).writeToFile(file.getAbsolute())) { throw new LocalAccessDeniedException(String.format("Cannot create file %s", file.getAbsolute())); } } }
/** * @param file A valid bookmark dictionary * @return Null if the file cannot be deserialized * @throws AccessDeniedException If the file is not readable */ @Override public S read(final Local file) throws AccessDeniedException { if(!file.exists()) { throw new LocalAccessDeniedException(file.getAbsolute()); } if(!file.isFile()) { throw new LocalAccessDeniedException(file.getAbsolute()); } NSDictionary dict = NSDictionary.dictionaryWithContentsOfFile(file.getAbsolute()); if(null == dict) { throw new AccessDeniedException(String.format("Failure parsing file %s", file.getName())); } return this.deserialize(dict); }
@Override public void write(final Collection<S> collection, final Local file) throws AccessDeniedException { NSMutableArray list = NSMutableArray.array(); for(S bookmark : collection) { list.addObject(bookmark.<NSDictionary>serialize(SerializerFactory.get())); } if(!list.writeToFile(file.getAbsolute())) { throw new LocalAccessDeniedException(String.format("Cannot create file %s", file.getAbsolute())); } }