FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added mutex around the code that utilizes nodegit. Prevents segmentat… · papandreou/node-gitfakefs@358eac4 · GitHub

Repository navigation

Commit 358eac4

Browse files
committed
Added mutex around the code that utilizes nodegit. Prevents segmentation faults.
I hope this will be temporary.
1 parent 613d0f1 commit 358eac4

1 file changed

Lines changed: 75 additions & 44 deletions

File tree

‎lib/GitWrappedFs.js‎

Lines changed: 75 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ var GitWrappedFs = module.exports = function GitWrappedFs(fs) {
8787
}
8888
});
8989

90+
var waitingQueue = [],
91+
isLocked = false;
92+
93+
function acquireLock(cb) {
94+
waitingQueue.push(cb);
95+
proceed();
96+
}
97+
98+
function proceed() {
99+
while (!isLocked && waitingQueue.length > 0) {
100+
isLocked = true;
101+
var nextCb = waitingQueue.shift(),
102+
callbackCalled = false;
103+
nextCb(function () {
104+
isLocked = false;
105+
proceed();
106+
});
107+
}
108+
}
109+
90110
Object.keys(fs).forEach(function (fsMethodName) {
91111
var fsPropertyValue = fs[fsMethodName];
92112
if (typeof fsPropertyValue === 'function') {
@@ -134,55 +154,66 @@ var GitWrappedFs = module.exports = function GitWrappedFs(fs) {
134154
}));
135155
}
136156

137-
if (additionalFragments[additionalFragments.length - 1] === '') {
138-
additionalFragments.pop();
139-
}
140-
getRepo(pathToRepository, passError(cb, function (repo) {
141-
if (additionalFragments.length === 0) {
142-
if (fsMethodName === 'readdir') {
143-
cb(null, [].concat(virtualContentsDirectories));
144-
} else if (fsMethodName === 'stat' || fsMethodName === 'lstat') {
145-
cb(null, new FakeStats({isDirectory: true}));
146-
} else {
147-
cb(new Error('GitWrappedFs: ' + fsMethodName + ' not supported on the virtual contents directory'));
148-
}
149-
} else if (additionalFragments.length >= 1) {
150-
var objectType = additionalFragments.shift();
151-
if (objectType === 'index') {
152-
// No further levels
153-
return proxyToGitFakeFs({index: true, ref: 'HEAD'});
154-
}
155-
// objectType is 'branches', 'tags', or 'commits'
156-
var objectName = additionalFragments.shift(); // Might be undefined
157-
158-
if (virtualContentsDirectories.indexOf(objectType) === -1) {
159-
process.nextTick(function () {
160-
cb(fakeFsErrors.Enoent("Error: ENOENT, no such file or directory '" + path + "'"));
161-
});
162-
} else if (fsMethodName === 'stat' || fsMethodName === 'lstat') {
163-
if (objectName) {
164-
getNamesForObjectType(pathToRepository, objectType, passError(cb, function (namesForObjectType) {
165-
if (namesForObjectType.indexOf(objectName) === -1) {
166-
cb(fakeFsErrors.Enoent("Error: ENOENT, no such file or directory '" + path + "'"));
167-
} else {
168-
cb(null, new FakeStats({isDirectory: true}));
169-
}
170-
}));
157+
acquireLock(function (releaseLock) {
158+
var _cb = cb;
159+
cb = function () {
160+
_cb.apply(this, arguments);
161+
releaseLock();
162+
};
163+
if (typeof lastArgument === 'function') {
164+
args[args.length - 1] = cb;
165+
}
166+
167+
if (additionalFragments[additionalFragments.length - 1] === '') {
168+
additionalFragments.pop();
169+
}
170+
getRepo(pathToRepository, passError(cb, function (repo) {
171+
if (additionalFragments.length === 0) {
172+
if (fsMethodName === 'readdir') {
173+
cb(null, [].concat(virtualContentsDirectories));
174+
} else if (fsMethodName === 'stat' || fsMethodName === 'lstat') {
175+
cb(null, new FakeStats({isDirectory: true}));
171176
} else {
177+
cb(new Error('GitWrappedFs: ' + fsMethodName + ' not supported on the virtual contents directory'));
178+
}
179+
} else if (additionalFragments.length >= 1) {
180+
var objectType = additionalFragments.shift();
181+
if (objectType === 'index') {
182+
// No further levels
183+
return proxyToGitFakeFs({index: true, ref: 'HEAD'});
184+
}
185+
// objectType is 'branches', 'tags', or 'commits'
186+
var objectName = additionalFragments.shift(); // Might be undefined
187+
188+
if (virtualContentsDirectories.indexOf(objectType) === -1) {
172189
process.nextTick(function () {
173-
cb(null, new FakeStats({isDirectory: true}));
190+
cb(fakeFsErrors.Enoent("Error: ENOENT, no such file or directory '" + path + "'"));
174191
});
192+
} else if (fsMethodName === 'stat' || fsMethodName === 'lstat') {
193+
if (objectName) {
194+
getNamesForObjectType(pathToRepository, objectType, passError(cb, function (namesForObjectType) {
195+
if (namesForObjectType.indexOf(objectName) === -1) {
196+
cb(fakeFsErrors.Enoent("Error: ENOENT, no such file or directory '" + path + "'"));
197+
} else {
198+
cb(null, new FakeStats({isDirectory: true}));
199+
}
200+
}));
201+
} else {
202+
process.nextTick(function () {
203+
cb(null, new FakeStats({isDirectory: true}));
204+
});
205+
}
206+
} else if (!objectName && fsMethodName === 'readdir') {
207+
getNamesForObjectType(pathToRepository, objectType, cb);
208+
} else {
209+
getGitFakeFs(pathToRepository, {index: objectType === 'index', ref: objectName}, passError(cb, function (gitFakeFs) {
210+
var rootRelativePathInsideGitRepo = '/' + additionalFragments.join('/');
211+
gitFakeFs[fsMethodName].apply(gitFakeFs, [rootRelativePathInsideGitRepo].concat(args.slice(1)));
212+
}));
175213
}
176-
} else if (!objectName && fsMethodName === 'readdir') {
177-
getNamesForObjectType(pathToRepository, objectType, cb);
178-
} else {
179-
getGitFakeFs(pathToRepository, {index: objectType === 'index', ref: objectName}, passError(cb, function (gitFakeFs) {
180-
var rootRelativePathInsideGitRepo = '/' + additionalFragments.join('/');
181-
gitFakeFs[fsMethodName].apply(gitFakeFs, [rootRelativePathInsideGitRepo].concat(args.slice(1)));
182-
}));
183214
}
184-
}
185-
}));
215+
}));
216+
});
186217
} else {
187218
return proxyToWrappedFs();
188219
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL