Permalink
Browse files

Implement MKDIR webdav command

  • Loading branch information...
justinsb committed Dec 20, 2013
1 parent e3dafd1 commit 1f34ba76e6947522dbb198111486fcb75747abc3
@@ -200,10 +200,17 @@ public void createNewFile(FsPath parentPath, ByteString name, InodeData.Builder
inode.setLength(length);
createNewFile(parentPath, name, inode, overwrite);
createNewEntry(parentPath, name, inode, overwrite);
}
private void createNewFile(FsPath parent, ByteString name, InodeData.Builder inode, boolean overwrite)
public void createNewFolder(FsPath parentPath, ByteString name, InodeData.Builder inode, boolean overwrite)
throws IOException, FsException {
Preconditions.checkState(inode.getChunkCount() == 0);
createNewEntry(parentPath, name, inode, overwrite);
}
private void createNewEntry(FsPath parent, ByteString name, InodeData.Builder inode, boolean overwrite)
throws IOException, FsException {
// TODO: We really should do this in a transaction; we can lose inodes if we crash...
@@ -10,7 +10,7 @@
public class Inode {
static final int S_IFMT = 0170000;
static final int S_IFDIR = 0040000;
public static final int S_IFDIR = 0040000;
final long id;
final InodeData data;
@@ -5,7 +5,6 @@
import io.netty.channel.ChannelFuture;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
@@ -241,7 +240,7 @@ HttpResponse buildXmlResponse(HttpResponseStatus status, XmlSerializable payload
String path = request.getUri();
if (request.getMethod().name().equals("MKCOL") || request.getMethod() == HttpMethod.PUT) {
if (shouldResolveParent()) {
path = Urls.getParentPath(path);
if (Strings.isNullOrEmpty(path)) {
return Futures.immediateFuture(null);
@@ -285,6 +284,10 @@ public FsPath call() throws Exception {
});
}
protected boolean shouldResolveParent() {
return false;
}
private ListeningExecutorService getExecutor() {
return requestHandler.getExecutor();
}
@@ -0,0 +1,55 @@
package com.cloudata.files.webdav;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpResponseStatus;
import com.cloudata.files.FilesModel.InodeData;
import com.cloudata.files.fs.FsClient;
import com.cloudata.files.fs.FsPath;
import com.cloudata.files.fs.Inode;
import com.google.protobuf.ByteString;
public class MkdirHandler extends MethodHandler {
public MkdirHandler(WebdavRequestHandler requestHandler) {
super(requestHandler);
}
@Override
protected boolean shouldResolveParent() {
return true;
}
@Override
protected HttpObject doAction(FsPath parentPath) throws Exception {
WebdavRequest request = getRequest();
FsClient fsClient = getFsClient();
if (!parentPath.isFolder()) {
throw new WebdavResponseException(HttpResponseStatus.CONFLICT);
}
if (request.hasContent()) {
throw new UnsupportedOperationException();
}
String path = request.getUri();
String name = Urls.getLastPathComponent(path);
InodeData.Builder inode = InodeData.newBuilder();
long now = System.currentTimeMillis();
inode.setCreateTime(now);
inode.setModifiedTime(now);
int mode = Inode.S_IFDIR;
inode.setMode(mode);
boolean overwrite = false;
fsClient.createNewFolder(parentPath, ByteString.copyFromUtf8(name), inode, overwrite);
return buildFullResponse(HttpResponseStatus.CREATED);
}
}
@@ -18,8 +18,13 @@ public PutHandler(WebdavRequestHandler requestHandler) {
}
@Override
public HttpObject doAction(FsPath fsPath) throws Exception {
if (!fsPath.isFolder()) {
protected boolean shouldResolveParent() {
return true;
}
@Override
public HttpObject doAction(FsPath parentPath) throws Exception {
if (!parentPath.isFolder()) {
throw new WebdavResponseException(HttpResponseStatus.CONFLICT);
}
@@ -47,7 +52,7 @@ public HttpObject doAction(FsPath fsPath) throws Exception {
SimpleChunkAccumulator simpleContent = (SimpleChunkAccumulator) content;
ByteSource source = simpleContent.getByteSource();
getFsClient().createNewFile(fsPath, ByteString.copyFromUtf8(newName), inode, source, overwrite);
getFsClient().createNewFile(parentPath, ByteString.copyFromUtf8(newName), inode, source, overwrite);
} else {
throw new IllegalStateException();
}
@@ -89,7 +89,7 @@ public void close() throws IOException {
} else if (request.getMethod().name().equals("PROPFIND")) {
handler = new PropfindHandler(this);
} else if (request.getMethod().name().equals("MKCOL")) {
throw new UnsupportedOperationException();
handler = new MkdirHandler(this);
} else if (request.getMethod() == HttpMethod.PUT) {
handler = new PutHandler(this);
} else if (request.getMethod() == HttpMethod.DELETE) {

0 comments on commit 1f34ba7

Please sign in to comment.