Hi Josh,
Mostly as a draft, I implemented the concept of the copy, delete, and reverse symlink concept as a subclass of AbstractExecFileTransfer as:
- Code: Select all
/*
* Copyright (C) 2015 University of Dundee & Open Microscopy Environment.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package ome.formats.importer.transfers;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Local-only file transfer mechanism which makes use of the platform
* copy and ln commands.
*
* This is only useful where the commands "cp (or ln -s) source target" (Unix) or
* "copy (or mklink) source target" (Windows) will work.
*
* @since 5.3.x
*/
public class CopyReverseSymlinkFileTransfer extends AbstractExecFileTransfer {
/**
* Executes "cp file location" (Unix) or "cp file location" (Windows),
* then creates a temporary symlink in the source location,
* and fails on non-0 return codes.
*
* @param file File to be copied
* @param location Location to copy to.
* @throws IOException
*/
protected ProcessBuilder createProcessBuilder(File file, File location) {
ProcessBuilder pb = new ProcessBuilder();
List<String> args = new ArrayList<String>();
if (isWindows()) {
// no idea about this, just guessing this might work
args.add("cmd");
args.add("/c");
args.add("copy");
args.add(file.getAbsolutePath());
args.add(location.getAbsolutePath());
args.add("&&");
args.add("mklink");
args.add(String.format("%s.tmp", file.getAbsolutePath()));
args.add(location.getAbsolutePath());
} else {
args.add("cp");
args.add(file.getAbsolutePath());
args.add(location.getAbsolutePath());
args.add("&&");
args.add("ln");
args.add("-s");
args.add(location.getAbsolutePath());
args.add(String.format("%s.tmp", file.getAbsolutePath()));
}
pb.command(args);
return pb;
}
/**
* Delete all copied files if there were no errors and change the temporary symlink name
*/
@Override
public void afterTransfer(int errors, List<String> srcFiles) throws CleanupFailure {
deleteTransferredFiles(errors, srcFiles);
// rename the symlink
renameSymlinks(errors, srcFiles);
}
/**
* Method used by this subclass during {@link FileTransfer#afterTransfer(int, List)}
* if they would like to rename symlinks to the files transferred in the set.
*/
protected void renameSymlinks(int errors, List<String> srcFiles)
throws CleanupFailure {
if (errors > 0) {
printLine();
log.error("{} error(s) found.", errors);
log.error("{} symlink rename not performed!", getClass().getSimpleName());
log.error("The following <filename>.tmp symlinks will *not* be renamed:");
for (String srcFile : srcFiles) {
log.error("\t{}", srcFile);
}
printLine();
return;
}
List<File> failedFiles = new ArrayList<File>();
for (String path : srcFiles) {
File tmpSymlink = new File(String.format("%s.tmp", path));
File realSymlink = new File(path);
try {
log.info("Removing .tmp from symlink {}...", tmpSymlink);
if (!tmpSymlink.renameTo(realSymlink)) {
throw new RuntimeException("Failed to rename.");
}
} catch (Exception e) {
log.error("Failed to rename temporary symlink {}", tmpSymlink);
failedFiles.add(tmpSymlink);
}
}
if (!failedFiles.isEmpty()) {
printLine();
log.error("Renaming failed!");
log.error("{} files could not be renamed and will need to " +
"be handled manually", failedFiles.size());
for (File failedFile : failedFiles) {
log.error("\t{}", failedFile.getAbsolutePath());
}
printLine();
throw new CleanupFailure(failedFiles);
}
}
}
In order to mostly use existing functionality, the implementation is effectively doing a copy and creating a reverse symlink with a temporary name, and then relies on the existing afterTransfer delete followed by a rename of the symlinks. Maybe a bit clumsy but seemed most compatible with the existing code.
Agreed that there are a number of assumptions before this can work but they are pretty much the same as are needed for hardlinks with all modern Linuxes.
I haven't yet compiled/run this, mostly because I don't know much about Java and how to build jars. While I figure that out, does this approach look workable?
Cheers,
- Damir