View Javadoc
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  
12  package org.eclipse.jgit.internal.storage.file;
13  
14  import java.io.File;
15  import java.text.MessageFormat;
16  
17  import org.eclipse.jgit.internal.JGitText;
18  import org.eclipse.jgit.internal.storage.pack.PackExt;
19  import org.eclipse.jgit.lib.ObjectId;
20  
21  /**
22   * A pack file (or pack related) File.
23   *
24   * Example: "pack-0123456789012345678901234567890123456789.idx"
25   */
26  public class PackFile extends File {
27  	private static final long serialVersionUID = 1L;
28  
29  	private static final String PREFIX = "pack-"; //$NON-NLS-1$
30  
31  	private final String base; // PREFIX + id i.e.
32  								// pack-0123456789012345678901234567890123456789
33  
34  	private final String id; // i.e. 0123456789012345678901234567890123456789
35  
36  	private final boolean hasOldPrefix;
37  
38  	private final PackExt packExt;
39  
40  	private static String createName(String id, PackExt extension) {
41  		return PREFIX + id + '.' + extension.getExtension();
42  	}
43  
44  	/**
45  	 * Create a PackFile for a pack or related file.
46  	 *
47  	 * @param file
48  	 *            File pointing to the location of the file.
49  	 */
50  	public PackFile(File file) {
51  		this(file.getParentFile(), file.getName());
52  	}
53  
54  	/**
55  	 * Create a PackFile for a pack or related file.
56  	 *
57  	 * @param directory
58  	 *            Directory to create the PackFile in.
59  	 * @param id
60  	 *            the {@link ObjectId} for this pack
61  	 * @param ext
62  	 *            the <code>packExt</code> of the name.
63  	 */
64  	public PackFile(File directory, ObjectId id, PackExt ext) {
65  		this(directory, id.name(), ext);
66  	}
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 id
74  	 *            the <code>id</code> (40 Hex char) section of the pack name.
75  	 * @param ext
76  	 *            the <code>packExt</code> of the name.
77  	 */
78  	public PackFile(File directory, String id, PackExt ext) {
79  		this(directory, createName(id, ext));
80  	}
81  
82  	/**
83  	 * Create a PackFile for a pack or related file.
84  	 *
85  	 * @param directory
86  	 *            Directory to create the PackFile in.
87  	 * @param name
88  	 *            Filename (last path section) of the PackFile
89  	 */
90  	public PackFile(File directory, String name) {
91  		super(directory, name);
92  		int dot = name.lastIndexOf('.');
93  
94  		if (dot < 0) {
95  			base = name;
96  			hasOldPrefix = false;
97  			packExt = null;
98  		} else {
99  			base = name.substring(0, dot);
100 			String tail = name.substring(dot + 1); // ["old-"] + extension
101 			packExt = getPackExt(tail);
102 			String old = tail.substring(0,
103 					tail.length() - getExtension().length());
104 			hasOldPrefix = old.equals(getExtPrefix(true));
105 		}
106 
107 		id = base.startsWith(PREFIX) ? base.substring(PREFIX.length()) : base;
108 	}
109 
110 	/**
111 	 * Getter for the field <code>id</code>.
112 	 *
113 	 * @return the <code>id</code> (40 Hex char) section of the name.
114 	 */
115 	public String getId() {
116 		return id;
117 	}
118 
119 	/**
120 	 * Getter for the field <code>packExt</code>.
121 	 *
122 	 * @return the <code>packExt</code> of the name.
123 	 */
124 	public PackExt getPackExt() {
125 		return packExt;
126 	}
127 
128 	/**
129 	 * Create a new similar PackFile with the given extension instead.
130 	 *
131 	 * @param ext
132 	 *            PackExt the extension to use.
133 	 * @return a PackFile instance with specified extension
134 	 */
135 	public PackFile create(PackExt ext) {
136 		return new PackFile(getParentFile(), getName(ext));
137 	}
138 
139 	/**
140 	 * Create a new similar PackFile in the given directory.
141 	 *
142 	 * @param directory
143 	 *            Directory to create the new PackFile in.
144 	 * @return a PackFile in the given directory
145 	 */
146 	public PackFile createForDirectory(File directory) {
147 		return new PackFile(directory, getName(false));
148 	}
149 
150 	/**
151 	 * Create a new similar preserved PackFile in the given directory.
152 	 *
153 	 * @param directory
154 	 *            Directory to create the new PackFile in.
155 	 * @return a PackFile in the given directory with "old-" prefixing the
156 	 *         extension
157 	 */
158 	public PackFile createPreservedForDirectory(File directory) {
159 		return new PackFile(directory, getName(true));
160 	}
161 
162 	private String getName(PackExt ext) {
163 		return base + '.' + getExtPrefix(hasOldPrefix) + ext.getExtension();
164 	}
165 
166 	private String getName(boolean isPreserved) {
167 		return base + '.' + getExtPrefix(isPreserved) + getExtension();
168 	}
169 
170 	private String getExtension() {
171 		return packExt == null ? "" : packExt.getExtension(); //$NON-NLS-1$
172 	}
173 
174 	private static String getExtPrefix(boolean isPreserved) {
175 		return isPreserved ? "old-" : ""; //$NON-NLS-1$ //$NON-NLS-2$
176 	}
177 
178 	private static PackExt getPackExt(String endsWithExtension) {
179 		for (PackExt ext : PackExt.values()) {
180 			if (endsWithExtension.endsWith(ext.getExtension())) {
181 				return ext;
182 			}
183 		}
184 		throw new IllegalArgumentException(MessageFormat.format(
185 				JGitText.get().unrecognizedPackExtension, endsWithExtension));
186 	}
187 }