List of usage examples for java.nio.file Files isSymbolicLink
public static boolean isSymbolicLink(Path path)
From source file:org.codice.ddf.migration.util.MigratableUtil.java
private boolean isSourceMigratable(Path source, Function<String, PathMigrationWarning> pathWarningBuilder, Collection<MigrationWarning> warnings) { if (source.isAbsolute()) { warnings.add(pathWarningBuilder.apply("is absolute")); return false; }/* w w w .ja va2 s. c o m*/ if (Files.isSymbolicLink(source)) { warnings.add(pathWarningBuilder.apply("contains a symbolic link")); return false; } try { if (!ddfHome.resolve(source).toRealPath().startsWith(ddfHome.toRealPath())) { warnings.add(pathWarningBuilder.apply(String.format("is outside [%s]", ddfHome))); return false; } } catch (IOException e) { warnings.add(pathWarningBuilder.apply("does not exist or cannot be read")); return false; } return true; }
From source file:org.codice.ddf.migration.util.MigratableUtilTest.java
@Test public void copyFileFromSystemPropertyValueSourceHasSymbolicLink() throws IOException { when(Files.isSymbolicLink(VALID_SOURCE_FILE)).thenReturn(true); MigratableUtil migratableUtil = new MigratableUtil(); migratableUtil.copyFileFromSystemPropertyValue(SOURCE_PATH_PROPERTY_NAME, VALID_DESTINATION_PATH, warnings); assertWarnings("contains a symbolic link"); }
From source file:backtype.storm.localizer.LocalizerTest.java
public void testArchives(String archivePath, boolean supportSymlinks, int size) throws Exception { Map conf = new HashMap(); // set clean time really high so doesn't kick in conf.put(Config.SUPERVISOR_LOCALIZER_CACHE_CLEANUP_INTERVAL_MS, 60 * 60 * 1000); String key1 = new File(archivePath).getName(); String topo1 = "topo1"; Localizer localizer = new TestLocalizer(conf, baseDir.toString()); // set really small so will do cleanup localizer.setTargetCacheSize(1);/* w ww . j a va2 s . c om*/ ReadableBlobMeta rbm = new ReadableBlobMeta(); rbm.set_settable(new SettableBlobMeta(BlobStoreAclHandler.WORLD_EVERYTHING)); when(mockblobstore.getBlobMeta(key1)).thenReturn(rbm); when(mockblobstore.getBlob(key1)).thenReturn(new TestInputStreamWithMeta(new FileInputStream(archivePath))); long timeBefore = System.nanoTime(); File user1Dir = localizer.getLocalUserFileCacheDir(user1); assertTrue("failed to create user dir", user1Dir.mkdirs()); LocalizedResource lrsrc = localizer.getBlob(new LocalResource(key1, true), user1, topo1, user1Dir); long timeAfter = System.nanoTime(); String expectedUserDir = joinPath(baseDir.toString(), Localizer.USERCACHE, user1); String expectedFileDir = joinPath(expectedUserDir, Localizer.FILECACHE, Localizer.ARCHIVESDIR); assertTrue("user filecache dir not created", new File(expectedFileDir).exists()); File keyFile = new File(expectedFileDir, key1 + ".0"); assertTrue("blob not created", keyFile.exists()); assertTrue("blob is not uncompressed", keyFile.isDirectory()); File symlinkFile = new File(keyFile, "tmptestsymlink"); if (supportSymlinks) { assertTrue("blob uncompressed doesn't contain symlink", Files.isSymbolicLink(symlinkFile.toPath())); } else { assertTrue("blob symlink file doesn't exist", symlinkFile.exists()); } LocalizedResourceSet lrsrcSet = localizer.getUserResources().get(user1); assertEquals("local resource set size wrong", 1, lrsrcSet.getSize()); assertEquals("user doesn't match", user1, lrsrcSet.getUser()); LocalizedResource key1rsrc = lrsrcSet.get(key1, true); assertNotNull("Local resource doesn't exist but should", key1rsrc); assertEquals("key doesn't match", key1, key1rsrc.getKey()); assertEquals("refcount doesn't match", 1, key1rsrc.getRefCount()); assertEquals("file path doesn't match", keyFile.toString(), key1rsrc.getFilePathWithVersion()); assertEquals("size doesn't match", size, key1rsrc.getSize()); assertTrue("timestamp not within range", (key1rsrc.getLastAccessTime() >= timeBefore && key1rsrc.getLastAccessTime() <= timeAfter)); timeBefore = System.nanoTime(); localizer.removeBlobReference(lrsrc.getKey(), user1, topo1, true); timeAfter = System.nanoTime(); lrsrcSet = localizer.getUserResources().get(user1); assertEquals("local resource set size wrong", 1, lrsrcSet.getSize()); key1rsrc = lrsrcSet.get(key1, true); assertNotNull("Local resource doesn't exist but should", key1rsrc); assertEquals("refcount doesn't match", 0, key1rsrc.getRefCount()); assertTrue("timestamp not within range", (key1rsrc.getLastAccessTime() >= timeBefore && key1rsrc.getLastAccessTime() <= timeAfter)); // should remove the blob since cache size set really small localizer.handleCacheCleanup(); lrsrcSet = localizer.getUserResources().get(user1); assertFalse("blob contents not deleted", symlinkFile.exists()); assertFalse("blob not deleted", keyFile.exists()); assertFalse("blob file dir not deleted", new File(expectedFileDir).exists()); assertFalse("blob dir not deleted", new File(expectedUserDir).exists()); assertNull("user set should be null", lrsrcSet); }
From source file:org.apache.storm.localizer.LocalizerTest.java
public void testArchives(String archivePath, boolean supportSymlinks, int size) throws Exception { if (Utils.isOnWindows()) { // Windows should set this to false cause symlink in compressed file doesn't work properly. supportSymlinks = false;/* ww w. j a v a 2s .c o m*/ } Map conf = new HashMap(); // set clean time really high so doesn't kick in conf.put(Config.SUPERVISOR_LOCALIZER_CACHE_CLEANUP_INTERVAL_MS, 60 * 60 * 1000); String key1 = new File(archivePath).getName(); String topo1 = "topo1"; Localizer localizer = new TestLocalizer(conf, baseDir.toString()); // set really small so will do cleanup localizer.setTargetCacheSize(1); ReadableBlobMeta rbm = new ReadableBlobMeta(); rbm.set_settable(new SettableBlobMeta(BlobStoreAclHandler.WORLD_EVERYTHING)); when(mockblobstore.getBlobMeta(key1)).thenReturn(rbm); when(mockblobstore.getBlob(key1)).thenReturn(new TestInputStreamWithMeta(new FileInputStream(archivePath))); long timeBefore = System.nanoTime(); File user1Dir = localizer.getLocalUserFileCacheDir(user1); assertTrue("failed to create user dir", user1Dir.mkdirs()); LocalizedResource lrsrc = localizer.getBlob(new LocalResource(key1, true), user1, topo1, user1Dir); long timeAfter = System.nanoTime(); String expectedUserDir = joinPath(baseDir.toString(), Localizer.USERCACHE, user1); String expectedFileDir = joinPath(expectedUserDir, Localizer.FILECACHE, Localizer.ARCHIVESDIR); assertTrue("user filecache dir not created", new File(expectedFileDir).exists()); File keyFile = new File(expectedFileDir, key1 + ".0"); assertTrue("blob not created", keyFile.exists()); assertTrue("blob is not uncompressed", keyFile.isDirectory()); File symlinkFile = new File(keyFile, "tmptestsymlink"); if (supportSymlinks) { assertTrue("blob uncompressed doesn't contain symlink", Files.isSymbolicLink(symlinkFile.toPath())); } else { assertTrue("blob symlink file doesn't exist", symlinkFile.exists()); } LocalizedResourceSet lrsrcSet = localizer.getUserResources().get(user1); assertEquals("local resource set size wrong", 1, lrsrcSet.getSize()); assertEquals("user doesn't match", user1, lrsrcSet.getUser()); LocalizedResource key1rsrc = lrsrcSet.get(key1, true); assertNotNull("Local resource doesn't exist but should", key1rsrc); assertEquals("key doesn't match", key1, key1rsrc.getKey()); assertEquals("refcount doesn't match", 1, key1rsrc.getRefCount()); assertEquals("file path doesn't match", keyFile.toString(), key1rsrc.getFilePathWithVersion()); assertEquals("size doesn't match", size, key1rsrc.getSize()); assertTrue("timestamp not within range", (key1rsrc.getLastAccessTime() >= timeBefore && key1rsrc.getLastAccessTime() <= timeAfter)); timeBefore = System.nanoTime(); localizer.removeBlobReference(lrsrc.getKey(), user1, topo1, true); timeAfter = System.nanoTime(); lrsrcSet = localizer.getUserResources().get(user1); assertEquals("local resource set size wrong", 1, lrsrcSet.getSize()); key1rsrc = lrsrcSet.get(key1, true); assertNotNull("Local resource doesn't exist but should", key1rsrc); assertEquals("refcount doesn't match", 0, key1rsrc.getRefCount()); assertTrue("timestamp not within range", (key1rsrc.getLastAccessTime() >= timeBefore && key1rsrc.getLastAccessTime() <= timeAfter)); // should remove the blob since cache size set really small localizer.handleCacheCleanup(); lrsrcSet = localizer.getUserResources().get(user1); assertFalse("blob contents not deleted", symlinkFile.exists()); assertFalse("blob not deleted", keyFile.exists()); assertFalse("blob file dir not deleted", new File(expectedFileDir).exists()); assertFalse("blob dir not deleted", new File(expectedUserDir).exists()); assertNull("user set should be null", lrsrcSet); }
From source file:org.everit.osgi.dev.maven.util.FileManager.java
/** * Copies an inputstream into a file. In case the file already exists, only those bytes are * overwritten in the target file that are changed. * * @param is//from www . j a va 2s . co m * The inputstream of the source. * @param targetFile * The file that will be overridden if it is necessary. * @throws IOException * if there is an error during copying the file. * @return true if the target file had to be changed, false if the target file was not changed. */ public boolean overCopyFile(final InputStream is, final File targetFile) throws IOException { boolean fileChanged = false; boolean symbolicLink = Files.isSymbolicLink(targetFile.toPath()); if (symbolicLink) { targetFile.delete(); } long sum = 0; byte[] buffer = new byte[1024]; try (RandomAccessFile targetRAF = new RandomAccessFile(targetFile, "rw");) { long originalTargetLength = targetFile.length(); int r = is.read(buffer); while (r > -1) { sum += r; byte[] bytesInTarget = tryReadingAmount(targetRAF, r); if (!PluginUtil.isBufferSame(buffer, r, bytesInTarget)) { fileChanged = true; targetRAF.seek(targetRAF.getFilePointer() - bytesInTarget.length); targetRAF.write(buffer, 0, r); } r = is.read(buffer); } if (sum < originalTargetLength) { targetRAF.setLength(sum); } } return fileChanged; }
From source file:org.apache.storm.localizer.LocalizedResource.java
@Override public void cleanupOrphanedData() throws IOException { //There are a few possible files that we would want to clean up //baseDir + "/" + "_tmp_" + baseName //baseDir + "/" + "_tmp_" + baseName + ".current" //baseDir + "/" + baseName.<VERSION> //baseDir + "/" + baseName.current //baseDir + "/" + baseName.version //In general we always want to delete the _tmp_ files if they are there. Path tmpOutput = tmpOutputLocation(); Files.deleteIfExists(tmpOutput); Path tmpSym = tmpSymlinkLocation(); Files.deleteIfExists(tmpSym); try {//from w ww.j av a2 s . com String baseName = getKey(); long version = getLocalVersion(); Path current = getCurrentSymlinkPath(); //If .current and .version do not match, we roll back the .version file to match // what .current is pointing to. if (Files.exists(current) && Files.isSymbolicLink(current)) { Path versionFile = Files.readSymbolicLink(current); Matcher m = VERSION_FILE_PATTERN.matcher(versionFile.getFileName().toString()); if (m.matches()) { long foundVersion = Long.valueOf(m.group(2)); if (foundVersion != version) { LOG.error("{} does not match the version file so fix the version file", current); //The versions are different so roll back to whatever current is try (PrintWriter restoreWriter = new PrintWriter( new BufferedWriter(new FileWriter(versionFilePath.toFile(), false)))) { restoreWriter.println(foundVersion); } version = foundVersion; } } } // Finally delete any baseName.<VERSION> files that are not pointed to by the current version final long finalVersion = version; LOG.debug("Looking to clean up after {} in {}", getKey(), baseDir); try (DirectoryStream<Path> ds = fsOps.newDirectoryStream(baseDir, (path) -> { Matcher m = VERSION_FILE_PATTERN.matcher(path.getFileName().toString()); if (m.matches()) { long foundVersion = Long.valueOf(m.group(2)); return m.group(1).equals(baseName) && foundVersion != finalVersion; } return false; })) { for (Path p : ds) { LOG.info("Cleaning up old localized resource file {}", p); if (Files.isDirectory(p)) { FileUtils.deleteDirectory(p.toFile()); } else { fsOps.deleteIfExists(p.toFile()); } } } } catch (NoSuchFileException e) { LOG.warn("Nothing to cleanup with badeDir {} even though we expected there to be something there", baseDir); } }
From source file:org.codice.ddf.configuration.migration.ExportMigrationEntryImpl.java
private boolean isMigratable() { final ExportMigrationReportImpl report = context.getReport(); if (path.isAbsolute()) { report.recordExternal(this, false); report.record(newWarning(String.format("is outside [%s]", context.getPathUtils().getDDFHome()))); return false; } else if (Files.isSymbolicLink(absolutePath)) { report.recordExternal(this, true); report.record(newWarning("is a symbolic link")); return false; }//w w w . j a v a 2s .c om return true; }
From source file:org.apache.jena.fuseki.mgt.ActionDatasets.java
@Override protected void execDeleteItem(HttpAction action) { // Does not exist? String name = action.getDatasetName(); if (name == null) name = ""; action.log.info(format("[%d] DELETE ds=%s", action.id, name)); if (!action.getDataAccessPointRegistry().isRegistered(name)) ServletOps.errorNotFound("No such dataset registered: " + name); // This acts as a lock. systemDSG.begin(ReadWrite.WRITE);// w w w . j a v a2s . c o m boolean committed = false; try { // Here, go offline. // Need to reference count operations when they drop to zero // or a timer goes off, we delete the dataset. DataAccessPoint ref = action.getDataAccessPointRegistry().get(name); // Redo check inside transaction. if (ref == null) ServletOps.errorNotFound("No such dataset registered: " + name); String filename = name.startsWith("/") ? name.substring(1) : name; List<String> configurationFiles = FusekiWebapp.existingConfigurationFile(filename); if (configurationFiles.size() != 1) { // This should not happen. action.log.warn(format("[%d] There are %d configuration files, not one.", action.id, configurationFiles.size())); ServletOps.errorOccurred(format( "There are %d configuration files, not one. Delete not performed; clearup of the filesystem needed.", action.id, configurationFiles.size())); } String cfgPathname = configurationFiles.get(0); // Delete configuration file. // Once deleted, server restart will not have the database. FileOps.deleteSilent(cfgPathname); // Get before removing. DataService dataService = ref.getDataService(); // Make it invisible in this running server. action.getDataAccessPointRegistry().remove(name); // Delete the database for real only when it is in the server "run/databases" // area. Don't delete databases that reside elsewhere. We do delete the // configuration file, so the databases will not be associated with the server // anymore. // JENA-1586: Remove from current running Fuseki server. boolean isTDB1 = org.apache.jena.tdb.sys.TDBInternal.isTDB1(dataService.getDataset()); boolean isTDB2 = org.apache.jena.tdb2.sys.TDBInternal.isTDB2(dataService.getDataset()); dataService.shutdown(); // JENA-1481: Really delete files. if ((isTDB1 || isTDB2)) { // Delete databases created by the UI, or the admin operation, which are // in predictable, unshared location on disk. // There may not be any database files, the in-memory case. Path pDatabase = FusekiWebapp.dirDatabases.resolve(filename); if (Files.exists(pDatabase)) { try { if (Files.isSymbolicLink(pDatabase)) { action.log.info(format("[%d] Database is a symbolic link, not removing files", action.id, pDatabase)); } else { IO.deleteAll(pDatabase); action.log.info(format("[%d] Deleted database files %s", action.id, pDatabase)); } } catch (RuntimeIOException ex) { action.log.error(format("[%d] Error while deleting database files %s: %s", action.id, pDatabase, ex.getMessage()), ex); // But we have managed to remove it from the running server, and removed its configuration, so declare victory. } } } // -- System database // Find graph associated with this dataset name. // (Statically configured databases aren't in the system database.) Node n = NodeFactory.createLiteral(DataAccessPoint.canonical(name)); Quad q = getOne(systemDSG, null, null, pServiceName.asNode(), n); // if ( q == null ) // ServletOps.errorBadRequest("Failed to find dataset for '"+name+"'"); if (q != null) { Node gn = q.getGraph(); //action.log.info("SHUTDOWN NEEDED"); // To ensure it goes away? systemDSG.deleteAny(gn, null, null, null); } systemDSG.commit(); committed = true; ServletOps.success(action); } finally { if (!committed) systemDSG.abort(); systemDSG.end(); } }
From source file:org.codice.ddf.migration.util.MigratableUtilTest.java
@Test public void copyFileFromJavaPropertyValueSourceHasSymbolicLink() throws IOException { when(Files.isSymbolicLink(VALID_SOURCE_FILE)).thenReturn(true); MigratableUtil migratableUtil = new MigratableUtil(); migratableUtil.copyFileFromJavaPropertyValue(JAVA_PROPERTIES_FILE, SOURCE_PATH_PROPERTY_NAME, VALID_DESTINATION_PATH, warnings); assertWarnings("contains a symbolic link"); }