Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of ssh://github.com/UNC-Libraries/Curators-Work…
Browse files Browse the repository at this point in the history
…bench
  • Loading branch information
daines committed Apr 16, 2013
2 parents 5ef90d2 + ab4b0d0 commit c6d8d97
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 84 deletions.
4 changes: 2 additions & 2 deletions workbench_plugin/src/main/java/log4j.properties
Expand Up @@ -15,7 +15,7 @@
#

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=ERROR, A1
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
Expand All @@ -24,5 +24,5 @@ log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

log4j.logger.unc.lib=WARN
log4j.logger.unc.lib=DEBUG
log4j.logger.org.irods.jargon.core=WARN
Expand Up @@ -124,6 +124,14 @@ public static void setFileID(IResource r, String id) {
throw new Error(e);
}
}

public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + "B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
return String.format("%d%sB", (int)(bytes / Math.pow(unit, exp)), pre);
}

public static String getFileID(IResource r) {
String result = null;
Expand Down
Expand Up @@ -49,15 +49,17 @@ public class OriginalsLinkJob extends Job {
URI baselocation = null;
List<URI> locations = null;
String name = null;
boolean removeable = false;
IProject project = null;
private boolean prestaged;
private URI prestagedBase;

OriginalsLinkJob(URI baselocation, List<URI> locations, IProject project, boolean prestaged, URI prestagedBase) {
OriginalsLinkJob(URI baselocation, List<URI> locations, IProject project, boolean prestaged, URI prestagedBase, boolean removeable) {
super("creating link to " + locations.size() + " location(s)");
this.baselocation = baselocation;
this.locations = locations;
this.project = project;
this.removeable = removeable;
this.prestaged = prestaged;
this.prestagedBase = prestagedBase;
}
Expand Down Expand Up @@ -114,16 +116,16 @@ protected IStatus run(IProgressMonitor monitor) {
}
}
for(URI volumeRoot : volumes) {
OriginalStub existingStub = null;
boolean foundStub = false;
for(OriginalStub s :n.getOriginals()) {
if(s.getVolumeRoot().equals(volumeRoot)) {
existingStub = s;
if(s.getVolumeRoot().equals(volumeRoot) && s.isAttached()) {
s.addLocations(volumeToLocations.get(volumeRoot), volumeToPrestageLocations.get(volumeRoot));
foundStub = true;
break;
}
}
if(existingStub != null && existingStub.isAttached()) {
existingStub.addLocations(volumeToLocations.get(volumeRoot), volumeToPrestageLocations.get(volumeRoot));
} else {
OriginalStub original = new OriginalStub(volumeRoot, volumeToLocations.get(volumeRoot), volumeToPrestageLocations.get(volumeRoot), this.project);
if(!foundStub) {
OriginalStub original = new OriginalStub(volumeRoot, volumeToLocations.get(volumeRoot), volumeToPrestageLocations.get(volumeRoot), this.project, this.removeable);
n.addOriginal(original);
}
}
Expand Down
Expand Up @@ -66,6 +66,8 @@
import org.eclipse.swt.widgets.Widget;
import org.osgi.service.prefs.BackingStoreException;

import unc.lib.cdr.workbench.originals.OriginalFileStore;
import unc.lib.cdr.workbench.originals.OriginalStub;
import unc.lib.cdr.workbench.rcp.Activator;

/**
Expand All @@ -91,6 +93,7 @@ public class PickOriginalLocationsPage extends WizardPage implements Listener {
private Combo locationField;
private Button drivesBrowseButton;
private Button irodsBrowseButton;
private Button removeableButton;
private Button preStagedButton;
private Combo preStagedCombo;
private Label preStageSuffixLabel;
Expand Down Expand Up @@ -192,7 +195,7 @@ public void createControl(Composite parent) {

createRootDirectoryGroup(composite);
createFileSelectionGroup(composite);
// createButtonsGroup(composite);
createRemoveableGroup(composite);

createDestinationGroup(composite);

Expand All @@ -207,6 +210,31 @@ public void createControl(Composite parent) {
setControl(composite);
}

private void createRemoveableGroup(Composite composite) {
Group optionsGroup = new Group(composite, SWT.NONE);
//optionsGroup.setText("Pre-Staged Location");
GridLayout layout = new GridLayout();
layout.numColumns = 2;
optionsGroup.setLayout(layout);
optionsGroup.setFont(composite.getFont());
optionsGroup.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));

Label lab = new Label(optionsGroup, SWT.WRAP);
GridData labdata = new GridData();
// labdata.verticalSpan = 2;
labdata.horizontalAlignment = GridData.FILL;
labdata.horizontalSpan = 1;
lab.setLayoutData(labdata);
lab.setText("Removeable media, such as optical discs and flash drives, are not always reachable.");

this.removeableButton = new Button(optionsGroup, SWT.CHECK);
GridData data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
data.horizontalSpan = 3;
this.removeableButton.setLayoutData(data);
this.removeableButton.setText("This is removeable media.");

}

/**
* @param parent
*/
Expand Down Expand Up @@ -263,7 +291,6 @@ public void checkStateChanged(CheckStateChangedEvent event) {
}
}
}
updateWidgets();
}
});
}
Expand Down Expand Up @@ -587,11 +614,19 @@ private void initialPopulateProjectField() {
}
projectCombo.setItems(openProjects.toArray(new String[] {}));

IProject p = null;

if (IProject.class.isInstance(selection.getFirstElement())) {
IProject p = (IProject) selection.getFirstElement();
if (p.isOpen()) {
projectCombo.select(openProjects.indexOf(p.getName()));
}
p = (IProject) selection.getFirstElement();
} else if(OriginalStub.class.isInstance(selection.getFirstElement())) {
OriginalStub stub = (OriginalStub)selection.getFirstElement();
p = (IProject)stub.getProject();
} else if(OriginalFileStore.class.isInstance(selection.getFirstElement())) {
OriginalFileStore store = (OriginalFileStore)selection.getFirstElement();
p = store.getProject();
}
if (p != null && p.isOpen()) {
projectCombo.select(openProjects.indexOf(p.getName()));
} else {
projectCombo.select(0);
}
Expand Down Expand Up @@ -669,6 +704,7 @@ private void updateFromLocationField() {
*/
public boolean finish() {
List<URI> selected = getCheckedLocations();
boolean removeable = this.removeableButton.getSelection();
URI prestageBase = null;
try {
if (this.preStagedButton.getSelection()) {
Expand All @@ -682,7 +718,7 @@ public boolean finish() {
return false;
}
Job linkJob = new OriginalsLinkJob(this.location, selected, getProject(), this.preStagedButton.getSelection(),
prestageBase);
prestageBase, removeable);
// linkJob.addJobChangeListener(this);
linkJob.schedule();
// SNAPSHOT MAY NOT BE NEEDED ANY MORE and takes time
Expand Down
Expand Up @@ -49,7 +49,8 @@ public URI getVolumeRoot() {
private Map<URI, URI> prestageLocations = new HashMap<URI, URI>();
private transient IProject project;
private String projectName;
private int volumeHash;
private int volumeHash = -1;
private boolean removeable = false;
private String volumeType;
private String name;

Expand All @@ -59,14 +60,17 @@ public String getVolumeType() {

private transient List<OriginalFileStore> stores = new ArrayList<OriginalFileStore>();

public OriginalStub(URI volumeRoot, List<URI> locations, Map<URI, URI> prestageLocations, IProject project) {
public OriginalStub(URI volumeRoot, List<URI> locations,
Map<URI, URI> prestageLocations, IProject project,
boolean removeable) {
this.volumeRoot = volumeRoot;
this.locations = locations;
this.project = project;
this.projectName = project.getName();
if (prestageLocations != null)
this.prestageLocations = prestageLocations;
init();
this.removeable = removeable;
try {
this.volumeHash = VolumeUtil.makeVolumeFingerprint(volumeRoot);
this.volumeType = VolumeUtil.getFileStore(volumeRoot).type();
Expand Down Expand Up @@ -103,10 +107,12 @@ private void init() {
this.stores = new ArrayList<OriginalFileStore>();
if (isAttached()) {
for (URI base : locations) {
this.stores.add((OriginalFileStore) OriginalsFileSystem.wrapStore(base, this));
this.stores.add((OriginalFileStore) OriginalsFileSystem
.wrapStore(base, this));
}
}
this.project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
this.project = ResourcesPlugin.getWorkspace().getRoot()
.getProject(projectName);
} catch (CoreException e) {
throw new Error(e);
}
Expand Down Expand Up @@ -137,14 +143,16 @@ public List<URI> getLocations() {
return locations;
}

public void addLocations(Collection<URI> newLocations, Map<URI, URI> prestageLocations) {
public void addLocations(Collection<URI> newLocations,
Map<URI, URI> prestageLocations) {
if (this.isAttached()) {
this.locations.addAll(newLocations);
if (prestageLocations != null)
this.prestageLocations.putAll(prestageLocations);
init();
} else {
throw new Error("cannot add locations to an original stub when it is not attached.");
throw new Error(
"cannot add locations to an original stub when it is not attached.");
}
}

Expand All @@ -154,16 +162,22 @@ public int getVolumeHash() {

public OriginalFileStore getVolumeRootStore() {
try {
return (OriginalFileStore) OriginalsFileSystem.wrapStore(this.volumeRoot, this);
return (OriginalFileStore) OriginalsFileSystem.wrapStore(
this.volumeRoot, this);
} catch (CoreException e) {
throw new Error(e);
}
}

public boolean isAttached() {
if(!this.removeable) {
return this.getVolumeRootStore().fetchInfo().exists();
}
try {
int currentHash = VolumeUtil.makeVolumeFingerprint(this.getVolumeRoot());
// System.err.println("hashes " + currentHash + " " + this.getVolumeHash());
int currentHash = VolumeUtil.makeVolumeFingerprint(this
.getVolumeRoot());
// System.err.println("hashes " + currentHash + " " +
// this.getVolumeHash());
return (currentHash == this.getVolumeHash());
} catch (NoSuchFileException e) {
return false;
Expand Down
Expand Up @@ -38,11 +38,12 @@
import unc.lib.cdr.workbench.rcp.Activator;

public class VolumeUtil {

@SuppressWarnings("unused")
private static final Logger LOG = LoggerFactory.getLogger(VolumeUtil.class);

public static final QualifiedName VOLUME_FINGERPRINT = new QualifiedName(Activator.PLUGIN_ID, "volume-fingerprint");

public static final QualifiedName VOLUME_FINGERPRINT = new QualifiedName(
Activator.PLUGIN_ID, "volume-fingerprint");
public static Set<String> removableFileStoreTypes = new HashSet<String>();

static {
Expand All @@ -56,33 +57,40 @@ public static boolean isVolumeRemovable(URI uri) throws IOException {
}

/**
* Computes a fingerprint for the volume (FileStore) containing this resource based on the names and dates in the
* root directory. The fingerprint will include: volume root creation time (if available) volume root name volume
* root file key (if available) volume root's oldest constituent file's timestamp
* Computes a fingerprint for the volume (FileStore) containing this
* resource based on the names and dates in the root directory. The
* fingerprint will include: volume root creation time (if available) volume
* root name volume root file key (if available) volume root's oldest
* constituent file's timestamp
*
* @param resource
* a resource
* a resource
*/
public static int makeVolumeFingerprint(URI uri) throws IOException {
FileStore fstore = getFileStore(uri);
Path path = Paths.get(uri);
Path volumeRoot = findTopResourceInVolume(fstore, path);
File fr = volumeRoot.toFile();
String s1 = FileSystemView.getFileSystemView().getSystemDisplayName(fr);
String s2 = FileSystemView.getFileSystemView().getSystemTypeDescription(fr);
LOG.debug("getSystemDisplayName : " + s1);
LOG.debug("getSystemTypeDescription : " + s2);

if("/".equals(volumeRoot.toFile().getPath())) {
String s2 = FileSystemView.getFileSystemView()
.getSystemTypeDescription(fr);
LOG.debug("getSystemDisplayName : " + s1);
LOG.debug("getSystemTypeDescription : " + s2);

if ("/".equals(volumeRoot.toFile().getPath())) {
return -1; // fake hash key for root linux filesystem
}
LOG.debug("Found volume root: " + volumeRoot);
long oldestFileCreation = -1;
File[] files = volumeRoot.toFile().listFiles();
if (files != null) {
for (File f : files) {
BasicFileAttributeView v = FileSystems.getDefault().provider()
.getFileAttributeView(f.toPath(), BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
BasicFileAttributeView v = FileSystems
.getDefault()
.provider()
.getFileAttributeView(f.toPath(),
BasicFileAttributeView.class,
LinkOption.NOFOLLOW_LINKS);
BasicFileAttributes basic = v.readAttributes();
long test = basic.creationTime().toMillis();
if (test > oldestFileCreation) {
Expand All @@ -92,39 +100,32 @@ public static int makeVolumeFingerprint(URI uri) throws IOException {
}
String name = volumeRoot.toString();
LOG.debug("Found volume root name: " + name);
if (fstore.supportsFileAttributeView("basic")) {
BasicFileAttributeView v = FileSystems.getDefault().provider()
.getFileAttributeView(volumeRoot, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
BasicFileAttributes basic = v.readAttributes();
LOG.debug("Found volume root file key: " + basic.fileKey());
LOG.debug("Found volume root create time: " + basic.creationTime());
LOG.debug("Oldest file create time: " + basic.creationTime());
int result = name.hashCode() ^ (int) basic.creationTime().toMillis()
^ (int) oldestFileCreation;
return result;
} else {
return name.hashCode() ^ (int) oldestFileCreation;
}
return name.hashCode() ^ (int) oldestFileCreation;
}

public static URI getTopResourceInVolume(URI location) throws IOException {
Path path = Paths.get(location);
FileStore fstore = getFileStore(location);
Path p = findTopResourceInVolume(fstore, path);
try {
URI result = new URI(location.getScheme(), location.getUserInfo(), location.getHost(), location.getPort(), p.toUri().getPath(), location.getQuery(), location.getFragment());
URI result = new URI(location.getScheme(), location.getUserInfo(),
location.getHost(), location.getPort(),
p.toUri().getPath(), location.getQuery(),
location.getFragment());
return result;
} catch(URISyntaxException e) {
} catch (URISyntaxException e) {
throw new Error(e);
}
}

private static Path findTopResourceInVolume(FileStore fstore, Path path) throws IOException {
private static Path findTopResourceInVolume(FileStore fstore, Path path)
throws IOException {
Path parent = path.getParent();
if (parent == null) {
return path; // root will do for default file system
} else {
FileStore parentStore = FileSystems.getDefault().provider().getFileStore(parent);
FileStore parentStore = FileSystems.getDefault().provider()
.getFileStore(parent);
if (fstore.equals(parentStore)) {
return findTopResourceInVolume(fstore, parent);
} else {
Expand Down

0 comments on commit c6d8d97

Please sign in to comment.