Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement create, append and mkdirs methods

  • Loading branch information...
commit 9c127ba2152405fb73a4dfaf84f31c045dbd423e 1 parent d39ea64
@wnagele authored
View
33 src/main/java/org/apache/hadoop/fs/sftp/SFTPFileSystem.java
@@ -180,17 +180,44 @@ private SFTPInputStream openInternal(Path file) throws IOException {
@Override
public FSDataOutputStream create(Path file, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
- throw new UnsupportedOperationException();
+ return createInternal(file, permission, overwrite, SFTPv3Client.SSH_FXF_CREAT | SFTPv3Client.SSH_FXF_WRITE | SFTPv3Client.SSH_FXF_TRUNC);
}
@Override
public FSDataOutputStream append(Path file, int bufferSize, Progressable progress) throws IOException {
- throw new UnsupportedOperationException();
+ return createInternal(file, null, true, SFTPv3Client.SSH_FXF_WRITE | SFTPv3Client.SSH_FXF_APPEND);
+ }
+
+ protected FSDataOutputStream createInternal(Path file, FsPermission permission, boolean overwrite, int flags) throws IOException {
+ if (exists(file) && !overwrite)
+ throw new IOException("File " + file + " exists");
+
+ Path parent = file.getParent();
+ if (!exists(parent))
+ mkdirs(parent);
+
+ SFTPv3FileAttributes attrs = null;
+ if (permission != null) {
+ attrs = new SFTPv3FileAttributes();
+ attrs.permissions = new Short(permission.toShort()).intValue();
+ }
+
+ String path = file.toUri().getPath();
+ SFTPv3FileHandle handle = client.openFile(path, flags, attrs);
+ SFTPOutputStream os = new SFTPOutputStream(handle, statistics);
+ return new FSDataOutputStream(os, statistics);
}
@Override
public boolean mkdirs(Path file, FsPermission permission) throws IOException {
- throw new UnsupportedOperationException();
+ if (!exists(file)) {
+ Path parent = file.getParent();
+ if (parent == null || mkdirs(parent, permission)) {
+ String path = file.toUri().getPath();
+ client.mkdir(path, permission.toShort());
+ }
+ }
+ return true;
}
@Override
View
48 src/main/java/org/apache/hadoop/fs/sftp/SFTPOutputStream.java
@@ -0,0 +1,48 @@
+package org.apache.hadoop.fs.sftp;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.hadoop.fs.FileSystem;
+
+import ch.ethz.ssh2.SFTPv3Client;
+import ch.ethz.ssh2.SFTPv3FileAttributes;
+import ch.ethz.ssh2.SFTPv3FileHandle;
+
+public class SFTPOutputStream extends OutputStream {
+ private SFTPv3FileHandle handle;
+ private FileSystem.Statistics stats;
+ private SFTPv3Client client;
+ private long pos;
+
+ public SFTPOutputStream(SFTPv3FileHandle handle, FileSystem.Statistics stats) throws IOException {
+ this.handle = handle;
+ this.stats = stats;
+ this.client = handle.getClient();
+
+ SFTPv3FileAttributes attrs = client.fstat(handle);
+ pos += attrs.size;
+ }
+
+ @Override
+ public void close() throws IOException {
+ super.close();
+ client.closeFile(handle);
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) throws IOException {
+ client.write(handle, pos, b, off, len);
+ pos += len;
+ if (stats != null)
+ stats.incrementBytesWritten(len);
+ }
+
+ @Override
+ public void write(int b) throws IOException {
+ client.write(handle, pos, new byte[] { (byte)b }, 0, 1);
+ pos++;
+ if (stats != null)
+ stats.incrementBytesWritten(1);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.