Repository navigation
Own shops by character and trade through the ledger #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2e8797c
wip: paused for handoff
ryanbarlow97 e89054b
Own shops by character and persist ledger trade intents
ryanbarlow97 eb522ae
fix: persist payment intent directory entries before posting
ryanbarlow97 b4a4be1
fix: quarantine an unreadable shop without closing every shop
ryanbarlow97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/net/tfminecraft/bartershops/DurableFiles.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package net.tfminecraft.bartershops; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.channels.FileChannel; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardCopyOption; | ||
| import java.nio.file.StandardOpenOption; | ||
|
|
||
| /** Publish whole, flushed files before money or inventory changes. */ | ||
| final class DurableFiles { | ||
| private DurableFiles() {} | ||
|
|
||
| static void write(Path target, String text) throws IOException { | ||
| write(target, text.getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
|
|
||
| static void write(Path target, byte[] bytes) throws IOException { | ||
| target = target.toAbsolutePath(); | ||
| Files.createDirectories(target.getParent()); | ||
| Path temp = Files.createTempFile(target.getParent(), ".pending-", ".tmp"); | ||
| try { | ||
| try (FileChannel channel = FileChannel.open(temp, StandardOpenOption.WRITE)) { | ||
| ByteBuffer buffer = ByteBuffer.wrap(bytes); | ||
| while (buffer.hasRemaining()) channel.write(buffer); | ||
| channel.force(true); | ||
| } | ||
| Files.move(temp, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); | ||
| // Parent entries also need flushing when an intent creates its first directory. | ||
| for (Path directory = target.getParent(); directory != null; directory = directory.getParent()) sync(directory); | ||
| } finally { | ||
| Files.deleteIfExists(temp); | ||
| } | ||
| } | ||
|
|
||
| static void delete(Path target) throws IOException { | ||
| Files.deleteIfExists(target); | ||
| sync(target.getParent()); | ||
| } | ||
|
|
||
| private static void sync(Path directory) throws IOException { | ||
| try (FileChannel channel = FileChannel.open(directory, StandardOpenOption.READ)) { | ||
| channel.force(true); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.