diff options
| author | hilimd <68371223+hilimd@users.noreply.github.com> | 2021-05-08 13:46:12 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-08 13:46:12 +0800 |
| commit | aabea37af1835af7ec53fb9b6a56295f2a591785 (patch) | |
| tree | db57540c45309f15d0efdaed274f84ece94f5a05 /other/java | |
| parent | 6b8892c5acc8c44c3183e0a4e1b9b673a58c9729 (diff) | |
| parent | 8f8738867f10c98d59b11eaf165e1c6028d7b1d0 (diff) | |
| download | seaweedfs-aabea37af1835af7ec53fb9b6a56295f2a591785.tar.xz seaweedfs-aabea37af1835af7ec53fb9b6a56295f2a591785.zip | |
Merge pull request #76 from chrislusf/master
sync
Diffstat (limited to 'other/java')
4 files changed, 73 insertions, 17 deletions
diff --git a/other/java/client/src/main/java/seaweedfs/client/FilerClient.java b/other/java/client/src/main/java/seaweedfs/client/FilerClient.java index 257a9873d..0a8356258 100644 --- a/other/java/client/src/main/java/seaweedfs/client/FilerClient.java +++ b/other/java/client/src/main/java/seaweedfs/client/FilerClient.java @@ -126,6 +126,18 @@ public class FilerClient extends FilerGrpcClient { } + public boolean exists(String path){ + File pathFile = new File(path); + String parent = pathFile.getParent(); + String entryName = pathFile.getName(); + if(parent == null) { + parent = path; + entryName =""; + } + return lookupEntry(parent, entryName) != null; + + } + public boolean rm(String path, boolean isRecursive, boolean ignoreRecusiveError) { File pathFile = new File(path); @@ -142,10 +154,12 @@ public class FilerClient extends FilerGrpcClient { public boolean touch(String path, int mode) { String currentUser = System.getProperty("user.name"); - return touch(path, mode, 0, 0, currentUser, new String[]{}); + + long now = System.currentTimeMillis() / 1000L; + return touch(path, now, mode, 0, 0, currentUser, new String[]{}); } - public boolean touch(String path, int mode, int uid, int gid, String userName, String[] groupNames) { + public boolean touch(String path, long modifiedTimeSecond, int mode, int uid, int gid, String userName, String[] groupNames) { File pathFile = new File(path); String parent = pathFile.getParent().replace('\\','/'); @@ -155,17 +169,25 @@ public class FilerClient extends FilerGrpcClient { if (entry == null) { return createEntry( parent, - newFileEntry(name, mode, uid, gid, userName, groupNames).build() + newFileEntry(name, modifiedTimeSecond, mode, uid, gid, userName, groupNames).build() ); } - long now = System.currentTimeMillis() / 1000L; - FilerProto.FuseAttributes.Builder attr = entry.getAttributes().toBuilder() - .setMtime(now) - .setUid(uid) - .setGid(gid) - .setUserName(userName) - .clearGroupName() - .addAllGroupName(Arrays.asList(groupNames)); + FilerProto.FuseAttributes.Builder attr = entry.getAttributes().toBuilder(); + if (modifiedTimeSecond>0) { + attr.setMtime(modifiedTimeSecond); + } + if (uid>0) { + attr.setUid(uid); + } + if (gid>0) { + attr.setGid(gid); + } + if (userName!=null) { + attr.setUserName(userName); + } + if (groupNames!=null) { + attr.clearGroupName().addAllGroupName(Arrays.asList(groupNames)); + } return updateEntry(parent, entry.toBuilder().setAttributes(attr).build()); } @@ -188,17 +210,15 @@ public class FilerClient extends FilerGrpcClient { .addAllGroupName(Arrays.asList(groupNames))); } - public FilerProto.Entry.Builder newFileEntry(String name, int mode, + public FilerProto.Entry.Builder newFileEntry(String name, long modifiedTimeSecond, int mode, int uid, int gid, String userName, String[] groupNames) { - long now = System.currentTimeMillis() / 1000L; - return FilerProto.Entry.newBuilder() .setName(name) .setIsDirectory(false) .setAttributes(FilerProto.FuseAttributes.newBuilder() - .setMtime(now) - .setCrtime(now) + .setMtime(modifiedTimeSecond) + .setCrtime(modifiedTimeSecond) .setUid(uid) .setGid(gid) .setFileMode(mode) diff --git a/other/java/client/src/main/java/seaweedfs/client/SeaweedInputStream.java b/other/java/client/src/main/java/seaweedfs/client/SeaweedInputStream.java index 4e40ce1b6..6097b8d56 100644 --- a/other/java/client/src/main/java/seaweedfs/client/SeaweedInputStream.java +++ b/other/java/client/src/main/java/seaweedfs/client/SeaweedInputStream.java @@ -6,6 +6,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.EOFException; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; @@ -34,6 +35,10 @@ public class SeaweedInputStream extends InputStream { this.entry = filerClient.lookupEntry( SeaweedOutputStream.getParentDirectory(fullpath), SeaweedOutputStream.getFileName(fullpath)); + if(entry == null){ + throw new FileNotFoundException(); + } + this.contentLength = SeaweedRead.fileSize(entry); this.visibleIntervalList = SeaweedRead.nonOverlappingVisibleIntervals(filerClient, entry.getChunksList()); diff --git a/other/java/client/src/test/java/seaweedfs/client/SeaweedFilerTest.java b/other/java/client/src/test/java/seaweedfs/client/SeaweedFilerTest.java index eaf17e5c6..f9a2c3f76 100644 --- a/other/java/client/src/test/java/seaweedfs/client/SeaweedFilerTest.java +++ b/other/java/client/src/test/java/seaweedfs/client/SeaweedFilerTest.java @@ -16,8 +16,17 @@ public class SeaweedFilerTest { filerClient.mkdirs("/new_folder", 0755); filerClient.touch("/new_folder/new_empty_file", 0755); filerClient.touch("/new_folder/new_empty_file2", 0755); + if(!filerClient.exists("/new_folder/new_empty_file")){ + System.out.println("/new_folder/new_empty_file should exists"); + } + filerClient.rm("/new_folder/new_empty_file", false, true); filerClient.rm("/new_folder", true, true); - + if(filerClient.exists("/new_folder/new_empty_file")){ + System.out.println("/new_folder/new_empty_file should not exists"); + } + if(!filerClient.exists("/")){ + System.out.println("/ should exists"); + } } } diff --git a/other/java/examples/src/main/java/com/seaweedfs/examples/ExampleWriteFile2.java b/other/java/examples/src/main/java/com/seaweedfs/examples/ExampleWriteFile2.java new file mode 100644 index 000000000..61d8c290f --- /dev/null +++ b/other/java/examples/src/main/java/com/seaweedfs/examples/ExampleWriteFile2.java @@ -0,0 +1,22 @@ +package com.seaweedfs.examples; + +import com.google.common.io.Files; +import seaweedfs.client.FilerClient; +import seaweedfs.client.SeaweedOutputStream; + +import java.io.File; +import java.io.IOException; + +public class ExampleWriteFile2 { + + public static void main(String[] args) throws IOException { + + FilerClient filerClient = new FilerClient("localhost", 18888); + + SeaweedOutputStream seaweedOutputStream = new SeaweedOutputStream(filerClient, "/test/1"); + Files.copy(new File("/etc/resolv.conf"), seaweedOutputStream); + seaweedOutputStream.close(); + + } + +} |
