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.io.InputStream;
24  import java.io.OutputStream;
25  import java.nio.ByteBuffer;
26  import java.nio.file.Files;
27  import java.nio.file.Path;
28  import java.nio.file.StandardCopyOption;
29  import java.util.concurrent.ThreadLocalRandom;
30  import java.util.concurrent.atomic.AtomicBoolean;
31  
32  import static java.util.Objects.requireNonNull;
33  
34  /**
35   * A utility class to write files.
36   *
37   * @since 1.9.0
38   * @deprecated Do not use this class; is not used in Resolver (see corresponding processor components in {@code org.eclipse.aether.spi.io} package).
39   */
40  @Deprecated
41  public final class FileUtils {
42      /**
43       * Logic borrowed from Commons-Lang3: we really need only this, to decide do we NIO2 file ops or not.
44       * For some reason non-NIO2 works better on Windows.
45       */
46      private static final boolean IS_WINDOWS =
47              System.getProperty("os.name", "unknown").startsWith("Windows");
48  
49      /**
50       * Escape hatch if atomic move is not desired on system we run on.
51       *
52       * @since 2.0.12
53       */
54      private static final boolean ATOMIC_MOVE =
55              Boolean.parseBoolean(System.getProperty(FileUtils.class.getName() + "ATOMIC_MOVE", "true"));
56  
57      private FileUtils() {
58          // hide constructor
59      }
60  
61      /**
62       * A temporary file, that is removed when closed.
63       */
64      public interface TempFile extends Closeable {
65          /**
66           * Returns the path of the created temp file.
67           */
68          Path getPath();
69      }
70  
71      /**
72       * A collocated temporary file, that resides next to a "target" file, and is removed when closed.
73       */
74      public interface CollocatedTempFile extends TempFile {
75          /**
76           * Upon close, atomically moves temp file to target file it is collocated with overwriting target (if exists).
77           * Invocation of this method merely signals that caller ultimately wants temp file to replace the target
78           * file, but when this method returns, the move operation did not yet happen, it will happen when this
79           * instance is closed.
80           * <p>
81           * Invoking this method <em>without writing to temp file</em> {@link #getPath()} (thus, not creating a temp
82           * file to be moved) is considered a bug, a mistake of the caller. Caller of this method should ensure
83           * that this method is invoked ONLY when the temp file is created and moving it to its final place is
84           * required.
85           */
86          void move() throws IOException;
87      }
88  
89      /**
90       * Creates a {@link TempFile} instance and backing temporary file on file system. It will be located in the default
91       * temporary-file directory. Returned instance should be handled in try-with-resource construct and created
92       * temp file is removed (if exists) when returned instance is closed.
93       * <p>
94       * This method uses {@link Files#createTempFile(String, String, java.nio.file.attribute.FileAttribute[])} to create
95       * the temporary file on file system.
96       */
97      public static TempFile newTempFile() throws IOException {
98          Path tempFile = Files.createTempFile("resolver", "tmp");
99          return new TempFile() {
100             @Override
101             public Path getPath() {
102                 return tempFile;
103             }
104 
105             @Override
106             public void close() throws IOException {
107                 Files.deleteIfExists(tempFile);
108             }
109         };
110     }
111 
112     /**
113      * Creates a {@link CollocatedTempFile} instance for given file without backing file. The path will be located in
114      * same directory where given file is, and will reuse its name for generated (randomized) name. Returned instance
115      * should be handled in try-with-resource and created temp path is removed (if exists) when returned instance is
116      * closed. The {@link CollocatedTempFile#move()} makes possible to atomically replace passed in file with the
117      * processed content written into a file backing the {@link CollocatedTempFile} instance.
118      * <p>
119      * The {@code file} nor it's parent directories have to exist. The parent directories are created if needed.
120      * <p>
121      * This method uses {@link Path#resolve(String)} to create the temporary file path in passed in file parent
122      * directory, but it does NOT create backing file on file system.
123      */
124     public static CollocatedTempFile newTempFile(Path file) throws IOException {
125         Path parent = requireNonNull(file.getParent(), "file must have parent");
126         Files.createDirectories(parent);
127         Path tempFile = parent.resolve(file.getFileName() + "."
128                 + Long.toUnsignedString(ThreadLocalRandom.current().nextLong()) + ".tmp");
129         return new CollocatedTempFile() {
130             private final AtomicBoolean wantsMove = new AtomicBoolean(false);
131             private final StandardCopyOption[] copyOption = FileUtils.ATOMIC_MOVE
132                     ? new StandardCopyOption[] {StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING}
133                     : new StandardCopyOption[] {StandardCopyOption.REPLACE_EXISTING};
134 
135             @Override
136             public Path getPath() {
137                 return tempFile;
138             }
139 
140             @Override
141             public void move() {
142                 wantsMove.set(true);
143             }
144 
145             @Override
146             public void close() throws IOException {
147                 if (wantsMove.get()) {
148                     if (IS_WINDOWS) {
149                         copy(tempFile, file);
150                     } else {
151                         Files.move(tempFile, file, copyOption);
152                     }
153                 }
154                 Files.deleteIfExists(tempFile);
155             }
156         };
157     }
158 
159     /**
160      * On Windows we use pre-NIO2 way to copy files, as for some reason it works. Beat me why.
161      */
162     private static void copy(Path source, Path target) throws IOException {
163         ByteBuffer buffer = ByteBuffer.allocate(1024 * 32);
164         byte[] array = buffer.array();
165         try (InputStream is = Files.newInputStream(source);
166                 OutputStream os = Files.newOutputStream(target)) {
167             while (true) {
168                 int bytes = is.read(array);
169                 if (bytes < 0) {
170                     break;
171                 }
172                 os.write(array, 0, bytes);
173             }
174         }
175     }
176 
177     /**
178      * A file writer, that accepts a {@link Path} to write some content to. Note: the file denoted by path may exist,
179      * hence implementation have to ensure it is able to achieve its goal ("replace existing" option or equivalent
180      * should be used).
181      */
182     @FunctionalInterface
183     public interface FileWriter {
184         void write(Path path) throws IOException;
185     }
186 
187     /**
188      * Writes file without backup.
189      *
190      * @param target that is the target file (must be file, the path must have parent)
191      * @param writer the writer that will accept a {@link Path} to write content to
192      * @throws IOException if at any step IO problem occurs
193      */
194     public static void writeFile(Path target, FileWriter writer) throws IOException {
195         writeFile(target, writer, false);
196     }
197 
198     /**
199      * Writes file with backup copy (appends ".bak" extension).
200      *
201      * @param target that is the target file (must be file, the path must have parent)
202      * @param writer the writer that will accept a {@link Path} to write content to
203      * @throws IOException if at any step IO problem occurs
204      */
205     public static void writeFileWithBackup(Path target, FileWriter writer) throws IOException {
206         writeFile(target, writer, true);
207     }
208 
209     /**
210      * Utility method to write out file to disk in "atomic" manner, with optional backups (".bak") if needed. This
211      * ensures that no other thread or process will be able to read not fully written files. Finally, this method
212      * may create the needed parent directories, if the passed in target parents does not exist.
213      *
214      * @param target   that is the target file (must be an existing or non-existing file, the path must have parent)
215      * @param writer   the writer that will accept a {@link Path} to write content to
216      * @param doBackup if {@code true}, and target file is about to be overwritten, a ".bak" file with old contents will
217      *                 be created/overwritten
218      * @throws IOException if at any step IO problem occurs
219      */
220     private static void writeFile(Path target, FileWriter writer, boolean doBackup) throws IOException {
221         requireNonNull(target, "target is null");
222         requireNonNull(writer, "writer is null");
223         Path parent = requireNonNull(target.getParent(), "target must have parent");
224 
225         try (CollocatedTempFile tempFile = newTempFile(target)) {
226             writer.write(tempFile.getPath());
227             if (doBackup && Files.isRegularFile(target)) {
228                 Files.copy(target, parent.resolve(target.getFileName() + ".bak"), StandardCopyOption.REPLACE_EXISTING);
229             }
230             tempFile.move();
231         }
232     }
233 }