@Before public void setPathHelper() throws Exception { PathHelper helper = new PathHelper(new PathHelper.Canonicalizer() { /** * Very basic, it does not try to canonicalize relative bits in the middle of a path. */ @Override public String canonicalize(String path) throws IOException { if ("".equals(path) || ".".equals(path) || "./".equals(path)) return "/home/me"; if ("..".equals(path) || "../".equals(path)) return "/home"; return path; } }, DEFAULT_PATH_SEPARATOR); when(sftpEngine.getPathHelper()).thenReturn(helper); when(sftpEngine.stat("/")).thenReturn(new FileAttributes.Builder().withType(FileMode.Type.DIRECTORY).build()); when(sftpEngine.getLoggerFactory()).thenReturn(LoggerFactory.DEFAULT); }
sftp.setattr(path, new FileAttributes.Builder() .withAtimeMtime(lastAccessDate, lastModificationDate) .build());
@Override public Void create() throws Exception { final AtomicReference<InputStream> inputStreamRef = new AtomicReference<InputStream>(); sftp = acquire(sftpConnection); try { sftp.put(new InMemorySourceFile() { @Override public String getName() { return path; } @Override public long getLength() { return length; } @Override public InputStream getInputStream() throws IOException { InputStream contents = contentsSupplier.get(); inputStreamRef.set(contents); return contents; } }, path); sftp.chmod(path, permissionsMask); if (lastAccessDate > 0) { sftp.setattr(path, new FileAttributes.Builder() .withAtimeMtime(lastAccessDate, lastModificationDate) .build()); } } finally { closeWhispering(inputStreamRef.get(), this); } return null; }
public FileAttributes readFileAttributes() throws SFTPException { final FileAttributes.Builder builder = new FileAttributes.Builder(); try { final int mask = readUInt32AsInt(); if (FileAttributes.Flag.SIZE.isSet(mask)) builder.withSize(readUInt64()); if (FileAttributes.Flag.UIDGID.isSet(mask)) builder.withUIDGID(readUInt32AsInt(), readUInt32AsInt()); if (FileAttributes.Flag.MODE.isSet(mask)) builder.withPermissions(readUInt32AsInt()); if (FileAttributes.Flag.ACMODTIME.isSet(mask)) builder.withAtimeMtime(readUInt32AsInt(), readUInt32AsInt()); if (FileAttributes.Flag.EXTENDED.isSet(mask)) { final int extCount = readUInt32AsInt(); for (int i = 0; i < extCount; i++) builder.withExtended(readString(), readString()); } } catch (BufferException be) { throw new SFTPException(be); } return builder.build(); }
public FileAttributes readFileAttributes() throws SFTPException { final FileAttributes.Builder builder = new FileAttributes.Builder(); try { final int mask = readUInt32AsInt(); if (FileAttributes.Flag.SIZE.isSet(mask)) builder.withSize(readUInt64()); if (FileAttributes.Flag.UIDGID.isSet(mask)) builder.withUIDGID(readUInt32AsInt(), readUInt32AsInt()); if (FileAttributes.Flag.MODE.isSet(mask)) builder.withPermissions(readUInt32AsInt()); if (FileAttributes.Flag.ACMODTIME.isSet(mask)) builder.withAtimeMtime(readUInt32AsInt(), readUInt32AsInt()); if (FileAttributes.Flag.EXTENDED.isSet(mask)) { final int extCount = readUInt32AsInt(); for (int i = 0; i < extCount; i++) builder.withExtended(readString(), readString()); } } catch (BufferException be) { throw new SFTPException(be); } return builder.build(); }
@Override public Path touch(final Path file, final TransferStatus status) throws BackgroundException { if(file.isFile()) { try { final FileAttributes attrs; if(Permission.EMPTY != status.getPermission()) { attrs = new FileAttributes.Builder().withPermissions(Integer.parseInt(status.getPermission().getMode(), 8)).build(); } else { attrs = FileAttributes.EMPTY; } final RemoteFile handle = session.sftp().open(file.getAbsolute(), EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE), attrs); handle.close(); } catch(IOException e) { throw new SFTPExceptionMappingService().map("Cannot create file {0}", e, file); } } return new Path(file.getParent(), file.getName(), file.getType(), new SFTPAttributesFinderFeature(session).find(file)); }
@Override public Path mkdir(final Path folder, final String region, final TransferStatus status) throws BackgroundException { try { final FileAttributes attrs; if(Permission.EMPTY != status.getPermission()) { attrs = new FileAttributes.Builder().withPermissions(Integer.parseInt(status.getPermission().getMode(), 8)).build(); } else { attrs = FileAttributes.EMPTY; } session.sftp().makeDir(folder.getAbsolute(), attrs); } catch(IOException e) { throw new SFTPExceptionMappingService().map("Cannot create folder {0}", e, folder); } return new Path(folder.getParent(), folder.getName(), folder.getType(), new SFTPAttributesFinderFeature(session).find(folder)); }
@Override public void setTimestamp(final Path file, final Long modified) throws BackgroundException { try { // We must both set the accessed and modified time. See AttribFlags.SSH_FILEXFER_ATTR_V3_ACMODTIME // All times are represented as seconds from Jan 1, 1970 in UTC. final FileAttributes attrs = new FileAttributes.Builder().withAtimeMtime( System.currentTimeMillis() / 1000, modified / 1000 ).build(); session.sftp().setAttributes(file.getAbsolute(), attrs); } catch(IOException e) { throw new SFTPExceptionMappingService().map("Cannot change timestamp of {0}", e, file); } } }
public void setLength(long len) throws IOException { setAttributes(new FileAttributes.Builder().withSize(len).build()); }
public void chgrp(String path, int gid) throws IOException { setattr(path, new FileAttributes.Builder().withUIDGID(uid(path), gid).build()); }
public void chown(String path, int uid) throws IOException { setattr(path, new FileAttributes.Builder().withUIDGID(uid, gid(path)).build()); }
public void chmod(String path, int perms) throws IOException { setattr(path, new FileAttributes.Builder().withPermissions(perms).build()); }
public void truncate(String path, long size) throws IOException { setattr(path, new FileAttributes.Builder().withSize(size).build()); }
public void chmod(String path, int perms) throws IOException { setattr(path, new FileAttributes.Builder().withPermissions(perms).build()); }
public void truncate(String path, long size) throws IOException { setattr(path, new FileAttributes.Builder().withSize(size).build()); }
@Override public void setUnixPermission(final Path file, final Permission permission) throws BackgroundException { final FileAttributes attr = new FileAttributes.Builder() .withPermissions(Integer.parseInt(permission.getMode(), 8)) .build(); try { session.sftp().setAttributes(file.getAbsolute(), attr); } catch(IOException e) { throw new SFTPExceptionMappingService().map("Failure to write attributes of {0}", e, file); } } }
public void chgrp(String path, int gid) throws IOException { setattr(path, new FileAttributes.Builder().withUIDGID(uid(path), gid).build()); }
public void chown(String path, int uid) throws IOException { setattr(path, new FileAttributes.Builder().withUIDGID(uid, gid(path)).build()); }
public void setLength(long len) throws IOException { setAttributes(new FileAttributes.Builder().withSize(len).build()); }
private FileAttributes getAttributes(LocalSourceFile local) throws IOException { final FileAttributes.Builder builder = new FileAttributes.Builder().withPermissions(local.getPermissions()); if (local.providesAtimeMtime()) builder.withAtimeMtime(local.getLastAccessTime(), local.getLastModifiedTime()); return builder.build(); }