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 org.eclipse.jgit.lib.Constants.CHARSET;
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.FileUtils;
62 import org.eclipse.jgit.util.GitDateParser;
63 import org.eclipse.jgit.util.SystemReader;
64
65
66
67
68 class GcLog {
69 private final FileRepository repo;
70
71 private final File logFile;
72
73 private final LockFile lock;
74
75 private Instant gcLogExpire;
76
77 private static final String LOG_EXPIRY_DEFAULT = "1.day.ago";
78
79 private boolean nonEmpty = false;
80
81
82
83
84
85
86
87 GcLog(FileRepository repo) {
88 this.repo = repo;
89 logFile = new File(repo.getDirectory(), "gc.log");
90 lock = new LockFile(logFile);
91 }
92
93 private Instant getLogExpiry() throws ParseException {
94 if (gcLogExpire == null) {
95 String logExpiryStr = repo.getConfig().getString(
96 ConfigConstants.CONFIG_GC_SECTION, null,
97 ConfigConstants.CONFIG_KEY_LOGEXPIRY);
98 if (logExpiryStr == null) {
99 logExpiryStr = LOG_EXPIRY_DEFAULT;
100 }
101 gcLogExpire = GitDateParser.parse(logExpiryStr, null,
102 SystemReader.getInstance().getLocale()).toInstant();
103 }
104 return gcLogExpire;
105 }
106
107 private boolean autoGcBlockedByOldLockFile(boolean background) {
108 try {
109 FileTime lastModified = Files.getLastModifiedTime(FileUtils.toPath(logFile));
110 if (lastModified.toInstant().compareTo(getLogExpiry()) > 0) {
111
112 if (!background) {
113 try (BufferedReader reader = Files
114 .newBufferedReader(FileUtils.toPath(logFile))) {
115 char[] buf = new char[1000];
116 int len = reader.read(buf, 0, 1000);
117 String oldError = new String(buf, 0, len);
118
119 throw new JGitInternalException(MessageFormat.format(
120 JGitText.get().gcLogExists, oldError, logFile));
121 }
122 }
123 return true;
124 }
125 } catch (NoSuchFileException e) {
126
127 } catch (IOException | ParseException e) {
128 throw new JGitInternalException(e.getMessage(), e);
129 }
130 return false;
131 }
132
133
134
135
136
137
138
139
140 boolean lock(boolean background) {
141 try {
142 if (!lock.lock()) {
143 return false;
144 }
145 } catch (IOException e) {
146 throw new JGitInternalException(e.getMessage(), e);
147 }
148 if (autoGcBlockedByOldLockFile(background)) {
149 lock.unlock();
150 return false;
151 }
152 return true;
153 }
154
155
156
157
158 void unlock() {
159 lock.unlock();
160 }
161
162
163
164
165
166
167
168 boolean commit() {
169 if (nonEmpty) {
170 return lock.commit();
171 } else {
172 logFile.delete();
173 lock.unlock();
174 return true;
175 }
176 }
177
178
179
180
181
182
183
184
185
186 void write(String content) throws IOException {
187 if (content.length() > 0) {
188 nonEmpty = true;
189 }
190 lock.write(content.getBytes(CHARSET));
191 }
192 }