Skip to content

Commit

Permalink
Release buffers that have become too large.
Browse files Browse the repository at this point in the history
  • Loading branch information
miyabe committed Mar 17, 2023
1 parent 97fb9e5 commit 16c040e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
14 changes: 14 additions & 0 deletions EncFSy_lib/EncFSFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ using namespace std;
static AutoSeededX917RNG<CryptoPP::AES> random;

namespace EncFS {
int64_t EncFSFile::counter = 0;

EncFSGetFileIVResult EncFSFile::getFileIV(const LPCWSTR FileName, int64_t *fileIv, bool create) {
if (this->fileIvAvailable) {
*fileIv = this->fileIv;
Expand Down Expand Up @@ -116,6 +118,7 @@ namespace EncFS {
DWORD readLen;
this->blockBuffer.resize(blocksLength);
if (!ReadFile(this->handle, &this->blockBuffer[0], (DWORD)blocksLength, &readLen, NULL)) {
this->clearBlockBuffer();
return -1;
}

Expand All @@ -140,6 +143,7 @@ namespace EncFS {
blockNum++;
}
}
this->clearBlockBuffer();
}
//printf("readEnd %d\n", copiedLen);
return copiedLen;
Expand Down Expand Up @@ -305,10 +309,12 @@ namespace EncFS {
DWORD readLen;
this->blockBuffer.resize(blocksLength);
if (!ReadFile(this->handle, &this->blockBuffer[0], (DWORD)blocksLength, &readLen, NULL)) {
this->clearBlockBuffer();
return -1;
}
len = readLen;
if (i >= len) {
this->clearBlockBuffer();
return i;
}

Expand All @@ -329,6 +335,7 @@ namespace EncFS {
bufferPos++;
shift = 0;
}
this->clearBlockBuffer();
return i;
}

Expand Down Expand Up @@ -490,4 +497,11 @@ namespace EncFS {
//printf("changeFileIV C %d\n", fileIv);
return true;
}

void EncFSFile::clearBlockBuffer() {
if (this->blockBuffer.capacity() > encfs.getBlockSize()) {
this->blockBuffer.clear();
this->blockBuffer.shrink_to_fit();
}
}
}
7 changes: 6 additions & 1 deletion EncFSy_lib/EncFSFile.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#pragma once;
#include <dokan.h>
#include <winbase.h>

Expand Down Expand Up @@ -34,6 +34,8 @@ namespace EncFS
wstring_convert<codecvt_utf8_utf16<wchar_t>> strConv;

public:
static int64_t counter;

EncFSFile(HANDLE handle, bool canRead) {
if (!handle || handle == INVALID_HANDLE_VALUE) {
throw EncFSIllegalStateException();
Expand All @@ -43,11 +45,13 @@ namespace EncFS
this->fileIvAvailable = false;
this->fileIv = 0L;
this->lastBlockNum = -1;
++counter;
}

~EncFSFile() {
CloseHandle(this->handle);
this->handle = INVALID_HANDLE_VALUE;
--counter;
}

inline HANDLE getHandle() {
Expand All @@ -64,5 +68,6 @@ namespace EncFS
private:
EncFSGetFileIVResult getFileIV(const LPCWSTR FileName, int64_t *fileIv, bool create);
bool _setLength(const LPCWSTR FileName, const size_t fileSize, const size_t length);
void clearBlockBuffer();
};
}
5 changes: 3 additions & 2 deletions EncFSy_lib/EncFSy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,9 @@ EncFSCreateFile(LPCWSTR FileName, PDOKAN_IO_SECURITY_CONTEXT SecurityContext,
// Open succeed but we need to inform the driver
// that the dir open and not created by returning STATUS_OBJECT_NAME_COLLISION
if (creationDisposition == OPEN_ALWAYS &&
fileAttr != INVALID_FILE_ATTRIBUTES)
return STATUS_OBJECT_NAME_COLLISION;
fileAttr != INVALID_FILE_ATTRIBUTES) {
status = STATUS_OBJECT_NAME_COLLISION;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion installer/installer.nsi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
!define VERSION "1.0.1"
!define VERSION "1.0.2"

!include LogicLib.nsh
!include x64.nsh
Expand Down

0 comments on commit 16c040e

Please sign in to comment.