Example usage for java.nio.file Files createLink

List of usage examples for java.nio.file Files createLink

Introduction

In this page you can find the example usage for java.nio.file Files createLink.

Prototype

public static Path createLink(Path link, Path existing) throws IOException 

Source Link

Document

Creates a new link (directory entry) for an existing file (optional operation).

Usage

From source file:com.fujitsu.dc.core.model.file.StreamingOutputForDavFile.java

/**
 * ./*  w  w w .  ja v a 2s .  com*/
 * @param fileFullPath ??
 * @throws BinaryDataNotFoundException ?????.
 */
public StreamingOutputForDavFile(String fileFullPath) throws BinaryDataNotFoundException {
    if (!Files.exists(Paths.get(fileFullPath))) {
        throw new BinaryDataNotFoundException(fileFullPath);
    }

    // ????????????
    String hardLinkName = UniqueNameComposer.compose(fileFullPath);

    for (int i = 0; i < maxRetryCount; i++) {
        try {
            synchronized (fileFullPath) {
                // ??.
                hardLinkPath = Files.createLink(Paths.get(hardLinkName), Paths.get(fileFullPath));
            }
            // ????
            hardLinkInput = new BufferedInputStream(new FileInputStream(hardLinkPath.toFile()));
            // ???
            return;
        } catch (IOException e) {
            // ????
            logger.debug(String.format("Creating hard link %s failed. Will try again.", hardLinkName));
            try {
                Thread.sleep(retryInterval);
            } catch (InterruptedException e1) {
                logger.debug("Thread interrupted.");
            }
        }
    }

    throw new BinaryDataNotFoundException("Unable to create hard link for DAV file: " + hardLinkName);
}

From source file:io.personium.core.model.file.StreamingOutputForDavFile.java

/**
 * Constructor./*  ww w  . jav a  2  s.  c om*/
 * @param fileFullPath Full path of the file to be read
 * @param cellId Cell ID
 * @param encryptionType encryption type
 * @throws BinaryDataNotFoundException Error when file does not exist.
 */
public StreamingOutputForDavFile(String fileFullPath, String cellId, String encryptionType)
        throws BinaryDataNotFoundException {
    if (!Files.exists(Paths.get(fileFullPath))) {
        throw new BinaryDataNotFoundException(fileFullPath);
    }

    // ????????????
    String hardLinkName = UniqueNameComposer.compose(fileFullPath);

    for (int i = 0; i < maxRetryCount; i++) {
        try {
            synchronized (fileFullPath) {
                // ??.
                hardLinkPath = Files.createLink(Paths.get(hardLinkName), Paths.get(fileFullPath));
            }
            // ????
            InputStream inputStream;
            if (DataCryptor.ENCRYPTION_TYPE_AES.equals(encryptionType)) {
                // Perform decryption.
                DataCryptor cryptor = new DataCryptor(cellId);
                inputStream = cryptor.decode(new FileInputStream(hardLinkPath.toFile()));
            } else {
                inputStream = new FileInputStream(hardLinkPath.toFile());
            }
            hardLinkInput = new BufferedInputStream(inputStream);
            // ???
            return;
        } catch (IOException e) {
            // ????
            logger.debug(String.format("Creating hard link %s failed. Will try again.", hardLinkName));
            try {
                Thread.sleep(retryInterval);
            } catch (InterruptedException e1) {
                logger.debug("Thread interrupted.");
            }
        }
    }

    throw new BinaryDataNotFoundException("Unable to create hard link for DAV file: " + hardLinkName);
}

From source file:com.ikanow.aleph2.example.flume_harvester.services.FlumeHarvestTechnology.java

@Override
public CompletableFuture<BasicMessageBean> onTestSource(DataBucketBean test_bucket,
        ProcessingTestSpecBean test_spec, IHarvestContext context) {
    try {/*from   w w  w  .j a  va2s.c o  m*/
        // First try stopping any existing running service:
        this.onSuspend(test_bucket, context);

        // Then clean up from the previous test just in case:
        FlumeUtils.getAgents(test_bucket).stream().forEach(agent -> {
            final Collection<SpoolDirConfig> spool_dirs = FlumeUtils.getSpoolDirs(agent);
            FlumeUtils.deleteGeneratedDirs(test_bucket, spool_dirs, true);
            spool_dirs.stream().forEach(Lambdas.wrap_consumer_u(v -> {
                //TODO (ALEPH-10): handle HDFS for the overriden src path (so can go fetch
                //TODO (ALEPH-10): security for this setting...
                final File dest_path = new File(v.path() + "/"
                        + BucketUtils.getUniqueSignature(test_bucket.full_name(), Optional.empty()));
                FileUtils.forceMkdir(dest_path);
                final File src_path = new File(Optional.ofNullable(v.test_src_path()).orElse(v.path()));
                if (src_path.exists()) {
                    FileUtils.listFiles(src_path, null, false).forEach(Lambdas.wrap_consumer_u(file -> {
                        Files.createLink(new File(dest_path.toString() + "/" + file.getName()).toPath(),
                                file.toPath());
                    }));
                }
                //(else just nothing to read on this node)
            }));
        });

        // Now start a normal source except in test mode
        final CompletableFuture<BasicMessageBean> reply = onNewSource(test_bucket, context, true, true);

        return reply;
    } catch (Exception e) {
        _logger.error(ErrorUtils.getLongForm("onTestSource: Unknown error {0}", e));
        return FutureUtils.returnError(e);
    }
}

From source file:com.thebuzzmedia.exiftool.ExifToolNew3.java

void copyFromAsHardLink(File src, File dest, Boolean overwriteIfAlreadyExists) {
    try {/*from w w w .  j a  v a 2s . c o m*/
        if (overwriteIfAlreadyExists) {
            if (dest.exists()) {
                FileUtils.forceDelete(dest);
            }
            FileUtils.forceMkdir(dest.getParentFile());
            Files.createLink(dest.toPath(), src.toPath());
        } else {
            if (dest.exists()) {
                throw new RuntimeException("Destination file " + this + " already exists.");
            } else {
                FileUtils.forceMkdir(dest.getParentFile());
                Files.createLink(dest.toPath(), src.toPath());
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:edu.lternet.pasta.datapackagemanager.DataPackageManagerResource.java

private void createDataFileLink(File entityFile, String dataToken) throws Exception {
    String msg = null;//from   ww w  .  j ava 2s  .co m

    /*
     * Create a link from the path of the data entity for 
     * this revision to the path of the data entity for the
     * prior revision.
     */
    String dataTokenPathStr = String.format("%s/%s", this.tmpDir, dataToken);
    FileSystem fileSystem = FileSystems.getDefault();

    String entityPathStr = entityFile.getAbsolutePath();
    java.nio.file.Path entityPath = fileSystem.getPath(entityPathStr);
    java.nio.file.Path dataTokenPath = fileSystem.getPath(dataTokenPathStr);
    String createLinkMsg = String.format("Creating link from %s to %s", dataTokenPathStr, entityPathStr);
    logger.warn(createLinkMsg);

    try {
        if (SystemUtils.IS_OS_WINDOWS) {
            Files.createLink(dataTokenPath, entityPath);
        } else {
            Files.createSymbolicLink(dataTokenPath, entityPath);
        }
    } catch (FileAlreadyExistsException e) {
        // this is okay, just issue a warning
        msg = String.format("Failed to create link from %s to %s: %s", dataTokenPathStr, entityPathStr,
                e.getMessage());
        logger.warn(msg);
    } catch (Exception e) {
        msg = String.format("Error creating link from %s to %s: %s", dataTokenPathStr, entityPathStr,
                e.getMessage());
        logger.error(msg);
        throw (e);
    }

}

From source file:org.apache.hadoop.hdfs.server.namenode.NNUpgradeUtil.java

/**
 * Perform any steps that must succeed across all storage dirs/JournalManagers
 * involved in an upgrade before proceeding onto the actual upgrade stage. If
 * a call to any JM's or local storage dir's doPreUpgrade method fails, then
 * doUpgrade will not be called for any JM. The existing current dir is
 * renamed to previous.tmp, and then a new, empty current dir is created.
 *
 * @param conf configuration for creating {@link EditLogFileOutputStream}
 * @param sd the storage directory to perform the pre-upgrade procedure.
 * @throws IOException in the event of error
 *///w  ww  .j av a  2 s .  c o m
static void doPreUpgrade(Configuration conf, StorageDirectory sd) throws IOException {
    LOG.info("Starting upgrade of storage directory " + sd.getRoot());

    // rename current to tmp
    renameCurToTmp(sd);

    final File curDir = sd.getCurrentDir();
    final File tmpDir = sd.getPreviousTmp();
    List<String> fileNameList = IOUtils.listDirectory(tmpDir, new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return dir.equals(tmpDir) && name.startsWith(NNStorage.NameNodeFile.EDITS.getName());
        }
    });

    for (String s : fileNameList) {
        File prevFile = new File(tmpDir, s);
        File newFile = new File(curDir, prevFile.getName());
        Files.createLink(newFile.toPath(), prevFile.toPath());
    }
}

From source file:org.apache.pulsar.functions.worker.FunctionActioner.java

private void downloadFile(File pkgFile, boolean isPkgUrlProvided, FunctionMetaData functionMetaData,
        int instanceId) throws FileNotFoundException, IOException {

    FunctionDetails details = functionMetaData.getFunctionDetails();
    File pkgDir = pkgFile.getParentFile();

    if (pkgFile.exists()) {
        log.warn("Function package exists already {} deleting it", pkgFile);
        pkgFile.delete();/*  www . java 2 s.  c om*/
    }

    File tempPkgFile;
    while (true) {
        tempPkgFile = new File(pkgDir,
                pkgFile.getName() + "." + instanceId + "." + UUID.randomUUID().toString());
        if (!tempPkgFile.exists() && tempPkgFile.createNewFile()) {
            break;
        }
    }
    String pkgLocationPath = functionMetaData.getPackageLocation().getPackagePath();
    boolean downloadFromHttp = isPkgUrlProvided && pkgLocationPath.startsWith(HTTP);
    log.info("{}/{}/{} Function package file {} will be downloaded from {}", tempPkgFile, details.getTenant(),
            details.getNamespace(), details.getName(),
            downloadFromHttp ? pkgLocationPath : functionMetaData.getPackageLocation());

    if (downloadFromHttp) {
        Utils.downloadFromHttpUrl(pkgLocationPath, new FileOutputStream(tempPkgFile));
    } else {
        Utils.downloadFromBookkeeper(dlogNamespace, new FileOutputStream(tempPkgFile), pkgLocationPath);
    }

    try {
        // create a hardlink, if there are two concurrent createLink operations, one will fail.
        // this ensures one instance will successfully download the package.
        try {
            Files.createLink(Paths.get(pkgFile.toURI()), Paths.get(tempPkgFile.toURI()));
            log.info("Function package file is linked from {} to {}", tempPkgFile, pkgFile);
        } catch (FileAlreadyExistsException faee) {
            // file already exists
            log.warn("Function package has been downloaded from {} and saved at {}",
                    functionMetaData.getPackageLocation(), pkgFile);
        }
    } finally {
        tempPkgFile.delete();
    }
}

From source file:playRepository.GitRepository.java

/**
 * Clones a local repository./*from   ww w  .  j  ava2s .co m*/
 *
 * This doesn't copy Git objects but hardlink them to save disk space.
 *
 * @param originalProject
 * @param forkProject
 * @throws IOException
 */
protected static void cloneHardLinkedRepository(Project originalProject, Project forkProject)
        throws IOException {
    Repository origin = GitRepository.buildGitRepository(originalProject);
    Repository forked = GitRepository.buildGitRepository(forkProject);
    forked.create();

    final Path originObjectsPath = Paths.get(new File(origin.getDirectory(), "objects").getAbsolutePath());
    final Path forkedObjectsPath = Paths.get(new File(forked.getDirectory(), "objects").getAbsolutePath());

    // Hardlink files .git/objects/ directory to save disk space,
    // but copy .git/info/alternates because the file can be modified.
    SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
        public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException {
            Path newPath = forkedObjectsPath.resolve(originObjectsPath.relativize(file.toAbsolutePath()));
            if (file.equals(forkedObjectsPath.resolve("/info/alternates"))) {
                Files.copy(file, newPath);
            } else {
                FileUtils.mkdirs(newPath.getParent().toFile(), true);
                Files.createLink(newPath, file);
            }
            return java.nio.file.FileVisitResult.CONTINUE;
        }
    };
    Files.walkFileTree(originObjectsPath, visitor);

    // Import refs.
    for (Map.Entry<String, Ref> entry : origin.getAllRefs().entrySet()) {
        RefUpdate updateRef = forked.updateRef(entry.getKey());
        Ref ref = entry.getValue();
        if (ref.isSymbolic()) {
            updateRef.link(ref.getTarget().getName());
        } else {
            updateRef.setNewObjectId(ref.getObjectId());
            updateRef.update();
        }
    }
}