Skip to content

Commit

Permalink
Merge pull request #6 from vibe-d/filecache_fix
Browse files Browse the repository at this point in the history
Make destruction of CachedFileStream.CTX deterministic
  • Loading branch information
l-kramer authored Nov 1, 2024
2 parents ccdc9b9 + cede6bc commit dd07842
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion run-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fi

dub test $DUB_FLAGS

if [ "$OS" == "macOS-latest" ]; then
if [ "$OS" == "macOS-13" ]; then
dub test :tls -c openssl-1.1 $DUB_FLAGS
DUB_FLAGS="$DUB_FLAGS --override-config vibe-stream:tls/openssl-1.1"
else
Expand Down
15 changes: 15 additions & 0 deletions source/vibe/stream/cached.d
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct CachedFileStream(InputStream)
enum outputStreamVersion = 2;

private static struct CTX {
int refCount;
InputStream source;
FileStream cachedFile;
ulong readPtr;
Expand All @@ -72,6 +73,7 @@ struct CachedFileStream(InputStream)
private this(InputStream source, bool writable, NativePath cached_file_path)
{
m_ctx = new CTX;
m_ctx.refCount = 1;
m_ctx.source = source;
m_ctx.canWrite = writable;
m_ctx.size = source.leastSize;
Expand All @@ -82,6 +84,19 @@ struct CachedFileStream(InputStream)
} else m_ctx.cachedFile = openFile(cached_file_path, FileMode.createTrunc);
}

this(this)
{
if (m_ctx) m_ctx.refCount++;
}

~this()
{
if (m_ctx) {
if (--m_ctx.refCount == 0)
close();
}
}

@property int fd() const nothrow { return m_ctx.cachedFile.fd; }
@property NativePath path() const nothrow { return m_ctx.cachedFile.path; }
@property bool isOpen() const nothrow { return m_ctx && m_ctx.cachedFile.isOpen; }
Expand Down

0 comments on commit dd07842

Please sign in to comment.