Skip to content

Commit d0c5b18

Browse files
committed
fix: updated transaction logic for _open
1 parent f1b5400 commit d0c5b18

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

src/EncryptedFS.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,22 +1993,24 @@ class EncryptedFS {
19931993
// It must continue the loop, by restarting the loop with an inherited transaction context
19941994
// This ensures that handling the existing inode is consistent
19951995
let raced = false;
1996-
let _tran: DBTransaction | undefined = tran;
1997-
let _tranRelease: ResourceRelease | undefined = undefined;
1996+
let tran_: DBTransaction | null = null;
1997+
let tranRelease_: ResourceRelease | null = null;
19981998
// Loop necessary due to following symlinks and optional `O_CREAT` file creation
19991999
while (true) {
20002000
if (navigated.target != null) {
20012001
// Handle existing target
2002-
if (_tran == null && _tranRelease == null) {
2002+
if (tran != null) {
2003+
tran_ = tran;
2004+
} else if (tran_ == null || tranRelease_ == null) {
20032005
const tranAcquire = this.iNodeMgr.transaction(navigated.target);
2004-
[_tranRelease, _tran] = (await tranAcquire()) as [
2006+
[tranRelease_, tran_] = (await tranAcquire()) as [
20052007
ResourceRelease,
20062008
DBTransaction,
20072009
];
20082010
}
20092011
let e: Error | undefined;
20102012
try {
2011-
const target = await this.iNodeMgr.get(navigated.target, _tran);
2013+
const target = await this.iNodeMgr.get(navigated.target, tran_);
20122014
if (target == null) {
20132015
// Try to find the target again
20142016
navigated = await this.navigate(path, false);
@@ -2034,7 +2036,7 @@ class EncryptedFS {
20342036
path,
20352037
// Only preserve the transaction context if it was inherited
20362038
// from a coalesced call, as it would already have be for `navigated.dir`
2037-
raced ? _tran : undefined,
2039+
raced ? tran_ : undefined,
20382040
);
20392041
// Restart the opening procedure with the new target
20402042
continue;
@@ -2074,27 +2076,27 @@ class EncryptedFS {
20742076
flags & constants.O_TRUNC &&
20752077
flags & (constants.O_WRONLY | constants.O_RDWR)
20762078
) {
2077-
await this.iNodeMgr.fileClearData(navigated.target, _tran);
2079+
await this.iNodeMgr.fileClearData(navigated.target, tran_);
20782080
await this.iNodeMgr.fileSetBlocks(
20792081
navigated.target,
20802082
Buffer.alloc(0),
20812083
this.blockSize,
20822084
undefined,
2083-
_tran,
2085+
tran_,
20842086
);
20852087
}
20862088
// Terminates loop, creates file descriptor
2087-
return await createFd(flags, navigated.target, _tran!);
2089+
return await createFd(flags, navigated.target, tran_!);
20882090
}
20892091
} catch (e_) {
20902092
e = e_;
20912093
throw e_;
20922094
} finally {
2093-
if (_tranRelease != null) {
2094-
await _tranRelease(e);
2095+
if (tranRelease_ != null) {
2096+
await tranRelease_(e);
20952097
// Clear the transaction variables
2096-
_tran = undefined;
2097-
_tranRelease = undefined;
2098+
tran_ = null;
2099+
tranRelease_ = null;
20982100
}
20992101
}
21002102
} else {
@@ -2112,17 +2114,19 @@ class EncryptedFS {
21122114
ResourceRelease,
21132115
INodeIndex,
21142116
];
2115-
if (_tran == null && _tranRelease == null) {
2117+
if (tran != null) {
2118+
tran_ = tran;
2119+
} else if (tran_ == null || tranRelease_ == null) {
21162120
const tranAcquire = this.iNodeMgr.transaction(ino, navigated.dir);
2117-
[_tranRelease, _tran] = (await tranAcquire()) as [
2121+
[tranRelease_, tran_] = (await tranAcquire()) as [
21182122
ResourceRelease,
21192123
DBTransaction,
21202124
];
21212125
}
21222126
// INode may be created while waiting for lock
21232127
// Transaction is maintained and not released
21242128
// This is to ensure that the already created locks are held
2125-
if ((await this.iNodeMgr.get(ino, _tran)) != null) {
2129+
if ((await this.iNodeMgr.get(ino, tran_)) != null) {
21262130
navigated.target = ino;
21272131
await inoRelease();
21282132
raced = true;
@@ -2132,7 +2136,7 @@ class EncryptedFS {
21322136
try {
21332137
const navigatedDirStat = await this.iNodeMgr.statGet(
21342138
navigated.dir,
2135-
_tran,
2139+
tran_,
21362140
);
21372141
// Cannot create if the current directory has been unlinked from its parent directory
21382142
if (navigatedDirStat.nlink < 2) {
@@ -2158,26 +2162,26 @@ class EncryptedFS {
21582162
},
21592163
this.blockSize,
21602164
undefined,
2161-
_tran,
2165+
tran_,
21622166
);
21632167
await this.iNodeMgr.dirSetEntry(
21642168
navigated.dir,
21652169
navigated.name,
21662170
ino!,
2167-
_tran,
2171+
tran_,
21682172
);
21692173
// Terminates loop, creates file descriptor
2170-
return await createFd(flags, ino!, _tran!);
2174+
return await createFd(flags, ino!, tran_!);
21712175
} catch (e_) {
21722176
e = e_;
21732177
throw e_;
21742178
} finally {
21752179
await inoRelease(e);
2176-
if (_tranRelease != null) {
2177-
await _tranRelease(e);
2180+
if (tranRelease_ != null) {
2181+
await tranRelease_(e);
21782182
// Clear the transaction variables
2179-
_tran = undefined;
2180-
_tranRelease = undefined;
2183+
tran_ = null;
2184+
tranRelease_ = null;
21812185
}
21822186
}
21832187
}

0 commit comments

Comments
 (0)