Skip to content

Commit

Permalink
Fix #4043. Get MD5 sum of files in S3 and CloudFiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Jan 16, 2010
1 parent eb6979c commit 7bba9eb
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 11 deletions.
5 changes: 5 additions & 0 deletions source/ch/cyberduck/core/AbstractPath.java
Expand Up @@ -440,4 +440,9 @@ public boolean isWritable() {
|| perm.getGroupPermissions()[Permission.WRITE]
|| perm.getOtherPermissions()[Permission.WRITE];
}

/**
* Calculate the MD5 sum as Hex-encoded string or null if failure
*/
public abstract void readChecksum();
}
4 changes: 4 additions & 0 deletions source/ch/cyberduck/core/Attributes.java
Expand Up @@ -96,4 +96,8 @@ public interface Attributes {
public abstract String getOwner();

public abstract String getGroup();

public abstract String getChecksum();

public abstract void setChecksum(String md5);
}
34 changes: 33 additions & 1 deletion source/ch/cyberduck/core/Local.java
Expand Up @@ -24,9 +24,11 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jets3t.service.utils.ServiceUtils;

import java.io.*;
import java.net.MalformedURLException;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -100,10 +102,18 @@ public void setGroup(String group) {
;
}

/**
*
* @return Always null
*/
public String getOwner() {
return null;
}

/**
*
* @return Always null
*/
public String getGroup() {
return null;
}
Expand Down Expand Up @@ -435,8 +445,30 @@ public String toURL() {
}
}

private String checksum;

@Override
public void readChecksum() {
try {
ServiceUtils.toHex(ServiceUtils.computeMD5Hash(new InputStream(this)));
}
catch(NoSuchAlgorithmException e) {
log.error("MD5 failed:" + e.getMessage());
}
catch(IOException e) {
log.error("MD5 failed:" + e.getMessage());
}
}

public String getChecksum() {
return checksum;
}

public void setChecksum(String checksum) {
this.checksum = checksum;
}

/**
*
* @return True if application was found to open the file with
*/
public abstract boolean open();
Expand Down
5 changes: 5 additions & 0 deletions source/ch/cyberduck/core/Path.java
Expand Up @@ -295,6 +295,11 @@ public void writeGroup(String group, boolean recursive) {
throw new UnsupportedOperationException();
}

@Override
public void readChecksum() {
;
}

/**
* Read the size of the file
*
Expand Down
9 changes: 9 additions & 0 deletions source/ch/cyberduck/core/PathAttributes.java
Expand Up @@ -52,6 +52,7 @@ public class PathAttributes implements Attributes, Serializable {
private int type = Path.FILE_TYPE;

protected Permission permission = null;
private String checksum;

public PathAttributes() {
super();
Expand Down Expand Up @@ -200,4 +201,12 @@ public String getGroup() {
}
return this.group;
}

public String getChecksum() {
return checksum;
}

public void setChecksum(String checksum) {
this.checksum = checksum;
}
}
14 changes: 4 additions & 10 deletions source/ch/cyberduck/core/cf/CFPath.java
Expand Up @@ -37,7 +37,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException;
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.Date;
Expand Down Expand Up @@ -234,6 +233,7 @@ public AttributedList<Path> list() {
file.setParent(this);
if(file.attributes.getType() == Path.FILE_TYPE) {
file.attributes.setSize(object.getSize());
file.attributes.setChecksum(object.getMd5sum());
}
try {
final Date modified = DateParser.parse(object.getLastModified());
Expand Down Expand Up @@ -311,15 +311,9 @@ protected void upload(final BandwidthThrottle throttle, final StreamListener lis
final InputStream in = new Local.InputStream(this.getLocal());
this.getSession().message(MessageFormat.format(Locale.localizedString("Compute MD5 hash of {0}", "Status"),
this.getName()));
String md5sum = null;
try {
md5sum = ServiceUtils.toHex(ServiceUtils.computeMD5Hash(new Local.InputStream(this.getLocal())));
this.getSession().message(MessageFormat.format(Locale.localizedString("Uploading {0}", "Status"),
this.getName()));
}
catch(NoSuchAlgorithmException e) {
log.error(e.getMessage(), e);
}
String md5sum = this.getLocal().getChecksum();
this.getSession().message(MessageFormat.format(Locale.localizedString("Uploading {0}", "Status"),
this.getName()));

final HashMap<String, String> metadata = new HashMap<String, String>();

Expand Down
26 changes: 26 additions & 0 deletions source/ch/cyberduck/core/s3/S3Path.java
Expand Up @@ -395,6 +395,32 @@ public boolean isRequesterPays() {
return false;
}

@Override
public void readChecksum() {
if(attributes.isFile()) {
try {
this.getSession().check();
this.getSession().message(MessageFormat.format(Locale.localizedString("Compute MD5 hash of {0}", "Status"),
this.getName()));

final S3Object details = this.getDetails();
if(StringUtils.isNotEmpty(details.getMd5HashAsHex())) {
attributes.setChecksum(details.getMd5HashAsHex());
}
else {
log.debug("Setting ETag Header as checksum for:" + this.toString());
attributes.setChecksum(details.getETag());
}
}
catch(S3ServiceException e) {
this.error("Cannot read file attributes", e);
}
catch(IOException e) {
this.error("Cannot read file attributes", e);
}
}
}

@Override
public void readSize() {
if(attributes.isFile()) {
Expand Down

0 comments on commit 7bba9eb

Please sign in to comment.