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.io.InputStream; 024import java.io.OutputStream; 025import java.nio.ByteBuffer; 026import java.nio.file.Files; 027import java.nio.file.Path; 028import java.nio.file.StandardCopyOption; 029import java.util.concurrent.ThreadLocalRandom; 030import java.util.concurrent.atomic.AtomicBoolean; 031 032import static java.util.Objects.requireNonNull; 033 034/** 035 * A utility class to write files. 036 * 037 * @since 1.9.0 038 * @deprecated Do not use this class; is not used in Resolver (see corresponding processor components in {@code org.eclipse.aether.spi.io} package). 039 */ 040@Deprecated 041public final class FileUtils { 042 /** 043 * Logic borrowed from Commons-Lang3: we really need only this, to decide do we NIO2 file ops or not. 044 * For some reason non-NIO2 works better on Windows. 045 */ 046 private static final boolean IS_WINDOWS = 047 System.getProperty("os.name", "unknown").startsWith("Windows"); 048 049 /** 050 * Escape hatch if atomic move is not desired on system we run on. 051 * 052 * @since 2.0.12 053 */ 054 private static final boolean ATOMIC_MOVE = 055 Boolean.parseBoolean(System.getProperty(FileUtils.class.getName() + "ATOMIC_MOVE", "true")); 056 057 private FileUtils() { 058 // hide constructor 059 } 060 061 /** 062 * A temporary file, that is removed when closed. 063 */ 064 public interface TempFile extends Closeable { 065 /** 066 * Returns the path of the created temp file. 067 */ 068 Path getPath(); 069 } 070 071 /** 072 * A collocated temporary file, that resides next to a "target" file, and is removed when closed. 073 */ 074 public interface CollocatedTempFile extends TempFile { 075 /** 076 * Upon close, atomically moves temp file to target file it is collocated with overwriting target (if exists). 077 * Invocation of this method merely signals that caller ultimately wants temp file to replace the target 078 * file, but when this method returns, the move operation did not yet happen, it will happen when this 079 * instance is closed. 080 * <p> 081 * Invoking this method <em>without writing to temp file</em> {@link #getPath()} (thus, not creating a temp 082 * file to be moved) is considered a bug, a mistake of the caller. Caller of this method should ensure 083 * that this method is invoked ONLY when the temp file is created and moving it to its final place is 084 * required. 085 */ 086 void move() throws IOException; 087 } 088 089 /** 090 * Creates a {@link TempFile} instance and backing temporary file on file system. It will be located in the default 091 * temporary-file directory. Returned instance should be handled in try-with-resource construct and created 092 * temp file is removed (if exists) when returned instance is closed. 093 * <p> 094 * This method uses {@link Files#createTempFile(String, String, java.nio.file.attribute.FileAttribute[])} to create 095 * the temporary file on file system. 096 */ 097 public static TempFile newTempFile() throws IOException { 098 Path tempFile = Files.createTempFile("resolver", "tmp"); 099 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}