public CatalogIOManager get(String io) throws CatalogIOException { if (io == null) { io = defaultCatalogScheme; } if (!catalogIOManagers.containsKey(io)) { switch (io) { case "file": catalogIOManagers.put("file", new PosixCatalogIOManager(configuration)); break; // case "hdfs": // catalogIOManagers.put("hdfs", new HdfsCatalogIOManager(configuration)); // break; default: throw new UnsupportedOperationException("Unsupported file system : " + io); } } return catalogIOManagers.get(io); }
@Override public void rename(URI oldName, URI newName) throws CatalogIOException { String parent; if (isDirectory(oldName)) { // if oldName is a file parent = ".."; } else { parent = "."; } checkUriExists(oldName); checkDirectoryUri(oldName.resolve(parent), true); try { if (!Files.exists(Paths.get(newName))) { Files.move(Paths.get(oldName), Paths.get(newName)); } else { throw new CatalogIOException("Unable to rename. File \"" + newName + "\" already exists"); } } catch (IOException e) { throw new CatalogIOException("Unable to rename file", e); } }
@Override public long getFileSize(URI file) throws CatalogIOException { checkUriScheme(file); try { return Files.size(Paths.get(file)); } catch (IOException e) { e.printStackTrace(); throw new CatalogIOException("Can't get file size", e); } }
@Override public void copyFile(URI source, URI target) throws IOException, CatalogIOException { checkUriExists(source); if ("file".equals(source.getScheme()) && "file".equals(target.getScheme())) { Files.copy(Paths.get(source), Paths.get(target), StandardCopyOption.REPLACE_EXISTING); } else { throw new CatalogIOException("Expected posix file system URIs."); } }
/** * ********** */ // public String getDataPath(String wsDataId){ // wsDataId.replaceAll(":", "/") // } // public String getJobPath(String accountId, String bucketId, String jobId) // { // return getUserUri(accountId) + "/jobs/" + jobId; // } private Path renameExistingFileIfNeeded(Path fullFilePath) { if (Files.exists(fullFilePath)) { String file = fullFilePath.getFileName().toString(); Path parent = fullFilePath.getParent(); String fileName = IOUtils.removeExtension(file); String fileExt = IOUtils.getExtension(file); String newname = null; if (fileName != null && fileExt != null) { newname = fileName + "-copy" + fileExt; } else { newname = file + "-copy"; } return renameExistingFileIfNeeded(parent.resolve(newname)); } else { return fullFilePath; } }
@Override public void moveFile(URI source, URI target) throws IOException, CatalogIOException { checkUriExists(source); if (source.getScheme().equals("file") && target.getScheme().equals("file")) { Files.move(Paths.get(source), Paths.get(target), StandardCopyOption.REPLACE_EXISTING); } else { throw new CatalogIOException("Can't move from " + source.getScheme() + " to " + target.getScheme()); } }
@BeforeClass public static void setUp() throws Exception { System.out.println("Testing PosixIOManagerTest"); Path path = Paths.get("/tmp").resolve("opencga"); try { if (Files.exists(path)) { IOUtils.deleteDirectory(path); } Files.createDirectory(path); Properties properties = new Properties(); properties.setProperty("CATALOG.FILE.ROOTDIR", path.toUri().toString()); posixCatalogIOManager = new PosixCatalogIOManager(properties); } catch (Exception e) { e.printStackTrace(); } }
@Override public Date getCreationDate(URI file) throws CatalogIOException { checkUriScheme(file); try { return Date.from(Files.readAttributes(Paths.get(file), BasicFileAttributes.class).creationTime().toInstant()); } catch (IOException e) { e.printStackTrace(); throw new CatalogIOException("Can't get file size", e); } }
@Override public List<URI> listFiles(URI directory) throws CatalogIOException { checkUriExists(directory); class ListFiles extends SimpleFileVisitor<Path> { private List<String> filePaths = new LinkedList<>(); @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { filePaths.add(file.toAbsolutePath().toString()); return super.visitFile(file, attrs); } public List<String> getFilePaths() { return filePaths; } } ListFiles fileVisitor = new ListFiles(); try { Files.walkFileTree(Paths.get(directory.getPath()), fileVisitor); } catch (IOException e) { throw new CatalogIOException("Unable to walkFileTree", e); } List<URI> fileUris = new LinkedList<>(); for (String filePath : fileVisitor.getFilePaths()) { try { fileUris.add(new URI("file", filePath, null)); } catch (URISyntaxException e) { throw CatalogIOException.uriSyntaxException(filePath, e); } } return fileUris; }
@Override public Date getModificationDate(URI file) throws CatalogIOException { checkUriScheme(file); try { return Date.from(Files.readAttributes(Paths.get(file), BasicFileAttributes.class).lastModifiedTime().toInstant()); } catch (IOException e) { e.printStackTrace(); throw new CatalogIOException("Can't get file size", e); } }