001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.util;
020
021import java.io.Closeable;
022import java.io.IOException;
023import java.nio.file.Files;
024import java.nio.file.Path;
025import java.nio.file.StandardCopyOption;
026import java.util.concurrent.ThreadLocalRandom;
027
028import static java.util.Objects.requireNonNull;
029
030/**
031 * A utility class to write files.
032 *
033 * @since 1.9.0
034 */
035public final class FileUtils {
036    private FileUtils() {
037        // hide constructor
038    }
039
040    /**
041     * A temporary file, that is removed when closed.
042     */
043    public interface TempFile extends Closeable {
044        /**
045         * Returns the path of the created temp file.
046         */
047        Path getPath();
048    }
049
050    /**
051     * A collocated temporary file, that resides next to a "target" file, and is removed when closed.
052     */
053    public interface CollocatedTempFile extends TempFile {
054        /**
055         * Atomically moves temp file to target file it is collocated with.
056         */
057        void move() throws IOException;
058    }
059
060    /**
061     * Creates a {@link TempFile} instance and backing temporary file on file system. It will be located in the default
062     * temporary-file directory. Returned instance should be handled in try-with-resource construct and created
063     * temp file is removed (if exists) when returned instance is closed.
064     * <p>
065     * This method uses {@link Files#createTempFile(String, String, java.nio.file.attribute.FileAttribute[])} to create
066     * the temporary file on file system.
067     */
068    public static TempFile newTempFile() throws IOException {
069        Path tempFile = Files.createTempFile("resolver", "tmp");
070        return new TempFile() {
071            @Override
072            public Path getPath() {
073                return tempFile;
074            }
075
076            @Override
077            public void close() throws IOException {
078                Files.deleteIfExists(tempFile);
079            }
080        };
081    }
082
083    /**
084     * Creates a {@link CollocatedTempFile} instance for given file without backing file. The path will be located in
085     * same directory where given file is, and will reuse its name for generated (randomized) name. Returned instance
086     * should be handled in try-with-resource and created temp path is removed (if exists) when returned instance is
087     * closed. The {@link CollocatedTempFile#move()} makes possible to atomically replace passed in file with the
088     * processed content written into a file backing the {@link CollocatedTempFile} instance.
089     * <p>
090     * The {@code file} nor it's parent directories have to exist. The parent directories are created if needed.
091     * <p>
092     * This method uses {@link Path#resolve(String)} to create the temporary file path in passed in file parent
093     * directory, but it does NOT create backing file on file system.
094     */
095    public static CollocatedTempFile newTempFile(Path file) throws IOException {
096        Path parent = requireNonNull(file.getParent(), "file must have parent");
097        Files.createDirectories(parent);
098        Path tempFile = parent.resolve(file.getFileName() + "."
099                + Long.toUnsignedString(ThreadLocalRandom.current().nextLong()) + ".tmp");
100        return new CollocatedTempFile() {
101            @Override
102            public Path getPath() {
103                return tempFile;
104            }
105
106            @Override
107            public void move() throws IOException {
108                Files.move(tempFile, file, StandardCopyOption.ATOMIC_MOVE);
109            }
110
111            @Override
112            public void close() throws IOException {
113                Files.deleteIfExists(tempFile);
114            }
115        };
116    }
117
118    /**
119     * A file writer, that accepts a {@link Path} to write some content to. Note: the file denoted by path may exist,
120     * hence implementation have to ensure it is able to achieve its goal ("replace existing" option or equivalent
121     * should be used).
122     */
123    @FunctionalInterface
124    public interface FileWriter {
125        void write(Path path) throws IOException;
126    }
127
128    /**
129     * Writes file without backup.
130     *
131     * @param target that is the target file (must be file, the path must have parent).
132     * @param writer the writer that will accept a {@link Path} to write content to.
133     * @throws IOException if at any step IO problem occurs.
134     */
135    public static void writeFile(Path target, FileWriter writer) throws IOException {
136        writeFile(target, writer, false);
137    }
138
139    /**
140     * Writes file with backup copy (appends ".bak" extension).
141     *
142     * @param target that is the target file (must be file, the path must have parent).
143     * @param writer the writer that will accept a {@link Path} to write content to.
144     * @throws IOException if at any step IO problem occurs.
145     */
146    public static void writeFileWithBackup(Path target, FileWriter writer) throws IOException {
147        writeFile(target, writer, true);
148    }
149
150    /**
151     * Utility method to write out file to disk in "atomic" manner, with optional backups (".bak") if needed. This
152     * ensures that no other thread or process will be able to read not fully written files. Finally, this methos
153     * may create the needed parent directories, if the passed in target parents does not exist.
154     *
155     * @param target   that is the target file (must be an existing or non-existing file, the path must have parent).
156     * @param writer   the writer that will accept a {@link Path} to write content to.
157     * @param doBackup if {@code true}, and target file is about to be overwritten, a ".bak" file with old contents will
158     *                 be created/overwritten.
159     * @throws IOException if at any step IO problem occurs.
160     */
161    private static void writeFile(Path target, FileWriter writer, boolean doBackup) throws IOException {
162        requireNonNull(target, "target is null");
163        requireNonNull(writer, "writer is null");
164        Path parent = requireNonNull(target.getParent(), "target must have parent");
165
166        try (CollocatedTempFile tempFile = newTempFile(target)) {
167            writer.write(tempFile.getPath());
168            if (doBackup && Files.isRegularFile(target)) {
169                Files.copy(target, parent.resolve(target.getFileName() + ".bak"), StandardCopyOption.REPLACE_EXISTING);
170            }
171            tempFile.move();
172        }
173    }
174}