PackFile.java

  1. /*
  2.  * Copyright (c) 2021 Qualcomm Innovation Center, Inc.
  3.  * and other copyright owners as documented in the project's IP log.
  4.  *
  5.  * This program and the accompanying materials are made available under the
  6.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  7.  * https://www.eclipse.org/org/documents/edl-v10.php.
  8.  *
  9.  * SPDX-License-Identifier: BSD-3-Clause
  10.  */

  11. package org.eclipse.jgit.internal.storage.file;

  12. import java.io.File;
  13. import java.text.MessageFormat;

  14. import org.eclipse.jgit.internal.JGitText;
  15. import org.eclipse.jgit.internal.storage.pack.PackExt;
  16. import org.eclipse.jgit.lib.ObjectId;

  17. /**
  18.  * A pack file (or pack related) File.
  19.  *
  20.  * Example: "pack-0123456789012345678901234567890123456789.idx"
  21.  */
  22. public class PackFile extends File {
  23.     private static final long serialVersionUID = 1L;

  24.     private static final String PREFIX = "pack-"; //$NON-NLS-1$

  25.     private final String base; // PREFIX + id i.e.
  26.                                 // pack-0123456789012345678901234567890123456789

  27.     private final String id; // i.e. 0123456789012345678901234567890123456789

  28.     private final boolean hasOldPrefix;

  29.     private final PackExt packExt;

  30.     private static String createName(String id, PackExt extension) {
  31.         return PREFIX + id + '.' + extension.getExtension();
  32.     }

  33.     /**
  34.      * Create a PackFile for a pack or related file.
  35.      *
  36.      * @param file
  37.      *            File pointing to the location of the file.
  38.      */
  39.     public PackFile(File file) {
  40.         this(file.getParentFile(), file.getName());
  41.     }

  42.     /**
  43.      * Create a PackFile for a pack or related file.
  44.      *
  45.      * @param directory
  46.      *            Directory to create the PackFile in.
  47.      * @param id
  48.      *            the {@link ObjectId} for this pack
  49.      * @param ext
  50.      *            the <code>packExt</code> of the name.
  51.      */
  52.     public PackFile(File directory, ObjectId id, PackExt ext) {
  53.         this(directory, id.name(), ext);
  54.     }

  55.     /**
  56.      * Create a PackFile for a pack or related file.
  57.      *
  58.      * @param directory
  59.      *            Directory to create the PackFile in.
  60.      * @param id
  61.      *            the <code>id</code> (40 Hex char) section of the pack name.
  62.      * @param ext
  63.      *            the <code>packExt</code> of the name.
  64.      */
  65.     public PackFile(File directory, String id, PackExt ext) {
  66.         this(directory, createName(id, ext));
  67.     }

  68.     /**
  69.      * Create a PackFile for a pack or related file.
  70.      *
  71.      * @param directory
  72.      *            Directory to create the PackFile in.
  73.      * @param name
  74.      *            Filename (last path section) of the PackFile
  75.      */
  76.     public PackFile(File directory, String name) {
  77.         super(directory, name);
  78.         int dot = name.lastIndexOf('.');

  79.         if (dot < 0) {
  80.             base = name;
  81.             hasOldPrefix = false;
  82.             packExt = null;
  83.         } else {
  84.             base = name.substring(0, dot);
  85.             String tail = name.substring(dot + 1); // ["old-"] + extension
  86.             packExt = getPackExt(tail);
  87.             String old = tail.substring(0,
  88.                     tail.length() - getExtension().length());
  89.             hasOldPrefix = old.equals(getExtPrefix(true));
  90.         }

  91.         id = base.startsWith(PREFIX) ? base.substring(PREFIX.length()) : base;
  92.     }

  93.     /**
  94.      * Getter for the field <code>id</code>.
  95.      *
  96.      * @return the <code>id</code> (40 Hex char) section of the name.
  97.      */
  98.     public String getId() {
  99.         return id;
  100.     }

  101.     /**
  102.      * Getter for the field <code>packExt</code>.
  103.      *
  104.      * @return the <code>packExt</code> of the name.
  105.      */
  106.     public PackExt getPackExt() {
  107.         return packExt;
  108.     }

  109.     /**
  110.      * Create a new similar PackFile with the given extension instead.
  111.      *
  112.      * @param ext
  113.      *            PackExt the extension to use.
  114.      * @return a PackFile instance with specified extension
  115.      */
  116.     public PackFile create(PackExt ext) {
  117.         return new PackFile(getParentFile(), getName(ext));
  118.     }

  119.     /**
  120.      * Create a new similar PackFile in the given directory.
  121.      *
  122.      * @param directory
  123.      *            Directory to create the new PackFile in.
  124.      * @return a PackFile in the given directory
  125.      */
  126.     public PackFile createForDirectory(File directory) {
  127.         return new PackFile(directory, getName(false));
  128.     }

  129.     /**
  130.      * Create a new similar preserved PackFile in the given directory.
  131.      *
  132.      * @param directory
  133.      *            Directory to create the new PackFile in.
  134.      * @return a PackFile in the given directory with "old-" prefixing the
  135.      *         extension
  136.      */
  137.     public PackFile createPreservedForDirectory(File directory) {
  138.         return new PackFile(directory, getName(true));
  139.     }

  140.     private String getName(PackExt ext) {
  141.         return base + '.' + getExtPrefix(hasOldPrefix) + ext.getExtension();
  142.     }

  143.     private String getName(boolean isPreserved) {
  144.         return base + '.' + getExtPrefix(isPreserved) + getExtension();
  145.     }

  146.     private String getExtension() {
  147.         return packExt == null ? "" : packExt.getExtension(); //$NON-NLS-1$
  148.     }

  149.     private static String getExtPrefix(boolean isPreserved) {
  150.         return isPreserved ? "old-" : ""; //$NON-NLS-1$ //$NON-NLS-2$
  151.     }

  152.     private static PackExt getPackExt(String endsWithExtension) {
  153.         for (PackExt ext : PackExt.values()) {
  154.             if (endsWithExtension.endsWith(ext.getExtension())) {
  155.                 return ext;
  156.             }
  157.         }
  158.         throw new IllegalArgumentException(MessageFormat.format(
  159.                 JGitText.get().unrecognizedPackExtension, endsWithExtension));
  160.     }
  161. }