|
| 1 | +package org.cryptomator.linux.sidebar; |
| 2 | + |
| 3 | +import org.cryptomator.integrations.sidebar.SidebarService; |
| 4 | +import org.cryptomator.integrations.sidebar.SidebarServiceException; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.StandardCopyOption; |
| 11 | +import java.nio.file.StandardOpenOption; |
| 12 | +import java.util.concurrent.locks.Lock; |
| 13 | +import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 14 | + |
| 15 | +public class NautilusSidebarService implements SidebarService { |
| 16 | + |
| 17 | + private static final int MAX_FILE_SIZE = 4096; |
| 18 | + private static final Path BOOKMARKS_FILE = Path.of(System.getProperty("user.home"), ".config/gtk-3.0/bookmarks"); |
| 19 | + private static final Path TMP_FILE = BOOKMARKS_FILE.resolveSibling("bookmarks.cryptomator.tmp"); |
| 20 | + private static final Lock BOOKMARKS_LOCK = new ReentrantReadWriteLock().writeLock(); |
| 21 | + |
| 22 | + @Override |
| 23 | + public SidebarEntry add(Path target, String displayName) throws SidebarServiceException { |
| 24 | + String entryLine = "file://" + target.toAbsolutePath() + " " + displayName; |
| 25 | + try { |
| 26 | + BOOKMARKS_LOCK.lock(); |
| 27 | + if (Files.size(BOOKMARKS_FILE) > MAX_FILE_SIZE) { |
| 28 | + throw new IOException("File %s exceeds size of %d bytes".formatted(BOOKMARKS_FILE, MAX_FILE_SIZE)); |
| 29 | + } |
| 30 | + //by reading all lines, we ensure that each line is terminated with EOL |
| 31 | + var entries = Files.readAllLines(BOOKMARKS_FILE, StandardCharsets.UTF_8); |
| 32 | + entries.add(entryLine); |
| 33 | + Files.write(TMP_FILE, entries, StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); |
| 34 | + Files.move(TMP_FILE, BOOKMARKS_FILE, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); |
| 35 | + return new NautilusSidebarEntry(entryLine); |
| 36 | + } catch (IOException e) { |
| 37 | + throw new SidebarServiceException("Adding entry to Nautilus bookmarks file failed.", e); |
| 38 | + } finally { |
| 39 | + BOOKMARKS_LOCK.unlock(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + static class NautilusSidebarEntry implements SidebarEntry { |
| 44 | + |
| 45 | + private final String line; |
| 46 | + private volatile boolean isRemoved = false; |
| 47 | + |
| 48 | + NautilusSidebarEntry(String line) { |
| 49 | + this.line = line; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void remove() throws SidebarServiceException { |
| 54 | + try { |
| 55 | + BOOKMARKS_LOCK.lock(); |
| 56 | + if (isRemoved) { |
| 57 | + return; |
| 58 | + } |
| 59 | + if (Files.size(BOOKMARKS_FILE) > MAX_FILE_SIZE) { |
| 60 | + throw new IOException("File %s exceeds size of %d bytes".formatted(BOOKMARKS_FILE, MAX_FILE_SIZE)); |
| 61 | + } |
| 62 | + var entries = Files.readAllLines(BOOKMARKS_FILE); |
| 63 | + if (entries.remove(line)) { |
| 64 | + Files.write(TMP_FILE, entries, StandardCharsets.UTF_8, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); |
| 65 | + Files.move(TMP_FILE, BOOKMARKS_FILE, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); |
| 66 | + } |
| 67 | + isRemoved = true; |
| 68 | + } catch (IOException e) { |
| 69 | + throw new SidebarServiceException("Removing entry from Nautilus bookmarks faile failed", e); |
| 70 | + } finally { |
| 71 | + BOOKMARKS_LOCK.unlock(); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments