Skip to content

Commit

Permalink
issue #56 implemented
Browse files Browse the repository at this point in the history
Added initial simple implementation and not completed.
  • Loading branch information
jarnaiz committed Aug 9, 2016
1 parent 42d26cd commit 2b642bf
Show file tree
Hide file tree
Showing 19 changed files with 865 additions and 393 deletions.
40 changes: 31 additions & 9 deletions src/main/java/com/upplication/s3fs/S3FileSystemProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.spi.FileSystemProvider;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -41,6 +42,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.upplication.s3fs.attribute.S3BasicFileAttributes;
import com.upplication.s3fs.attribute.S3PosixFileAttributes;
import com.upplication.s3fs.util.AttributesUtils;
import com.upplication.s3fs.util.Cache;
import com.upplication.s3fs.util.S3Utils;
Expand Down Expand Up @@ -233,7 +236,7 @@ protected boolean overloadPropertiesWithSystemProps(Properties props, String key
/**
* @return true if the key are overloaded by a system property
*/
protected boolean overloadPropertiesWithSystemEnv(Properties props, String key) {
public boolean overloadPropertiesWithSystemEnv(Properties props, String key) {
if (systemGetEnv(key) != null) {
props.setProperty(key, systemGetEnv(key));
return true;
Expand Down Expand Up @@ -463,13 +466,24 @@ public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type
s3Path.setFileAttributes(null);
return result;
} else {
S3FileAttributes attrs = s3Utils.getS3FileAttributes(s3Path);
S3BasicFileAttributes attrs = s3Utils.getS3FileAttributes(s3Path);
s3Path.setFileAttributes(attrs);
return type.cast(attrs);
}
} else if (type == PosixFileAttributes.class) {
if (s3Path.getFileAttributes() instanceof PosixFileAttributes &&
cache.isInTime(s3Path.getFileSystem().getCache(), s3Path.getFileAttributes())) {
A result = type.cast(s3Path.getFileAttributes());
s3Path.setFileAttributes(null);
return result;
}

S3PosixFileAttributes attrs = s3Utils.getS3PosixFileAttributes(s3Path);
s3Path.setFileAttributes(attrs);
return type.cast(attrs);
}

throw new UnsupportedOperationException(format("only %s supported", BasicFileAttributes.class));
throw new UnsupportedOperationException(format("only %s or %s supported", BasicFileAttributes.class, PosixFileAttributes.class));
}

@Override
Expand All @@ -478,20 +492,28 @@ public Map<String, Object> readAttributes(Path path, String attributes, LinkOpti
throw new IllegalArgumentException("Attributes null");
}

if (attributes.contains(":") && !attributes.contains("basic:")) {
throw new UnsupportedOperationException(format("attributes %s are not supported, only basic are supported", attributes));
if (attributes.contains(":") && !attributes.contains("basic:") && !attributes.contains("posix:")) {
throw new UnsupportedOperationException(format("attributes %s are not supported, only basic / posix are supported", attributes));
}

if (attributes.equals("*") || attributes.equals("basic:*")) {
BasicFileAttributes attr = readAttributes(path, BasicFileAttributes.class, options);
return AttributesUtils.fileAttributeToMap(attr);
} else {
}
else if (attributes.equals("posix:*")) {
PosixFileAttributes attr = readAttributes(path, PosixFileAttributes.class, options);
return AttributesUtils.fileAttributeToMap(attr);
}
else {
String[] filters = new String[]{attributes};
if (attributes.contains(",")){
filters = attributes.split(",");
}
BasicFileAttributes attr = readAttributes(path, BasicFileAttributes.class, options);
return AttributesUtils.fileAttributeToMap(attr, filters);
Class<? extends BasicFileAttributes> filter = BasicFileAttributes.class;
if (attributes.startsWith("posix:")) {
filter = PosixFileAttributes.class;
}
return AttributesUtils.fileAttributeToMap(readAttributes(path, filter, options), filters);
}
}

Expand Down Expand Up @@ -534,7 +556,7 @@ protected AmazonS3Factory getAmazonS3Factory(Properties props) {
*
* @return Properties amazon.properties
*/
protected Properties loadAmazonProperties() {
public Properties loadAmazonProperties() {
Properties props = new Properties();
// http://www.javaworld.com/javaworld/javaqa/2003-06/01-qa-0606-load.html
// http://www.javaworld.com/javaqa/2003-08/01-qa-0808-property.html
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/upplication/s3fs/S3Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.upplication.s3fs.attribute.S3BasicFileAttributes;

public class S3Path implements Path {

Expand All @@ -44,9 +45,9 @@ public class S3Path implements Path {
private S3FileSystem fileSystem;

/**
* S3FileAttributes cache
* S3BasicFileAttributes cache
*/
private S3FileAttributes fileAttributes;
private S3BasicFileAttributes fileAttributes;

/**
* path must be a string of the form "/{bucket}", "/{bucket}/{key}" or just
Expand All @@ -68,7 +69,7 @@ public S3Path(S3FileSystem fileSystem, String path) {
/**
* Build an S3Path from path segments. '/' are stripped from each segment.
*
* @param fileSystem
* @param fileSystem S3FileSystem
* @param first should be start with a '/' and is the bucket name
* @param more directories and files
*/
Expand Down Expand Up @@ -426,11 +427,11 @@ public int hashCode() {
return result;
}

public S3FileAttributes getFileAttributes() {
public S3BasicFileAttributes getFileAttributes() {
return fileAttributes;
}

public void setFileAttributes(S3FileAttributes fileAttributes) {
public void setFileAttributes(S3BasicFileAttributes fileAttributes) {
this.fileAttributes = fileAttributes;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.upplication.s3fs;
package com.upplication.s3fs.attribute;

import static java.lang.String.format;

import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;

public class S3FileAttributes implements BasicFileAttributes {
public class S3BasicFileAttributes implements BasicFileAttributes {
private final FileTime lastModifiedTime;
private final long size;
private final boolean directory;
private final boolean regularFile;
private final String key;
private long cacheCreated;

public S3FileAttributes(String key, FileTime lastModifiedTime, long size, boolean isDirectory, boolean isRegularFile) {
public S3BasicFileAttributes(String key, FileTime lastModifiedTime, long size, boolean isDirectory, boolean isRegularFile) {
this.key = key;
this.lastModifiedTime = lastModifiedTime;
this.size = size;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/upplication/s3fs/attribute/S3GroupPrincipal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.upplication.s3fs.attribute;

import java.nio.file.attribute.GroupPrincipal;

public class S3GroupPrincipal implements GroupPrincipal {
@Override
public String getName() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.upplication.s3fs.attribute;

import java.nio.file.attribute.*;
import java.util.Set;

import static java.lang.String.format;

public class S3PosixFileAttributes extends S3BasicFileAttributes implements PosixFileAttributes {

private UserPrincipal userPrincipal;
private GroupPrincipal groupPrincipal;
private Set<PosixFilePermission> posixFilePermissions;

public S3PosixFileAttributes(String key, FileTime lastModifiedTime, long size, boolean isDirectory, boolean isRegularFile, UserPrincipal userPrincipal, GroupPrincipal groupPrincipal, Set<PosixFilePermission> posixFilePermissionSet) {

super(key, lastModifiedTime, size, isDirectory, isRegularFile);

this.userPrincipal = userPrincipal;
this.groupPrincipal = groupPrincipal;
this.posixFilePermissions = posixFilePermissionSet;
}

@Override
public UserPrincipal owner() {
return this.userPrincipal;
}

@Override
public GroupPrincipal group() {
return this.groupPrincipal;
}

@Override
public Set<PosixFilePermission> permissions() {
return this.posixFilePermissions;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/upplication/s3fs/attribute/S3UserPrincipal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.upplication.s3fs.attribute;

import java.nio.file.attribute.UserPrincipal;

public class S3UserPrincipal implements UserPrincipal {

private String name;

public S3UserPrincipal(String name) {
this.name = name;
}

@Override
public String getName() {
return name;
}
}
23 changes: 21 additions & 2 deletions src/main/java/com/upplication/s3fs/util/AttributesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFileAttributes;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -12,7 +13,7 @@ public abstract class AttributesUtils {

/**
* Given a BasicFileAttributes not null then return a Map
* with the keys as the fields of the BasicFileAttributes and the values
* with the keys as the fields of the BasicFileAttributes or PosixFileAttributes and the values
* with the content of the fields
*
* @param attr BasicFileAttributes
Expand All @@ -29,11 +30,19 @@ public static Map<String, Object> fileAttributeToMap(BasicFileAttributes attr) {
result.put("lastAccessTime", attr.lastAccessTime());
result.put("lastModifiedTime", attr.lastModifiedTime());
result.put("size", attr.size());

if (attr instanceof PosixFileAttributes) {
PosixFileAttributes posixAttr = (PosixFileAttributes) attr;
result.put("permissions", posixAttr.permissions());
result.put("owner", posixAttr.owner());
result.put("group", posixAttr.group());
}

return result;
}

/**
* transform the BasicFileAttributes to Map filtering by the keys
* transform the java.nio.file.attribute.BasicFileAttributes to Map filtering by the keys
* given in the filters param
*
* @param attr BasicFileAttributes not null to tranform to map
Expand All @@ -45,6 +54,7 @@ public static Map<String, Object> fileAttributeToMap(BasicFileAttributes attr, S

for (String filter : filters) {
filter = filter.replace("basic:", "");
filter = filter.replace("posix:", "");
switch (filter) {
case "creationTime":
result.put("creationTime", attr.creationTime());
Expand Down Expand Up @@ -73,6 +83,15 @@ public static Map<String, Object> fileAttributeToMap(BasicFileAttributes attr, S
case "size":
result.put("size", attr.size());
break;
case "permissions":
result.put("permissions", ((PosixFileAttributes)attr).permissions());
break;
case "group":
result.put("group", ((PosixFileAttributes)attr).group());
break;
case "owner":
result.put("owner", ((PosixFileAttributes)attr).owner());
break;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/upplication/s3fs/util/Cache.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.upplication.s3fs.util;

import com.upplication.s3fs.S3FileAttributes;
import com.upplication.s3fs.attribute.S3BasicFileAttributes;

public class Cache {

Expand All @@ -11,7 +11,7 @@ public class Cache {
* @param fileAttributes S3FileAttributes to check if is still valid, can be null
* @return true or false, if cache are -1 and fileAttributes are not null then always return true
*/
public boolean isInTime(int cache, S3FileAttributes fileAttributes) {
public boolean isInTime(int cache, S3BasicFileAttributes fileAttributes) {
if (fileAttributes == null) {
return false;
}
Expand Down
Loading

0 comments on commit 2b642bf

Please sign in to comment.