1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.internal.storage.file;
45
46 import static java.nio.charset.StandardCharsets.UTF_8;
47
48 import java.io.BufferedReader;
49 import java.io.File;
50 import java.io.IOException;
51 import java.nio.file.Files;
52 import java.nio.file.NoSuchFileException;
53 import java.nio.file.attribute.FileTime;
54 import java.text.MessageFormat;
55 import java.text.ParseException;
56 import java.time.Instant;
57
58 import org.eclipse.jgit.api.errors.JGitInternalException;
59 import org.eclipse.jgit.internal.JGitText;
60 import org.eclipse.jgit.lib.ConfigConstants;
61 import org.eclipse.jgit.util.GitDateParser;
62 import org.eclipse.jgit.util.SystemReader;
63
64
65
66
67 class GcLog {
68 private final FileRepository repo;
69
70 private final File logFile;
71
72 private final LockFile lock;
73
74 private Instant gcLogExpire;
75
76 private static final String LOG_EXPIRY_DEFAULT = "1.day.ago";
77
78 private boolean nonEmpty = false;
79
80
81
82
83
84
85
86 GcLog(FileRepository repo) {
87 this.repo = repo;
88 logFile = new File(repo.getDirectory(), "gc.log");
89 lock = new LockFile(logFile);
90 }
91
92 private Instant getLogExpiry() throws ParseException {
93 if (gcLogExpire == null) {
94 String logExpiryStr = repo.getConfig().getString(
95 ConfigConstants.CONFIG_GC_SECTION, null,
96 ConfigConstants.CONFIG_KEY_LOGEXPIRY);
97 if (logExpiryStr == null) {
98 logExpiryStr = LOG_EXPIRY_DEFAULT;
99 }
100 gcLogExpire = GitDateParser.parse(logExpiryStr, null,
101 SystemReader.getInstance().getLocale()).toInstant();
102 }
103 return gcLogExpire;
104 }
105
106 private boolean autoGcBlockedByOldLockFile(boolean background) {
107 try {
108 FileTime lastModified = Files.getLastModifiedTime(logFile.toPath());
109 if (lastModified.toInstant().compareTo(getLogExpiry()) > 0) {
110
111 if (!background) {
112 try (BufferedReader reader = Files
113 .newBufferedReader(logFile.toPath())) {
114 char[] buf = new char[1000];
115 int len = reader.read(buf, 0, 1000);
116 String oldError = new String(buf, 0, len);
117
118 throw new JGitInternalException(MessageFormat.format(
119 JGitText.get().gcLogExists, oldError, logFile));
120 }
121 }
122 return true;
123 }
124 } catch (NoSuchFileException e) {
125
126 } catch (IOException | ParseException e) {
127 throw new JGitInternalException(e.getMessage(), e);
128 }
129 return false;
130 }
131
132
133
134
135
136
137
138
139 boolean lock(boolean background) {
140 try {
141 if (!lock.lock()) {
142 return false;
143 }
144 } catch (IOException e) {
145 throw new JGitInternalException(e.getMessage(), e);
146 }
147 if (autoGcBlockedByOldLockFile(background)) {
148 lock.unlock();
149 return false;
150 }
151 return true;
152 }
153
154
155
156
157 void unlock() {
158 lock.unlock();
159 }
160
161
162
163
164
165
166
167 boolean commit() {
168 if (nonEmpty) {
169 return lock.commit();
170 } else {
171 logFile.delete();
172 lock.unlock();
173 return true;
174 }
175 }
176
177
178
179
180
181
182
183
184
185 void write(String content) throws IOException {
186 if (content.length() > 0) {
187 nonEmpty = true;
188 }
189 lock.write(content.getBytes(UTF_8));
190 }
191 }