View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util;
20  
21  import java.io.Closeable;
22  import java.io.IOException;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.StandardCopyOption;
26  import java.util.concurrent.ThreadLocalRandom;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * A utility class to write files.
32   *
33   * @since 1.9.0
34   */
35  public final class FileUtils {
36      private FileUtils() {
37          // hide constructor
38      }
39  
40      /**
41       * A temporary file, that is removed when closed.
42       */
43      public interface TempFile extends Closeable {
44          /**
45           * Returns the path of the created temp file.
46           */
47          Path getPath();
48      }
49  
50      /**
51       * A collocated temporary file, that resides next to a "target" file, and is removed when closed.
52       */
53      public interface CollocatedTempFile extends TempFile {
54          /**
55           * Atomically moves temp file to target file it is collocated with.
56           */
57          void move() throws IOException;
58      }
59  
60      /**
61       * Creates a {@link TempFile} instance and backing temporary file on file system. It will be located in the default
62       * temporary-file directory. Returned instance should be handled in try-with-resource construct and created
63       * temp file is removed (if exists) when returned instance is closed.
64       * <p>
65       * This method uses {@link Files#createTempFile(String, String, java.nio.file.attribute.FileAttribute[])} to create
66       * the temporary file on file system.
67       */
68      public static TempFile newTempFile() throws IOException {
69          Path tempFile = Files.createTempFile("resolver", "tmp");
70          return new TempFile() {
71              @Override
72              public Path getPath() {
73                  return tempFile;
74              }
75  
76              @Override
77              public void close() throws IOException {
78                  Files.deleteIfExists(tempFile);
79              }
80          };
81      }
82  
83      /**
84       * Creates a {@link CollocatedTempFile} instance for given file without backing file. The path will be located in
85       * same directory where given file is, and will reuse its name for generated (randomized) name. Returned instance
86       * should be handled in try-with-resource and created temp path is removed (if exists) when returned instance is
87       * closed. The {@link CollocatedTempFile#move()} makes possible to atomically replace passed in file with the
88       * processed content written into a file backing the {@link CollocatedTempFile} instance.
89       * <p>
90       * The {@code file} nor it's parent directories have to exist. The parent directories are created if needed.
91       * <p>
92       * This method uses {@link Path#resolve(String)} to create the temporary file path in passed in file parent
93       * directory, but it does NOT create backing file on file system.
94       */
95      public static CollocatedTempFile newTempFile(Path file) throws IOException {
96          Path parent = requireNonNull(file.getParent(), "file must have parent");
97          Files.createDirectories(parent);
98          Path tempFile = parent.resolve(file.getFileName() + "."
99                  + 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 }