1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-12 23:42:06 +01:00
This commit is contained in:
Paul Schaub 2022-01-21 00:11:17 +01:00
parent 7aa48f458b
commit a5d592a102
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 167 additions and 0 deletions

View file

@ -0,0 +1,75 @@
package org.pgpainless.key.storage;
import java.io.File;
public class CertDStore {
private final File baseDirectory;
private static final String STORE_NAME = "pgp.cert.d";
public CertDStore() {
this(getDefaultBaseDir());
}
public CertDStore(File baseDirectory) {
this.baseDirectory = baseDirectory;
}
public File fingerprintToPrefixDir(String fingerprint) {
String dirName = fingerprint.toLowerCase().substring(0, 2);
return new File(baseDirectory, dirName);
}
public String fingerprintToCertFileName(String fingerprint) {
String certFileName = fingerprint.toLowerCase().substring(2);
return certFileName;
}
public File fingerprintToCertFile(String fingerprint) {
File dir = fingerprintToPrefixDir(fingerprint);
File certFile = new File(dir, fingerprintToCertFileName(fingerprint));
return certFile;
}
public File getBaseDirectory() {
return baseDirectory;
}
private static File getDefaultBaseDir() {
// Check for environment variable
String baseDirFromEnv = System.getenv("PGP_CERT_D");
if (baseDirFromEnv != null) {
return new File(baseDirFromEnv);
}
// return OS-specific default dir
String osName = System.getProperty("os.name", "generic")
.toLowerCase();
return getDefaultBaseDirForOS(osName, File.separator);
}
public static File getDefaultBaseDirForOS(String osName, String separator) {
if (osName.contains("win")) {
String appData = System.getenv("APPDATA");
String roaming = appData + separator + "Roaming";
return new File(roaming, STORE_NAME);
}
if (osName.contains("nux")) {
String xdg_data_home = System.getenv("XDG_DATA_HOME");
String rootPath = xdg_data_home;
if (xdg_data_home == null) {
rootPath = System.getProperty("user.home") + separator + ".local" + separator + "share";
}
return new File(rootPath, STORE_NAME);
}
if (osName.contains("mac")) {
String home = System.getenv("HOME");
return new File(home + separator + "Library" + separator + "Application Support", STORE_NAME);
}
throw new IllegalArgumentException("Unknown OS " + osName);
}
}

View file

@ -0,0 +1,24 @@
package org.pgpainless.key.storage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
public interface CertificateStore {
Item get(String identifier) throws IOException;
Item getIfChanged(String identifier, String tag) throws IOException;
<
Item insert(InputStream data, MergeCallback merge) throws IOException;
Item tryInsert(InputStream data, MergeCallback merge) throws IOException;
Item insertSpecial(String specialName, InputStream data, MergeCallback merge) throws IOException;
Item tryInsertSpecial(String specialName, InputStream data, MergeCallback merge) throws IOException;
Iterator<Item> items();
Iterator<String> fingerprints();
}

View file

@ -0,0 +1,28 @@
package org.pgpainless.key.storage;
import java.io.InputStream;
public class Item {
private final String fingerprint;
private final String tag;
private final InputStream data;
public Item(String fingerprint, String tag, InputStream data) {
this.fingerprint = fingerprint;
this.tag = tag;
this.data = data;
}
public String getFingerprint() {
return fingerprint;
}
public String getTag() {
return tag;
}
public InputStream getData() {
return data;
}
}

View file

@ -0,0 +1,23 @@
package org.pgpainless.key.storage;
import javax.annotation.Nullable;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Merge a given certificate (update) with an existing certificate.
*/
public interface MergeCallback {
/**
* Merge the given certificate data with the existing certificate and return the result.
*
* If no existing certificate is found (i.e. existing is null), this method returns the binary representation of data.
*
* @param data input stream containing the certificate
* @param existing optional input stream containing an already existing copy of the certificate
* @return output stream containing the binary representation of the merged certificate
*/
OutputStream merge(InputStream data, @Nullable InputStream existing);
}

View file

@ -0,0 +1,17 @@
package org.pgpainless.key.storage;
import org.junit.jupiter.api.Test;
import java.io.File;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CertDStoreTest {
@Test
public void testGetDefaultBaseDir() {
CertDStore store = new CertDStore();
File baseDir = store.getBaseDirectory();
assertEquals("pgp.cert.d", baseDir.getName());
}
}