/** * Given a file object, this creates a PLUSFile representation filled with appropriate metadata, * file owner, and so on. * * @param file * @return */ public static PLUSFile extractMetadata(File file, AbstractProvenanceClient client) { if (!file.exists() || !file.canRead()) return null; PLUSFile f = new PLUSFile(file); // Try to figure out who owns this file, if it exists. if (f.getOwner() == null) { // This is a java-7 ism that permits us to access the file owner. try { Path path = Paths.get(file.getAbsolutePath()); UserPrincipal owner = Files.getOwner(path); String username = owner.getName(); try { PLUSActor a = client.actorExistsByName(username); if (a == null) a = new PLUSActor(username); f.setOwner(a); } catch (PLUSException e) { log.warning(e.getMessage()); } } catch (IOException exc) { log.warning(exc.getMessage()); } } try { f.getMetadata().put("isLink", "" + file.getCanonicalFile().equals(file.getAbsoluteFile())); } catch (Exception exc) {; } try { f.getMetadata().put("exists", "" + file.exists()); } catch (Exception exc) {; } try { f.getMetadata().put("path", file.getAbsolutePath()); } catch (Exception exc) {; } try { f.getMetadata().put("canonical", file.getCanonicalPath()); } catch (Exception exc) {; } try { f.getMetadata().put("directory", "" + file.isDirectory()); } catch (Exception exc) {; } try { f.getMetadata().put("lastmodified", "" + file.lastModified()); } catch (Exception exc) {; } try { f.getMetadata().put("size", "" + file.length()); } catch (Exception exc) {; } return f; } // End extractMetadata
/** * Takes a list of files as arguments; hashes each file, and creates a corresponding PLUSFile * provenance object in the local repository. Creates the corresponding hash metadata entry and an * NPE to the hash value. * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; LocalProvenanceClient client = new LocalProvenanceClient(); while ((line = br.readLine()) != null) { String filename = line.trim(); File f = new File(filename); if (!f.exists()) { System.err.println(filename + " does not exist."); continue; } else if (f.isDirectory()) continue; PLUSFile fl = new PLUSFile(f); String hash = fl.hash(); NonProvenanceEdge npe = new NonProvenanceEdge(fl, hash, NonProvenanceEdge.NPE_TYPE_CONTENT_HASH); ProvenanceCollection c = ProvenanceCollection.collect(fl); c.addNonProvenanceEdge(npe); client.report(c); try { URI u = new URI("http", "//" + filename, null); npe = new NonProvenanceEdge(fl.getId(), u.toString(), NonProvenanceEdge.NPE_TYPE_URI); // log.info("Wrote URL " + u); client.report(ProvenanceCollection.collect(npe)); } catch (Exception exc) { System.err.println("Illegal URI: " + filename + " - " + exc.getMessage()); } System.out.println("Wrote " + fl + " => " + fl.getId()); } // End while } // End main