OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.incrementalinstall; | |
6 | |
7 import android.util.Log; | |
8 | |
9 import java.io.File; | |
10 import java.io.FileOutputStream; | |
11 import java.io.IOException; | |
12 import java.nio.channels.FileLock; | |
13 import java.util.concurrent.Callable; | |
14 | |
15 /** | |
16 * Helpers for dealing with .lock files used during install / first run. | |
17 */ | |
18 final class LockFile { | |
19 private static final String TAG = "cr.incrementalinstall"; | |
20 | |
21 private final File mFile; | |
22 private final FileOutputStream mOutputStream; | |
23 private final FileLock mFileLock; | |
24 | |
25 private LockFile(File file, FileOutputStream outputStream, FileLock fileLock ) { | |
26 mFile = file; | |
27 mOutputStream = outputStream; | |
28 mFileLock = fileLock; | |
29 } | |
30 | |
31 /** | |
32 * Clears the lock file by writing to it (making it non-zero in length); | |
33 */ | |
34 static void clearInstallerLock(File lockFile) throws IOException { | |
35 Log.i(TAG, "Clearing " + lockFile); | |
36 // On Android M, we can't delete files in /data/local/tmp, so we write t o it instead. | |
nyquist
2015/09/16 06:30:46
Nit: Should this be "Android M+"?
agrieve
2015/09/16 14:59:59
Done.
| |
37 FileOutputStream os = new FileOutputStream(lockFile); | |
38 os.write(1); | |
39 os.close(); | |
40 } | |
41 | |
42 /** | |
43 * Waits for the given file to be non-zero in length. | |
44 */ | |
45 static void waitForInstallerLock(final File file, long timeoutMs) { | |
46 pollingWait(new Callable<Boolean>() { | |
47 @Override public Boolean call() { | |
48 return !installerLockExists(file); | |
49 } | |
50 }, file, timeoutMs); | |
51 } | |
52 | |
53 /** | |
54 * Waits for the given file to be non-zero in length. | |
55 */ | |
56 private static void pollingWait(Callable<Boolean> func, File file, long time outMs) { | |
57 final long pollIntervalMs = 200; | |
nyquist
2015/09/16 06:30:46
Nit: Unnecessary final
agrieve
2015/09/16 14:59:59
Done.
| |
58 for (int i = 0; i < timeoutMs / pollIntervalMs; i++) { | |
59 try { | |
60 if (func.call()) { | |
61 if (i > 0) { | |
62 Log.i(TAG, "Finished waiting on lock file: " + file); | |
63 } | |
64 return; | |
65 } else if (i == 0) { | |
66 Log.i(TAG, "Waiting on lock file: " + file); | |
67 } | |
68 } catch (Exception e) { | |
69 throw new RuntimeException(e); | |
70 } | |
71 try { | |
72 Thread.sleep(pollIntervalMs); | |
73 } catch (InterruptedException e) { | |
74 // Should never happen. | |
75 } | |
76 } | |
77 throw new RuntimeException("Timed out waiting for lock file: " + file); | |
78 } | |
79 | |
80 /** | |
81 * Returns whether the given lock file is missing or is in the locked state. | |
82 */ | |
83 static boolean installerLockExists(File file) { | |
84 return !file.exists() || file.length() == 0; | |
85 } | |
86 | |
87 /** | |
88 * Attempts to acquire a lock for the given file. | |
89 * @return Returns the FileLock if it was acquired, or null otherwise. | |
90 */ | |
91 static LockFile acquireRuntimeLock(final File file) { | |
nyquist
2015/09/16 06:30:46
Nit: Unnecessary final
agrieve
2015/09/16 14:59:59
Done.
| |
92 try { | |
93 FileOutputStream outputStream = new FileOutputStream(file); | |
94 FileLock lock = outputStream.getChannel().tryLock(); | |
95 if (lock != null) { | |
96 Log.i(TAG, "Created lock file: " + file); | |
97 return new LockFile(file, outputStream, lock); | |
98 } | |
99 outputStream.close(); | |
100 } catch (IOException e) { | |
101 // Do nothing. We didn't get the lock. | |
102 } | |
103 return null; | |
104 } | |
105 | |
106 /** | |
107 * Waits for the given file to not exist. | |
108 */ | |
109 static void waitForRuntimeLock(final File file, long timeoutMs) { | |
110 pollingWait(new Callable<Boolean>() { | |
111 @Override public Boolean call() { | |
112 return !file.exists(); | |
113 } | |
114 }, file, timeoutMs); | |
115 } | |
116 | |
117 /** | |
118 * Releases and deletes the lock file. | |
119 */ | |
120 void release() throws IOException { | |
121 Log.i(TAG, "Deleting lock file: " + mFile); | |
122 mFileLock.release(); | |
123 mOutputStream.close(); | |
124 mFile.delete(); | |
nyquist
2015/09/16 06:30:46
Nit: Do we need to do anything special if this ope
agrieve
2015/09/16 14:59:59
Done.
| |
125 } | |
126 } | |
OLD | NEW |