You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GcLog.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2017 Two Sigma Open Source
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.file;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.nio.file.Files;
  48. import java.nio.file.NoSuchFileException;
  49. import java.nio.file.attribute.FileTime;
  50. import java.text.ParseException;
  51. import java.time.Instant;
  52. import org.eclipse.jgit.api.errors.JGitInternalException;
  53. import org.eclipse.jgit.lib.ConfigConstants;
  54. import org.eclipse.jgit.util.FileUtils;
  55. import org.eclipse.jgit.util.GitDateParser;
  56. import org.eclipse.jgit.util.SystemReader;
  57. /**
  58. * This class manages the gc.log file for a {@link FileRepository}.
  59. */
  60. class GcLog {
  61. private final FileRepository repo;
  62. private final File logFile;
  63. private final LockFile lock;
  64. private Instant gcLogExpire;
  65. private static final String LOG_EXPIRY_DEFAULT = "1.day.ago"; //$NON-NLS-1$
  66. private boolean nonEmpty = false;
  67. /**
  68. * Construct a GcLog object for a {@link FileRepository}
  69. *
  70. * @param repo
  71. * the repository
  72. */
  73. GcLog(FileRepository repo) {
  74. this.repo = repo;
  75. logFile = new File(repo.getDirectory(), "gc.log"); //$NON-NLS-1$
  76. lock = new LockFile(logFile);
  77. }
  78. private Instant getLogExpiry() throws ParseException {
  79. if (gcLogExpire == null) {
  80. String logExpiryStr = repo.getConfig().getString(
  81. ConfigConstants.CONFIG_GC_SECTION, null,
  82. ConfigConstants.CONFIG_KEY_LOGEXPIRY);
  83. if (logExpiryStr == null) {
  84. logExpiryStr = LOG_EXPIRY_DEFAULT;
  85. }
  86. gcLogExpire = GitDateParser.parse(logExpiryStr, null,
  87. SystemReader.getInstance().getLocale()).toInstant();
  88. }
  89. return gcLogExpire;
  90. }
  91. private boolean autoGcBlockedByOldLockFile() {
  92. try {
  93. FileTime lastModified = Files.getLastModifiedTime(FileUtils.toPath(logFile));
  94. if (lastModified.toInstant().compareTo(getLogExpiry()) > 0) {
  95. // There is an existing log file, which is too recent to ignore
  96. return true;
  97. }
  98. } catch (NoSuchFileException e) {
  99. // No existing log file, OK.
  100. } catch (IOException | ParseException e) {
  101. throw new JGitInternalException(e.getMessage(), e);
  102. }
  103. return false;
  104. }
  105. /**
  106. * Lock the GC log file for updates
  107. *
  108. * @return {@code true} if we hold the lock
  109. */
  110. boolean lock() {
  111. try {
  112. if (!lock.lock()) {
  113. return false;
  114. }
  115. } catch (IOException e) {
  116. throw new JGitInternalException(e.getMessage(), e);
  117. }
  118. if (autoGcBlockedByOldLockFile()) {
  119. lock.unlock();
  120. return false;
  121. }
  122. return true;
  123. }
  124. /**
  125. * Unlock (roll back) the GC log lock
  126. */
  127. void unlock() {
  128. lock.unlock();
  129. }
  130. /**
  131. * Commit changes to the gc log, if there have been any writes. Otherwise,
  132. * just unlock and delete the existing file (if any)
  133. *
  134. * @return true if committing (or unlocking/deleting) succeeds.
  135. */
  136. boolean commit() {
  137. if (nonEmpty) {
  138. return lock.commit();
  139. } else {
  140. logFile.delete();
  141. lock.unlock();
  142. return true;
  143. }
  144. }
  145. /**
  146. * Write to the pending gc log. Content will be committed upon a call to
  147. * commit()
  148. *
  149. * @param content
  150. * The content to write
  151. * @throws IOException
  152. */
  153. void write(String content) throws IOException {
  154. if (content.length() > 0) {
  155. nonEmpty = true;
  156. }
  157. lock.write(content.getBytes(UTF_8));
  158. }
  159. }