| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.annotation.CurrentTimeMillisLong; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.SuppressLint; |
| 23 |
import android.annotation.SystemApi; |
| 24 |
import android.os.FileUtils; |
| 25 |
import android.os.SystemClock; |
| 26 |
|
| 27 |
import libcore.io.IoUtils; |
| 28 |
|
| 29 |
import java.io.File; |
| 30 |
import java.io.FileInputStream; |
| 31 |
import java.io.FileNotFoundException; |
| 32 |
import java.io.FileOutputStream; |
| 33 |
import java.io.IOException; |
| 34 |
import java.util.function.Consumer; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class AtomicFile { |
| 52 |
private static final String LOG_TAG = "AtomicFile"; |
| 53 |
|
| 54 |
private final File mBaseName; |
| 55 |
private final File mNewName; |
| 56 |
private final File mLegacyBackupName; |
| 57 |
private SystemConfigFileCommitEventLogger mCommitEventLogger; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public AtomicFile(File baseName) { |
| 64 |
this(baseName, (SystemConfigFileCommitEventLogger) null); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public AtomicFile(File baseName, String commitTag) { |
| 72 |
this(baseName, new SystemConfigFileCommitEventLogger(commitTag)); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 82 |
@SuppressLint("StreamFiles") |
| 83 |
public AtomicFile(@NonNull File baseName, |
| 84 |
@Nullable SystemConfigFileCommitEventLogger commitEventLogger) { |
| 85 |
mBaseName = baseName; |
| 86 |
mNewName = new File(baseName.getPath() + ".new"); |
| 87 |
mLegacyBackupName = new File(baseName.getPath() + ".bak"); |
| 88 |
mCommitEventLogger = commitEventLogger; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public File getBaseFile() { |
| 96 |
return mBaseName; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public void delete() { |
| 103 |
mBaseName.delete(); |
| 104 |
mNewName.delete(); |
| 105 |
mLegacyBackupName.delete(); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public FileOutputStream startWrite() throws IOException { |
| 123 |
return startWrite(0); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
@Deprecated |
| 136 |
public FileOutputStream startWrite(long startTime) throws IOException { |
| 137 |
if (mCommitEventLogger != null) { |
| 138 |
if (startTime != 0) { |
| 139 |
mCommitEventLogger.setStartTime(startTime); |
| 140 |
} |
| 141 |
|
| 142 |
mCommitEventLogger.onStartWrite(); |
| 143 |
} |
| 144 |
|
| 145 |
if (mLegacyBackupName.exists()) { |
| 146 |
rename(mLegacyBackupName, mBaseName); |
| 147 |
} |
| 148 |
|
| 149 |
try { |
| 150 |
return new FileOutputStream(mNewName); |
| 151 |
} catch (FileNotFoundException e) { |
| 152 |
File parent = mNewName.getParentFile(); |
| 153 |
if (!parent.mkdirs()) { |
| 154 |
throw new IOException("Failed to create directory for " + mNewName); |
| 155 |
} |
| 156 |
FileUtils.setPermissions(parent.getPath(), FileUtils.S_IRWXU | FileUtils.S_IRWXG |
| 157 |
| FileUtils.S_IXOTH, -1, -1); |
| 158 |
try { |
| 159 |
return new FileOutputStream(mNewName); |
| 160 |
} catch (FileNotFoundException e2) { |
| 161 |
throw new IOException("Failed to create new file " + mNewName, e2); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public void finishWrite(FileOutputStream str) { |
| 173 |
if (str == null) { |
| 174 |
return; |
| 175 |
} |
| 176 |
if (!FileUtils.sync(str)) { |
| 177 |
Log.e(LOG_TAG, "Failed to sync file output stream"); |
| 178 |
} |
| 179 |
try { |
| 180 |
str.close(); |
| 181 |
} catch (IOException e) { |
| 182 |
Log.e(LOG_TAG, "Failed to close file output stream", e); |
| 183 |
} |
| 184 |
rename(mNewName, mBaseName); |
| 185 |
if (mCommitEventLogger != null) { |
| 186 |
mCommitEventLogger.onFinishWrite(); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public void failWrite(FileOutputStream str) { |
| 196 |
if (str == null) { |
| 197 |
return; |
| 198 |
} |
| 199 |
if (!FileUtils.sync(str)) { |
| 200 |
Log.e(LOG_TAG, "Failed to sync file output stream"); |
| 201 |
} |
| 202 |
try { |
| 203 |
str.close(); |
| 204 |
} catch (IOException e) { |
| 205 |
Log.e(LOG_TAG, "Failed to close file output stream", e); |
| 206 |
} |
| 207 |
if (!mNewName.delete()) { |
| 208 |
Log.e(LOG_TAG, "Failed to delete new file " + mNewName); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
@Deprecated public void truncate() throws IOException { |
| 216 |
try { |
| 217 |
FileOutputStream fos = new FileOutputStream(mBaseName); |
| 218 |
FileUtils.sync(fos); |
| 219 |
fos.close(); |
| 220 |
} catch (FileNotFoundException e) { |
| 221 |
throw new IOException("Couldn't append " + mBaseName); |
| 222 |
} catch (IOException e) { |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
@Deprecated public FileOutputStream openAppend() throws IOException { |
| 230 |
try { |
| 231 |
return new FileOutputStream(mBaseName, true); |
| 232 |
} catch (FileNotFoundException e) { |
| 233 |
throw new IOException("Couldn't append " + mBaseName); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public FileInputStream openRead() throws FileNotFoundException { |
| 244 |
if (mLegacyBackupName.exists()) { |
| 245 |
rename(mLegacyBackupName, mBaseName); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
if (mNewName.exists() && mBaseName.exists()) { |
| 256 |
if (!mNewName.delete()) { |
| 257 |
Log.e(LOG_TAG, "Failed to delete outdated new file " + mNewName); |
| 258 |
} |
| 259 |
} |
| 260 |
return new FileInputStream(mBaseName); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public boolean exists() { |
| 269 |
return mBaseName.exists() || mLegacyBackupName.exists(); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
@CurrentTimeMillisLong |
| 279 |
public long getLastModifiedTime() { |
| 280 |
if (mLegacyBackupName.exists()) { |
| 281 |
return mLegacyBackupName.lastModified(); |
| 282 |
} |
| 283 |
return mBaseName.lastModified(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public byte[] readFully() throws IOException { |
| 291 |
FileInputStream stream = openRead(); |
| 292 |
try { |
| 293 |
int pos = 0; |
| 294 |
int avail = stream.available(); |
| 295 |
byte[] data = new byte[avail]; |
| 296 |
while (true) { |
| 297 |
int amt = stream.read(data, pos, data.length-pos); |
| 298 |
|
| 299 |
|
| 300 |
if (amt <= 0) { |
| 301 |
|
| 302 |
|
| 303 |
return data; |
| 304 |
} |
| 305 |
pos += amt; |
| 306 |
avail = stream.available(); |
| 307 |
if (avail > data.length-pos) { |
| 308 |
byte[] newData = new byte[pos+avail]; |
| 309 |
System.arraycopy(data, 0, newData, 0, pos); |
| 310 |
data = newData; |
| 311 |
} |
| 312 |
} |
| 313 |
} finally { |
| 314 |
stream.close(); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
public void write(Consumer<FileOutputStream> writeContent) { |
| 320 |
FileOutputStream out = null; |
| 321 |
try { |
| 322 |
out = startWrite(); |
| 323 |
writeContent.accept(out); |
| 324 |
finishWrite(out); |
| 325 |
} catch (Throwable t) { |
| 326 |
failWrite(out); |
| 327 |
throw ExceptionUtils.propagate(t); |
| 328 |
} finally { |
| 329 |
IoUtils.closeQuietly(out); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
@Override |
| 334 |
public String toString() { |
| 335 |
return "AtomicFile[" + mBaseName + "]"; |
| 336 |
} |
| 337 |
|
| 338 |
private static void rename(File source, File target) { |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
if (target.isDirectory()) { |
| 346 |
if (!target.delete()) { |
| 347 |
Log.e(LOG_TAG, "Failed to delete file which is a directory " + target); |
| 348 |
} |
| 349 |
} |
| 350 |
if (!source.renameTo(target)) { |
| 351 |
Log.e(LOG_TAG, "Failed to rename " + source + " to " + target); |
| 352 |
} |
| 353 |
} |
| 354 |
} |