filesystem.js 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997
  1. // -------------------------------------------------
  2. // ----------------- FILESYSTEM---------------------
  3. // -------------------------------------------------
  4. // Implementation of a unix filesystem in memory.
  5. "use strict";
  6. var S_IRWXUGO = 0x1FF;
  7. var S_IFMT = 0xF000;
  8. var S_IFSOCK = 0xC000;
  9. var S_IFLNK = 0xA000;
  10. var S_IFREG = 0x8000;
  11. var S_IFBLK = 0x6000;
  12. var S_IFDIR = 0x4000;
  13. var S_IFCHR = 0x2000;
  14. //var S_IFIFO 0010000
  15. //var S_ISUID 0004000
  16. //var S_ISGID 0002000
  17. //var S_ISVTX 0001000
  18. var O_RDONLY = 0x0000; // open for reading only
  19. var O_WRONLY = 0x0001; // open for writing only
  20. var O_RDWR = 0x0002; // open for reading and writing
  21. var O_ACCMODE = 0x0003; // mask for above modes
  22. var STATUS_INVALID = -0x1;
  23. var STATUS_OK = 0x0;
  24. var STATUS_ON_STORAGE = 0x2;
  25. var STATUS_UNLINKED = 0x4;
  26. var STATUS_FORWARDING = 0x5;
  27. /** @const */ var JSONFS_VERSION = 3;
  28. /** @const */ var JSONFS_IDX_NAME = 0;
  29. /** @const */ var JSONFS_IDX_SIZE = 1;
  30. /** @const */ var JSONFS_IDX_MTIME = 2;
  31. /** @const */ var JSONFS_IDX_MODE = 3;
  32. /** @const */ var JSONFS_IDX_UID = 4;
  33. /** @const */ var JSONFS_IDX_GID = 5;
  34. /** @const */ var JSONFS_IDX_TARGET = 6;
  35. /** @const */ var JSONFS_IDX_SHA256 = 6;
  36. /**
  37. * @constructor
  38. * @param {!FileStorageInterface} storage
  39. * @param {{ last_qidnumber: number }=} qidcounter Another fs's qidcounter to synchronise with.
  40. */
  41. function FS(storage, qidcounter) {
  42. /** @type {Array.<!Inode>} */
  43. this.inodes = [];
  44. this.events = [];
  45. this.storage = storage;
  46. this.qidcounter = qidcounter || { last_qidnumber: 0 };
  47. //this.tar = new TAR(this);
  48. this.inodedata = {};
  49. this.total_size = 256 * 1024 * 1024 * 1024;
  50. this.used_size = 0;
  51. /** @type {!Array<!FSMountInfo>} */
  52. this.mounts = [];
  53. //RegisterMessage("LoadFilesystem", this.LoadFilesystem.bind(this) );
  54. //RegisterMessage("MergeFile", this.MergeFile.bind(this) );
  55. //RegisterMessage("tar",
  56. // function(data) {
  57. // SendToMaster("tar", this.tar.Pack(data));
  58. // }.bind(this)
  59. //);
  60. //RegisterMessage("sync",
  61. // function(data) {
  62. // SendToMaster("sync", this.tar.Pack(data));
  63. // }.bind(this)
  64. //);
  65. // root entry
  66. this.CreateDirectory("", -1);
  67. }
  68. FS.prototype.get_state = function()
  69. {
  70. let state = [];
  71. state[0] = this.inodes;
  72. state[1] = this.qidcounter.last_qidnumber;
  73. state[2] = [];
  74. for(const [id, data] of Object.entries(this.inodedata))
  75. {
  76. if((this.inodes[id].mode & S_IFDIR) === 0)
  77. {
  78. state[2].push([id, data]);
  79. }
  80. }
  81. state[3] = this.total_size;
  82. state[4] = this.used_size;
  83. state = state.concat(this.mounts);
  84. return state;
  85. };
  86. FS.prototype.set_state = function(state)
  87. {
  88. this.inodes = state[0].map(state => { const inode = new Inode(0); inode.set_state(state); return inode; });
  89. this.qidcounter.last_qidnumber = state[1];
  90. this.inodedata = {};
  91. for(let [key, value] of state[2])
  92. {
  93. if(value.buffer.byteLength !== value.byteLength)
  94. {
  95. // make a copy if we didn't get one
  96. value = value.slice();
  97. }
  98. this.inodedata[key] = value;
  99. }
  100. this.total_size = state[3];
  101. this.used_size = state[4];
  102. this.mounts = state.slice(5);
  103. };
  104. // -----------------------------------------------------
  105. FS.prototype.AddEvent = function(id, OnEvent) {
  106. var inode = this.inodes[id];
  107. if(inode.status === STATUS_OK || inode.status === STATUS_ON_STORAGE) {
  108. OnEvent();
  109. }
  110. else if(this.is_forwarder(inode))
  111. {
  112. this.follow_fs(inode).AddEvent(inode.foreign_id, OnEvent);
  113. }
  114. else
  115. {
  116. this.events.push({id: id, OnEvent: OnEvent});
  117. }
  118. };
  119. FS.prototype.HandleEvent = function(id) {
  120. const inode = this.inodes[id];
  121. if(this.is_forwarder(inode))
  122. {
  123. this.follow_fs(inode).HandleEvent(inode.foreign_id);
  124. }
  125. //message.Debug("number of events: " + this.events.length);
  126. var newevents = [];
  127. for(var i=0; i<this.events.length; i++) {
  128. if(this.events[i].id === id) {
  129. this.events[i].OnEvent();
  130. } else {
  131. newevents.push(this.events[i]);
  132. }
  133. }
  134. this.events = newevents;
  135. };
  136. FS.prototype.load_from_json = function(fs)
  137. {
  138. dbg_assert(fs, "Invalid fs passed to load_from_json");
  139. if(fs["version"] !== JSONFS_VERSION)
  140. {
  141. throw "The filesystem JSON format has changed. " +
  142. "Please update your fs2json (https://github.com/copy/fs2json) and recreate the filesystem JSON.";
  143. }
  144. var fsroot = fs["fsroot"];
  145. this.used_size = fs["size"];
  146. for(var i = 0; i < fsroot.length; i++) {
  147. this.LoadRecursive(fsroot[i], 0);
  148. }
  149. //if(DEBUG)
  150. //{
  151. // this.Check();
  152. //}
  153. };
  154. FS.prototype.LoadRecursive = function(data, parentid)
  155. {
  156. var inode = this.CreateInode();
  157. const name = data[JSONFS_IDX_NAME];
  158. inode.size = data[JSONFS_IDX_SIZE];
  159. inode.mtime = data[JSONFS_IDX_MTIME];
  160. inode.ctime = inode.mtime;
  161. inode.atime = inode.mtime;
  162. inode.mode = data[JSONFS_IDX_MODE];
  163. inode.uid = data[JSONFS_IDX_UID];
  164. inode.gid = data[JSONFS_IDX_GID];
  165. var ifmt = inode.mode & S_IFMT;
  166. if(ifmt === S_IFDIR)
  167. {
  168. this.PushInode(inode, parentid, name);
  169. this.LoadDir(this.inodes.length - 1, data[JSONFS_IDX_TARGET]);
  170. }
  171. else if(ifmt === S_IFREG)
  172. {
  173. inode.status = STATUS_ON_STORAGE;
  174. inode.sha256sum = data[JSONFS_IDX_SHA256];
  175. dbg_assert(inode.sha256sum);
  176. this.PushInode(inode, parentid, name);
  177. }
  178. else if(ifmt === S_IFLNK)
  179. {
  180. inode.symlink = data[JSONFS_IDX_TARGET];
  181. this.PushInode(inode, parentid, name);
  182. }
  183. else if(ifmt === S_IFSOCK)
  184. {
  185. // socket: ignore
  186. }
  187. else
  188. {
  189. dbg_log("Unexpected ifmt: " + h(ifmt) + " (" + name + ")");
  190. }
  191. };
  192. FS.prototype.LoadDir = function(parentid, children)
  193. {
  194. for(var i = 0; i < children.length; i++) {
  195. this.LoadRecursive(children[i], parentid);
  196. }
  197. };
  198. // -----------------------------------------------------
  199. /**
  200. * @private
  201. * @param {Inode} inode
  202. * @return {boolean}
  203. */
  204. FS.prototype.should_be_linked = function(inode)
  205. {
  206. // Note: Non-root forwarder inode could still have a non-forwarder parent, so don't use
  207. // parent inode to check.
  208. return !this.is_forwarder(inode) || inode.foreign_id === 0;
  209. };
  210. /**
  211. * @private
  212. * @param {number} parentid
  213. * @param {number} idx
  214. * @param {string} name
  215. */
  216. FS.prototype.link_under_dir = function(parentid, idx, name)
  217. {
  218. const inode = this.inodes[idx];
  219. const parent_inode = this.inodes[parentid];
  220. dbg_assert(!this.is_forwarder(parent_inode),
  221. "Filesystem: Shouldn't link under fowarder parents");
  222. dbg_assert(this.IsDirectory(parentid),
  223. "Filesystem: Can't link under non-directories");
  224. dbg_assert(this.should_be_linked(inode),
  225. "Filesystem: Can't link across filesystems apart from their root");
  226. dbg_assert(inode.nlinks >= 0,
  227. "Filesystem: Found negative nlinks value of " + inode.nlinks);
  228. dbg_assert(!parent_inode.direntries.has(name),
  229. "Filesystem: Name '" + name + "' is already taken");
  230. parent_inode.direntries.set(name, idx);
  231. inode.nlinks++;
  232. if(this.IsDirectory(idx))
  233. {
  234. dbg_assert(!inode.direntries.has(".."),
  235. "Filesystem: Cannot link a directory twice");
  236. if(!inode.direntries.has(".")) inode.nlinks++;
  237. inode.direntries.set(".", idx);
  238. inode.direntries.set("..", parentid);
  239. parent_inode.nlinks++;
  240. }
  241. };
  242. /**
  243. * @private
  244. * @param {number} parentid
  245. * @param {string} name
  246. */
  247. FS.prototype.unlink_from_dir = function(parentid, name)
  248. {
  249. const idx = this.Search(parentid, name);
  250. const inode = this.inodes[idx];
  251. const parent_inode = this.inodes[parentid];
  252. dbg_assert(!this.is_forwarder(parent_inode), "Filesystem: Can't unlink from forwarders");
  253. dbg_assert(this.IsDirectory(parentid), "Filesystem: Can't unlink from non-directories");
  254. const exists = parent_inode.direntries.delete(name);
  255. if(!exists)
  256. {
  257. dbg_assert(false, "Filesystem: Can't unlink non-existent file: " + name);
  258. return;
  259. }
  260. inode.nlinks--;
  261. if(this.IsDirectory(idx))
  262. {
  263. dbg_assert(inode.direntries.get("..") === parentid,
  264. "Filesystem: Found directory with bad parent id");
  265. inode.direntries.delete("..");
  266. parent_inode.nlinks--;
  267. }
  268. dbg_assert(inode.nlinks >= 0,
  269. "Filesystem: Found negative nlinks value of " + inode.nlinks);
  270. };
  271. FS.prototype.PushInode = function(inode, parentid, name) {
  272. if(parentid !== -1) {
  273. this.inodes.push(inode);
  274. inode.fid = this.inodes.length - 1;
  275. this.link_under_dir(parentid, inode.fid, name);
  276. return;
  277. } else {
  278. if(this.inodes.length === 0) { // if root directory
  279. this.inodes.push(inode);
  280. inode.direntries.set(".", 0);
  281. inode.direntries.set("..", 0);
  282. inode.nlinks = 2;
  283. return;
  284. }
  285. }
  286. message.Debug("Error in Filesystem: Pushed inode with name = "+ name + " has no parent");
  287. message.Abort();
  288. };
  289. /** @constructor */
  290. function Inode(qidnumber)
  291. {
  292. this.direntries = new Map(); // maps filename to inode id
  293. this.status = 0;
  294. this.size = 0x0;
  295. this.uid = 0x0;
  296. this.gid = 0x0;
  297. this.fid = 0;
  298. this.ctime = 0;
  299. this.atime = 0;
  300. this.mtime = 0;
  301. this.major = 0x0;
  302. this.minor = 0x0;
  303. this.symlink = "";
  304. this.mode = 0x01ED;
  305. this.qid = {
  306. type: 0,
  307. version: 0,
  308. path: qidnumber,
  309. };
  310. this.caps = undefined;
  311. this.nlinks = 0;
  312. this.sha256sum = "";
  313. /** @type{!Array<!FSLockRegion>} */
  314. this.locks = []; // lock regions applied to the file, sorted by starting offset.
  315. // For forwarders:
  316. this.mount_id = -1; // which fs in this.mounts does this inode forward to?
  317. this.foreign_id = -1; // which foreign inode id does it represent?
  318. //this.qid_type = 0;
  319. //this.qid_version = 0;
  320. //this.qid_path = qidnumber;
  321. }
  322. Inode.prototype.get_state = function()
  323. {
  324. const state = [];
  325. state[0] = this.mode;
  326. if((this.mode & S_IFMT) === S_IFDIR)
  327. {
  328. state[1] = [...this.direntries];
  329. }
  330. else if((this.mode & S_IFMT) === S_IFREG)
  331. {
  332. state[1] = this.sha256sum;
  333. }
  334. else if((this.mode & S_IFMT) === S_IFLNK)
  335. {
  336. state[1] = this.symlink;
  337. }
  338. else if((this.mode & S_IFMT) === S_IFSOCK)
  339. {
  340. state[1] = [this.minor, this.major];
  341. }
  342. else
  343. {
  344. state[1] = null;
  345. }
  346. state[2] = this.locks;
  347. state[3] = this.status;
  348. state[4] = this.size;
  349. state[5] = this.uid;
  350. state[6] = this.gid;
  351. state[7] = this.fid;
  352. state[8] = this.ctime;
  353. state[9] = this.atime;
  354. state[10] = this.mtime;
  355. state[11] = this.qid.version;
  356. state[12] = this.qid.path;
  357. state[13] = this.nlinks;
  358. //state[23] = this.mount_id;
  359. //state[24] = this.foreign_id;
  360. //state[25] = this.caps; // currently not writable
  361. return state;
  362. };
  363. Inode.prototype.set_state = function(state)
  364. {
  365. this.mode = state[0];
  366. if((this.mode & S_IFMT) === S_IFDIR)
  367. {
  368. this.direntries = new Map();
  369. for(const [name, entry] of state[1])
  370. {
  371. this.direntries.set(name, entry);
  372. }
  373. }
  374. else if((this.mode & S_IFMT) === S_IFREG)
  375. {
  376. this.sha256sum = state[1];
  377. }
  378. else if((this.mode & S_IFMT) === S_IFLNK)
  379. {
  380. this.symlink = state[1];
  381. }
  382. else if((this.mode & S_IFMT) === S_IFSOCK)
  383. {
  384. [this.minor, this.major] = state[1];
  385. }
  386. else
  387. {
  388. // Nothing
  389. }
  390. this.locks = [];
  391. for(const lock_state of state[2])
  392. {
  393. const lock = new FSLockRegion();
  394. lock.set_state(lock_state);
  395. this.locks.push(lock);
  396. }
  397. this.status = state[3];
  398. this.size = state[4];
  399. this.uid = state[5];
  400. this.gid = state[6];
  401. this.fid = state[7];
  402. this.ctime = state[8];
  403. this.atime = state[9];
  404. this.mtime = state[10];
  405. this.qid.type = (this.mode & S_IFMT) >> 8;
  406. this.qid.version = state[11];
  407. this.qid.path = state[12];
  408. this.nlinks = state[13];
  409. //this.mount_id = state[23];
  410. //this.foreign_id = state[24];
  411. //this.caps = state[20];
  412. };
  413. /**
  414. * Clones given inode to new idx, effectively diverting the inode to new idx value.
  415. * Hence, original idx value is now free to use without losing the original information.
  416. * @private
  417. * @param {number} parentid Parent of target to divert.
  418. * @param {string} filename Name of target to divert.
  419. * @return {number} New idx of diversion.
  420. */
  421. FS.prototype.divert = function(parentid, filename)
  422. {
  423. const old_idx = this.Search(parentid, filename);
  424. const old_inode = this.inodes[old_idx];
  425. const new_inode = new Inode(-1);
  426. dbg_assert(old_inode, "Filesystem divert: name (" + filename + ") not found");
  427. dbg_assert(this.IsDirectory(old_idx) || old_inode.nlinks <= 1,
  428. "Filesystem: can't divert hardlinked file '" + filename + "' with nlinks=" +
  429. old_inode.nlinks);
  430. // Shallow copy is alright.
  431. Object.assign(new_inode, old_inode);
  432. const idx = this.inodes.length;
  433. this.inodes.push(new_inode);
  434. new_inode.fid = idx;
  435. // Relink references
  436. if(this.is_forwarder(old_inode))
  437. {
  438. this.mounts[old_inode.mount_id].backtrack.set(old_inode.foreign_id, idx);
  439. }
  440. if(this.should_be_linked(old_inode))
  441. {
  442. this.unlink_from_dir(parentid, filename);
  443. this.link_under_dir(parentid, idx, filename);
  444. }
  445. // Update children
  446. if(this.IsDirectory(old_idx) && !this.is_forwarder(old_inode))
  447. {
  448. for(const [name, child_id] of new_inode.direntries)
  449. {
  450. if(name === "." || name === "..") continue;
  451. if(this.IsDirectory(child_id))
  452. {
  453. this.inodes[child_id].direntries.set("..", idx);
  454. }
  455. }
  456. }
  457. // Relocate local data if any.
  458. this.inodedata[idx] = this.inodedata[old_idx];
  459. delete this.inodedata[old_idx];
  460. // Retire old reference information.
  461. old_inode.direntries = new Map();
  462. old_inode.nlinks = 0;
  463. return idx;
  464. };
  465. /**
  466. * Copy all non-redundant info.
  467. * References left untouched: local idx value and links
  468. * @private
  469. * @param {!Inode} src_inode
  470. * @param {!Inode} dest_inode
  471. */
  472. FS.prototype.copy_inode = function(src_inode, dest_inode)
  473. {
  474. Object.assign(dest_inode, src_inode, {
  475. fid: dest_inode.fid,
  476. direntries: dest_inode.direntries,
  477. nlinks: dest_inode.nlinks,
  478. });
  479. };
  480. FS.prototype.CreateInode = function() {
  481. //console.log("CreateInode", Error().stack);
  482. const now = Math.round(Date.now() / 1000);
  483. const inode = new Inode(++this.qidcounter.last_qidnumber);
  484. inode.atime = inode.ctime = inode.mtime = now;
  485. return inode;
  486. };
  487. // Note: parentid = -1 for initial root directory.
  488. FS.prototype.CreateDirectory = function(name, parentid) {
  489. const parent_inode = this.inodes[parentid];
  490. if(parentid >= 0 && this.is_forwarder(parent_inode))
  491. {
  492. const foreign_parentid = parent_inode.foreign_id;
  493. const foreign_id = this.follow_fs(parent_inode).CreateDirectory(name, foreign_parentid);
  494. return this.create_forwarder(parent_inode.mount_id, foreign_id);
  495. }
  496. var x = this.CreateInode();
  497. x.mode = 0x01FF | S_IFDIR;
  498. if(parentid >= 0) {
  499. x.uid = this.inodes[parentid].uid;
  500. x.gid = this.inodes[parentid].gid;
  501. x.mode = (this.inodes[parentid].mode & 0x1FF) | S_IFDIR;
  502. }
  503. x.qid.type = S_IFDIR >> 8;
  504. this.PushInode(x, parentid, name);
  505. this.NotifyListeners(this.inodes.length-1, "newdir");
  506. return this.inodes.length-1;
  507. };
  508. FS.prototype.CreateFile = function(filename, parentid) {
  509. const parent_inode = this.inodes[parentid];
  510. if(this.is_forwarder(parent_inode))
  511. {
  512. const foreign_parentid = parent_inode.foreign_id;
  513. const foreign_id = this.follow_fs(parent_inode).CreateFile(filename, foreign_parentid);
  514. return this.create_forwarder(parent_inode.mount_id, foreign_id);
  515. }
  516. var x = this.CreateInode();
  517. x.uid = this.inodes[parentid].uid;
  518. x.gid = this.inodes[parentid].gid;
  519. x.qid.type = S_IFREG >> 8;
  520. x.mode = (this.inodes[parentid].mode & 0x1B6) | S_IFREG;
  521. this.PushInode(x, parentid, filename);
  522. this.NotifyListeners(this.inodes.length-1, "newfile");
  523. return this.inodes.length-1;
  524. };
  525. FS.prototype.CreateNode = function(filename, parentid, major, minor) {
  526. const parent_inode = this.inodes[parentid];
  527. if(this.is_forwarder(parent_inode))
  528. {
  529. const foreign_parentid = parent_inode.foreign_id;
  530. const foreign_id =
  531. this.follow_fs(parent_inode).CreateNode(filename, foreign_parentid, major, minor);
  532. return this.create_forwarder(parent_inode.mount_id, foreign_id);
  533. }
  534. var x = this.CreateInode();
  535. x.major = major;
  536. x.minor = minor;
  537. x.uid = this.inodes[parentid].uid;
  538. x.gid = this.inodes[parentid].gid;
  539. x.qid.type = S_IFSOCK >> 8;
  540. x.mode = (this.inodes[parentid].mode & 0x1B6);
  541. this.PushInode(x, parentid, filename);
  542. return this.inodes.length-1;
  543. };
  544. FS.prototype.CreateSymlink = function(filename, parentid, symlink) {
  545. const parent_inode = this.inodes[parentid];
  546. if(this.is_forwarder(parent_inode))
  547. {
  548. const foreign_parentid = parent_inode.foreign_id;
  549. const foreign_id =
  550. this.follow_fs(parent_inode).CreateSymlink(filename, foreign_parentid, symlink);
  551. return this.create_forwarder(parent_inode.mount_id, foreign_id);
  552. }
  553. var x = this.CreateInode();
  554. x.uid = this.inodes[parentid].uid;
  555. x.gid = this.inodes[parentid].gid;
  556. x.qid.type = S_IFLNK >> 8;
  557. x.symlink = symlink;
  558. x.mode = S_IFLNK;
  559. this.PushInode(x, parentid, filename);
  560. return this.inodes.length-1;
  561. };
  562. FS.prototype.CreateTextFile = async function(filename, parentid, str) {
  563. const parent_inode = this.inodes[parentid];
  564. if(this.is_forwarder(parent_inode))
  565. {
  566. const foreign_parentid = parent_inode.foreign_id;
  567. const foreign_id = await
  568. this.follow_fs(parent_inode).CreateTextFile(filename, foreign_parentid, str);
  569. return this.create_forwarder(parent_inode.mount_id, foreign_id);
  570. }
  571. var id = this.CreateFile(filename, parentid);
  572. var x = this.inodes[id];
  573. var data = new Uint8Array(str.length);
  574. x.size = str.length;
  575. for(var j = 0; j < str.length; j++) {
  576. data[j] = str.charCodeAt(j);
  577. }
  578. await this.set_data(id, data);
  579. return id;
  580. };
  581. /**
  582. * @param {Uint8Array} buffer
  583. */
  584. FS.prototype.CreateBinaryFile = async function(filename, parentid, buffer) {
  585. const parent_inode = this.inodes[parentid];
  586. if(this.is_forwarder(parent_inode))
  587. {
  588. const foreign_parentid = parent_inode.foreign_id;
  589. const foreign_id = await
  590. this.follow_fs(parent_inode).CreateBinaryFile(filename, foreign_parentid, buffer);
  591. return this.create_forwarder(parent_inode.mount_id, foreign_id);
  592. }
  593. var id = this.CreateFile(filename, parentid);
  594. var x = this.inodes[id];
  595. var data = new Uint8Array(buffer.length);
  596. data.set(buffer);
  597. await this.set_data(id, data);
  598. x.size = buffer.length;
  599. return id;
  600. };
  601. FS.prototype.OpenInode = function(id, mode) {
  602. var inode = this.inodes[id];
  603. if(this.is_forwarder(inode))
  604. {
  605. return this.follow_fs(inode).OpenInode(inode.foreign_id, mode);
  606. }
  607. if((inode.mode&S_IFMT) === S_IFDIR) {
  608. this.FillDirectory(id);
  609. }
  610. /*
  611. var type = "";
  612. switch(inode.mode&S_IFMT) {
  613. case S_IFREG: type = "File"; break;
  614. case S_IFBLK: type = "Block Device"; break;
  615. case S_IFDIR: type = "Directory"; break;
  616. case S_IFCHR: type = "Character Device"; break;
  617. }
  618. */
  619. //message.Debug("open:" + this.GetFullPath(id) + " type: " + inode.mode + " status:" + inode.status);
  620. return true;
  621. };
  622. FS.prototype.CloseInode = async function(id) {
  623. //message.Debug("close: " + this.GetFullPath(id));
  624. var inode = this.inodes[id];
  625. if(this.is_forwarder(inode))
  626. {
  627. return await this.follow_fs(inode).CloseInode(inode.foreign_id);
  628. }
  629. if(inode.status === STATUS_ON_STORAGE)
  630. {
  631. this.storage.uncache(inode.sha256sum);
  632. }
  633. if(inode.status === STATUS_UNLINKED) {
  634. //message.Debug("Filesystem: Delete unlinked file");
  635. inode.status = STATUS_INVALID;
  636. await this.DeleteData(id);
  637. }
  638. };
  639. /**
  640. * @return {!Promise<number>} 0 if success, or -errno if failured.
  641. */
  642. FS.prototype.Rename = async function(olddirid, oldname, newdirid, newname) {
  643. // message.Debug("Rename " + oldname + " to " + newname);
  644. if((olddirid === newdirid) && (oldname === newname)) {
  645. return 0;
  646. }
  647. var oldid = this.Search(olddirid, oldname);
  648. if(oldid === -1)
  649. {
  650. return -ENOENT;
  651. }
  652. // For event notification near end of method.
  653. var oldpath = this.GetFullPath(olddirid) + "/" + oldname;
  654. var newid = this.Search(newdirid, newname);
  655. if(newid !== -1) {
  656. const ret = this.Unlink(newdirid, newname);
  657. if(ret < 0) return ret;
  658. }
  659. var idx = oldid; // idx contains the id which we want to rename
  660. var inode = this.inodes[idx];
  661. const olddir = this.inodes[olddirid];
  662. const newdir = this.inodes[newdirid];
  663. if(!this.is_forwarder(olddir) && !this.is_forwarder(newdir))
  664. {
  665. // Move inode within current filesystem.
  666. this.unlink_from_dir(olddirid, oldname);
  667. this.link_under_dir(newdirid, idx, newname);
  668. inode.qid.version++;
  669. }
  670. else if(this.is_forwarder(olddir) && olddir.mount_id === newdir.mount_id)
  671. {
  672. // Move inode within the same child filesystem.
  673. const ret = await
  674. this.follow_fs(olddir).Rename(olddir.foreign_id, oldname, newdir.foreign_id, newname);
  675. if(ret < 0) return ret;
  676. }
  677. else if(this.is_a_root(idx))
  678. {
  679. // The actual inode is a root of some descendant filesystem.
  680. // Moving mountpoint across fs not supported - needs to update all corresponding forwarders.
  681. dbg_log("XXX: Attempted to move mountpoint (" + oldname + ") - skipped", LOG_9P);
  682. return -EPERM;
  683. }
  684. else if(!this.IsDirectory(idx) && this.GetInode(idx).nlinks > 1)
  685. {
  686. // Move hardlinked inode vertically in mount tree.
  687. dbg_log("XXX: Attempted to move hardlinked file (" + oldname + ") " +
  688. "across filesystems - skipped", LOG_9P);
  689. return -EPERM;
  690. }
  691. else
  692. {
  693. // Jump between filesystems.
  694. // Can't work with both old and new inode information without first diverting the old
  695. // information into a new idx value.
  696. const diverted_old_idx = this.divert(olddirid, oldname);
  697. const old_real_inode = this.GetInode(idx);
  698. const data = await this.Read(diverted_old_idx, 0, old_real_inode.size);
  699. if(this.is_forwarder(newdir))
  700. {
  701. // Create new inode.
  702. const foreign_fs = this.follow_fs(newdir);
  703. const foreign_id = this.IsDirectory(diverted_old_idx) ?
  704. foreign_fs.CreateDirectory(newname, newdir.foreign_id) :
  705. foreign_fs.CreateFile(newname, newdir.foreign_id);
  706. const new_real_inode = foreign_fs.GetInode(foreign_id);
  707. this.copy_inode(old_real_inode, new_real_inode);
  708. // Point to this new location.
  709. this.set_forwarder(idx, newdir.mount_id, foreign_id);
  710. }
  711. else
  712. {
  713. // Replace current forwarder with real inode.
  714. this.delete_forwarder(inode);
  715. this.copy_inode(old_real_inode, inode);
  716. // Link into new location in this filesystem.
  717. this.link_under_dir(newdirid, idx, newname);
  718. }
  719. // Rewrite data to newly created destination.
  720. await this.ChangeSize(idx, old_real_inode.size);
  721. if(data && data.length)
  722. {
  723. await this.Write(idx, 0, data.length, data);
  724. }
  725. // Move children to newly created destination.
  726. if(this.IsDirectory(idx))
  727. {
  728. for(const child_filename of this.GetChildren(diverted_old_idx))
  729. {
  730. const ret = await this.Rename(diverted_old_idx, child_filename, idx, child_filename);
  731. if(ret < 0) return ret;
  732. }
  733. }
  734. // Perform destructive changes only after migration succeeded.
  735. await this.DeleteData(diverted_old_idx);
  736. const ret = this.Unlink(olddirid, oldname);
  737. if(ret < 0) return ret;
  738. }
  739. this.NotifyListeners(idx, "rename", {oldpath: oldpath});
  740. return 0;
  741. };
  742. FS.prototype.Write = async function(id, offset, count, buffer) {
  743. this.NotifyListeners(id, "write");
  744. var inode = this.inodes[id];
  745. if(this.is_forwarder(inode))
  746. {
  747. const foreign_id = inode.foreign_id;
  748. await this.follow_fs(inode).Write(foreign_id, offset, count, buffer);
  749. return;
  750. }
  751. var data = await this.get_buffer(id);
  752. if(!data || data.length < (offset+count)) {
  753. await this.ChangeSize(id, Math.floor(((offset+count)*3)/2));
  754. inode.size = offset + count;
  755. data = await this.get_buffer(id);
  756. } else
  757. if(inode.size < (offset+count)) {
  758. inode.size = offset + count;
  759. }
  760. if(buffer)
  761. {
  762. data.set(buffer.subarray(0, count), offset);
  763. }
  764. await this.set_data(id, data);
  765. };
  766. FS.prototype.Read = async function(inodeid, offset, count)
  767. {
  768. const inode = this.inodes[inodeid];
  769. if(this.is_forwarder(inode))
  770. {
  771. const foreign_id = inode.foreign_id;
  772. return await this.follow_fs(inode).Read(foreign_id, offset, count);
  773. }
  774. return await this.get_data(inodeid, offset, count);
  775. };
  776. FS.prototype.Search = function(parentid, name) {
  777. const parent_inode = this.inodes[parentid];
  778. if(this.is_forwarder(parent_inode))
  779. {
  780. const foreign_parentid = parent_inode.foreign_id;
  781. const foreign_id = this.follow_fs(parent_inode).Search(foreign_parentid, name);
  782. if(foreign_id === -1) return -1;
  783. return this.get_forwarder(parent_inode.mount_id, foreign_id);
  784. }
  785. const childid = parent_inode.direntries.get(name);
  786. return childid === undefined ? -1 : childid;
  787. };
  788. FS.prototype.CountUsedInodes = function()
  789. {
  790. let count = this.inodes.length;
  791. for(const { fs, backtrack } of this.mounts)
  792. {
  793. count += fs.CountUsedInodes();
  794. // Forwarder inodes don't count.
  795. count -= backtrack.size;
  796. }
  797. return count;
  798. };
  799. FS.prototype.CountFreeInodes = function()
  800. {
  801. let count = 1024 * 1024;
  802. for(const { fs } of this.mounts)
  803. {
  804. count += fs.CountFreeInodes();
  805. }
  806. return count;
  807. };
  808. FS.prototype.GetTotalSize = function() {
  809. let size = this.used_size;
  810. for(const { fs } of this.mounts)
  811. {
  812. size += fs.GetTotalSize();
  813. }
  814. return size;
  815. //var size = 0;
  816. //for(var i=0; i<this.inodes.length; i++) {
  817. // var d = this.inodes[i].data;
  818. // size += d ? d.length : 0;
  819. //}
  820. //return size;
  821. };
  822. FS.prototype.GetSpace = function() {
  823. let size = this.total_size;
  824. for(const { fs } of this.mounts)
  825. {
  826. size += fs.GetSpace();
  827. }
  828. return this.total_size;
  829. };
  830. /**
  831. * XXX: Not ideal.
  832. * @param {number} idx
  833. * @return {string}
  834. */
  835. FS.prototype.GetDirectoryName = function(idx)
  836. {
  837. const parent_inode = this.inodes[this.GetParent(idx)];
  838. if(this.is_forwarder(parent_inode))
  839. {
  840. return this.follow_fs(parent_inode).GetDirectoryName(this.inodes[idx].foreign_id);
  841. }
  842. // Root directory.
  843. if(!parent_inode) return "";
  844. for(const [name, childid] of parent_inode.direntries)
  845. {
  846. if(childid === idx) return name;
  847. }
  848. dbg_assert(false, "Filesystem: Found directory inode whose parent doesn't link to it");
  849. return "";
  850. };
  851. FS.prototype.GetFullPath = function(idx) {
  852. dbg_assert(this.IsDirectory(idx), "Filesystem: Cannot get full path of non-directory inode");
  853. var path = "";
  854. while(idx !== 0) {
  855. path = "/" + this.GetDirectoryName(idx) + path;
  856. idx = this.GetParent(idx);
  857. }
  858. return path.substring(1);
  859. };
  860. /**
  861. * @param {number} parentid
  862. * @param {number} targetid
  863. * @param {string} name
  864. * @return {number} 0 if success, or -errno if failured.
  865. */
  866. FS.prototype.Link = function(parentid, targetid, name)
  867. {
  868. if(this.IsDirectory(targetid))
  869. {
  870. return -EPERM;
  871. }
  872. const parent_inode = this.inodes[parentid];
  873. const inode = this.inodes[targetid];
  874. if(this.is_forwarder(parent_inode))
  875. {
  876. if(!this.is_forwarder(inode) || inode.mount_id !== parent_inode.mount_id)
  877. {
  878. dbg_log("XXX: Attempted to hardlink a file into a child filesystem - skipped", LOG_9P);
  879. return -EPERM;
  880. }
  881. return this.follow_fs(parent_inode).Link(parent_inode.foreign_id, inode.foreign_id, name);
  882. }
  883. if(this.is_forwarder(inode))
  884. {
  885. dbg_log("XXX: Attempted to hardlink file across filesystems - skipped", LOG_9P);
  886. return -EPERM;
  887. }
  888. this.link_under_dir(parentid, targetid, name);
  889. return 0;
  890. };
  891. FS.prototype.Unlink = function(parentid, name) {
  892. if(name === "." || name === "..")
  893. {
  894. // Also guarantees that root cannot be deleted.
  895. return -EPERM;
  896. }
  897. const idx = this.Search(parentid, name);
  898. const inode = this.inodes[idx];
  899. const parent_inode = this.inodes[parentid];
  900. //message.Debug("Unlink " + inode.name);
  901. // forward if necessary
  902. if(this.is_forwarder(parent_inode))
  903. {
  904. dbg_assert(this.is_forwarder(inode), "Children of forwarders should be forwarders");
  905. const foreign_parentid = parent_inode.foreign_id;
  906. return this.follow_fs(parent_inode).Unlink(foreign_parentid, name);
  907. // Keep the forwarder dangling - file is still accessible.
  908. }
  909. if(this.IsDirectory(idx) && !this.IsEmpty(idx))
  910. {
  911. return -ENOTEMPTY;
  912. }
  913. this.unlink_from_dir(parentid, name);
  914. if(inode.nlinks === 0)
  915. {
  916. // don't delete the content. The file is still accessible
  917. inode.status = STATUS_UNLINKED;
  918. this.NotifyListeners(idx, "delete");
  919. }
  920. return 0;
  921. };
  922. FS.prototype.DeleteData = async function(idx)
  923. {
  924. const inode = this.inodes[idx];
  925. if(this.is_forwarder(inode))
  926. {
  927. await this.follow_fs(inode).DeleteData(inode.foreign_id);
  928. return;
  929. }
  930. inode.size = 0;
  931. delete this.inodedata[idx];
  932. };
  933. /**
  934. * @private
  935. * @param {number} idx
  936. * @return {!Promise<Uint8Array>} The buffer that contains the file contents, which may be larger
  937. * than the data itself. To ensure that any modifications done to this buffer is reflected
  938. * to the file, call set_data with the modified buffer.
  939. */
  940. FS.prototype.get_buffer = async function(idx)
  941. {
  942. const inode = this.inodes[idx];
  943. dbg_assert(inode, `Filesystem get_buffer: idx ${idx} does not point to an inode`);
  944. if(this.inodedata[idx])
  945. {
  946. return this.inodedata[idx];
  947. }
  948. else if(inode.status === STATUS_ON_STORAGE)
  949. {
  950. dbg_assert(inode.sha256sum, "Filesystem get_data: found inode on server without sha256sum");
  951. return await this.storage.read(inode.sha256sum, 0, inode.size);
  952. }
  953. else
  954. {
  955. return null;
  956. }
  957. };
  958. /**
  959. * @private
  960. * @param {number} idx
  961. * @param {number} offset
  962. * @param {number} count
  963. * @return {!Promise<Uint8Array>}
  964. */
  965. FS.prototype.get_data = async function(idx, offset, count)
  966. {
  967. const inode = this.inodes[idx];
  968. dbg_assert(inode, `Filesystem get_data: idx ${idx} does not point to an inode`);
  969. if(this.inodedata[idx])
  970. {
  971. return this.inodedata[idx].subarray(offset, offset + count);
  972. }
  973. else if(inode.status === STATUS_ON_STORAGE)
  974. {
  975. dbg_assert(inode.sha256sum, "Filesystem get_data: found inode on server without sha256sum");
  976. return await this.storage.read(inode.sha256sum, offset, count);
  977. }
  978. else
  979. {
  980. return null;
  981. }
  982. };
  983. /**
  984. * @private
  985. * @param {number} idx
  986. * @param {Uint8Array} buffer
  987. */
  988. FS.prototype.set_data = async function(idx, buffer)
  989. {
  990. // Current scheme: Save all modified buffers into local inodedata.
  991. this.inodedata[idx] = buffer;
  992. if(this.inodes[idx].status === STATUS_ON_STORAGE)
  993. {
  994. this.inodes[idx].status = STATUS_OK;
  995. this.storage.uncache(this.inodes[idx].sha256sum);
  996. }
  997. };
  998. /**
  999. * @param {number} idx
  1000. * @return {!Inode}
  1001. */
  1002. FS.prototype.GetInode = function(idx)
  1003. {
  1004. dbg_assert(!isNaN(idx), "Filesystem GetInode: NaN idx");
  1005. dbg_assert(idx >= 0 && idx < this.inodes.length, "Filesystem GetInode: out of range idx:" + idx);
  1006. const inode = this.inodes[idx];
  1007. if(this.is_forwarder(inode))
  1008. {
  1009. return this.follow_fs(inode).GetInode(inode.foreign_id);
  1010. }
  1011. return inode;
  1012. };
  1013. FS.prototype.ChangeSize = async function(idx, newsize)
  1014. {
  1015. var inode = this.GetInode(idx);
  1016. var temp = await this.get_data(idx, 0, inode.size);
  1017. //message.Debug("change size to: " + newsize);
  1018. if(newsize === inode.size) return;
  1019. var data = new Uint8Array(newsize);
  1020. inode.size = newsize;
  1021. if(temp)
  1022. {
  1023. var size = Math.min(temp.length, inode.size);
  1024. data.set(temp.subarray(0, size), 0);
  1025. }
  1026. await this.set_data(idx, data);
  1027. };
  1028. FS.prototype.SearchPath = function(path) {
  1029. //path = path.replace(/\/\//g, "/");
  1030. path = path.replace("//", "/");
  1031. var walk = path.split("/");
  1032. if(walk.length > 0 && walk[walk.length - 1].length === 0) walk.pop();
  1033. if(walk.length > 0 && walk[0].length === 0) walk.shift();
  1034. const n = walk.length;
  1035. var parentid = -1;
  1036. var id = 0;
  1037. let forward_path = null;
  1038. for(var i=0; i<n; i++) {
  1039. parentid = id;
  1040. id = this.Search(parentid, walk[i]);
  1041. if(!forward_path && this.is_forwarder(this.inodes[parentid]))
  1042. {
  1043. forward_path = "/" + walk.slice(i).join("/");
  1044. }
  1045. if(id === -1) {
  1046. if(i < n-1) return {id: -1, parentid: -1, name: walk[i], forward_path }; // one name of the path cannot be found
  1047. return {id: -1, parentid: parentid, name: walk[i], forward_path}; // the last element in the path does not exist, but the parent
  1048. }
  1049. }
  1050. return {id: id, parentid: parentid, name: walk[i], forward_path};
  1051. };
  1052. // -----------------------------------------------------
  1053. /**
  1054. * @param {number} dirid
  1055. * @param {Array<{parentid: number, name: string}>} list
  1056. */
  1057. FS.prototype.GetRecursiveList = function(dirid, list) {
  1058. if(this.is_forwarder(this.inodes[dirid]))
  1059. {
  1060. const foreign_fs = this.follow_fs(this.inodes[dirid]);
  1061. const foreign_dirid = this.inodes[dirid].foreign_id;
  1062. const mount_id = this.inodes[dirid].mount_id;
  1063. const foreign_start = list.length;
  1064. foreign_fs.GetRecursiveList(foreign_dirid, list);
  1065. for(let i = foreign_start; i < list.length; i++)
  1066. {
  1067. list[i].parentid = this.get_forwarder(mount_id, list[i].parentid);
  1068. }
  1069. return;
  1070. }
  1071. for(const [name, id] of this.inodes[dirid].direntries)
  1072. {
  1073. if(name !== "." && name !== "..")
  1074. {
  1075. list.push({ parentid: dirid, name });
  1076. if(this.IsDirectory(id))
  1077. {
  1078. this.GetRecursiveList(id, list);
  1079. }
  1080. }
  1081. }
  1082. };
  1083. FS.prototype.RecursiveDelete = function(path) {
  1084. var toDelete = [];
  1085. var ids = this.SearchPath(path);
  1086. if(ids.id === -1) return;
  1087. this.GetRecursiveList(ids.id, toDelete);
  1088. for(var i=toDelete.length-1; i>=0; i--)
  1089. {
  1090. const ret = this.Unlink(toDelete[i].parentid, toDelete[i].name);
  1091. dbg_assert(ret === 0, "Filesystem RecursiveDelete failed at parent=" + toDelete[i].parentid +
  1092. ", name='" + toDelete[i].name + "' with error code: " + (-ret));
  1093. }
  1094. };
  1095. FS.prototype.DeleteNode = function(path) {
  1096. var ids = this.SearchPath(path);
  1097. if(ids.id === -1) return;
  1098. if((this.inodes[ids.id].mode&S_IFMT) === S_IFREG){
  1099. const ret = this.Unlink(ids.parentid, ids.name);
  1100. dbg_assert(ret === 0, "Filesystem DeleteNode failed with error code: " + (-ret));
  1101. }
  1102. else if((this.inodes[ids.id].mode&S_IFMT) === S_IFDIR){
  1103. this.RecursiveDelete(path);
  1104. const ret = this.Unlink(ids.parentid, ids.name);
  1105. dbg_assert(ret === 0, "Filesystem DeleteNode failed with error code: " + (-ret));
  1106. }
  1107. };
  1108. /** @param {*=} info */
  1109. FS.prototype.NotifyListeners = function(id, action, info) {
  1110. //if(info==undefined)
  1111. // info = {};
  1112. //var path = this.GetFullPath(id);
  1113. //if (this.watchFiles[path] === true && action=='write') {
  1114. // message.Send("WatchFileEvent", path);
  1115. //}
  1116. //for (var directory of this.watchDirectories) {
  1117. // if (this.watchDirectories.hasOwnProperty(directory)) {
  1118. // var indexOf = path.indexOf(directory)
  1119. // if(indexOf === 0 || indexOf === 1)
  1120. // message.Send("WatchDirectoryEvent", {path: path, event: action, info: info});
  1121. // }
  1122. //}
  1123. };
  1124. FS.prototype.Check = function() {
  1125. for(var i=1; i<this.inodes.length; i++)
  1126. {
  1127. if(this.inodes[i].status === STATUS_INVALID) continue;
  1128. var inode = this.GetInode(i);
  1129. if(inode.nlinks < 0) {
  1130. message.Debug("Error in filesystem: negative nlinks=" + inode.nlinks + " at id =" + i);
  1131. }
  1132. if(this.IsDirectory(i))
  1133. {
  1134. const inode = this.GetInode(i);
  1135. if(this.IsDirectory(i) && this.GetParent(i) < 0) {
  1136. message.Debug("Error in filesystem: negative parent id " + i);
  1137. }
  1138. for(const [name, id] of inode.direntries)
  1139. {
  1140. if(name.length === 0) {
  1141. message.Debug("Error in filesystem: inode with no name and id " + id);
  1142. }
  1143. for(const c of name) {
  1144. if(c < 32) {
  1145. message.Debug("Error in filesystem: Unallowed char in filename");
  1146. }
  1147. }
  1148. }
  1149. }
  1150. }
  1151. };
  1152. FS.prototype.FillDirectory = function(dirid) {
  1153. const inode = this.inodes[dirid];
  1154. if(this.is_forwarder(inode))
  1155. {
  1156. // XXX: The ".." of a mountpoint should point back to an inode in this fs.
  1157. // Otherwise, ".." gets the wrong qid and mode.
  1158. this.follow_fs(inode).FillDirectory(inode.foreign_id);
  1159. return;
  1160. }
  1161. let size = 0;
  1162. for(const name of inode.direntries.keys())
  1163. {
  1164. size += 13 + 8 + 1 + 2 + texten.encode(name).length;
  1165. }
  1166. const data = this.inodedata[dirid] = new Uint8Array(size);
  1167. inode.size = size;
  1168. let offset = 0x0;
  1169. for(const [name, id] of inode.direntries)
  1170. {
  1171. const child = this.GetInode(id);
  1172. offset += marshall.Marshall(
  1173. ["Q", "d", "b", "s"],
  1174. [child.qid,
  1175. offset+13+8+1+2+texten.encode(name).length,
  1176. child.mode >> 12,
  1177. name],
  1178. data, offset);
  1179. }
  1180. };
  1181. FS.prototype.RoundToDirentry = function(dirid, offset_target)
  1182. {
  1183. const data = this.inodedata[dirid];
  1184. dbg_assert(data, `FS directory data for dirid=${dirid} should be generated`);
  1185. dbg_assert(data.length, "FS directory should have at least an entry");
  1186. if(offset_target >= data.length)
  1187. {
  1188. return data.length;
  1189. }
  1190. let offset = 0;
  1191. while(true)
  1192. {
  1193. const next_offset = marshall.Unmarshall(["Q", "d"], data, { offset })[1];
  1194. if(next_offset > offset_target) break;
  1195. offset = next_offset;
  1196. }
  1197. return offset;
  1198. };
  1199. /**
  1200. * @param {number} idx
  1201. * @return {boolean}
  1202. */
  1203. FS.prototype.IsDirectory = function(idx)
  1204. {
  1205. const inode = this.inodes[idx];
  1206. if(this.is_forwarder(inode))
  1207. {
  1208. return this.follow_fs(inode).IsDirectory(inode.foreign_id);
  1209. }
  1210. return (inode.mode & S_IFMT) === S_IFDIR;
  1211. };
  1212. /**
  1213. * @param {number} idx
  1214. * @return {boolean}
  1215. */
  1216. FS.prototype.IsEmpty = function(idx)
  1217. {
  1218. const inode = this.inodes[idx];
  1219. if(this.is_forwarder(inode))
  1220. {
  1221. return this.follow_fs(inode).IsDirectory(inode.foreign_id);
  1222. }
  1223. for(const name of inode.direntries.keys())
  1224. {
  1225. if(name !== "." && name !== "..") return false;
  1226. }
  1227. return true;
  1228. };
  1229. /**
  1230. * @param {number} idx
  1231. * @return {!Array<string>} List of children names
  1232. */
  1233. FS.prototype.GetChildren = function(idx)
  1234. {
  1235. dbg_assert(this.IsDirectory(idx), "Filesystem: cannot get children of non-directory inode");
  1236. const inode = this.inodes[idx];
  1237. if(this.is_forwarder(inode))
  1238. {
  1239. return this.follow_fs(inode).GetChildren(inode.foreign_id);
  1240. }
  1241. const children = [];
  1242. for(const name of inode.direntries.keys())
  1243. {
  1244. if(name !== "." && name !== "..")
  1245. {
  1246. children.push(name);
  1247. }
  1248. }
  1249. return children;
  1250. };
  1251. /**
  1252. * @param {number} idx
  1253. * @return {number} Local idx of parent
  1254. */
  1255. FS.prototype.GetParent = function(idx)
  1256. {
  1257. dbg_assert(this.IsDirectory(idx), "Filesystem: cannot get parent of non-directory inode");
  1258. const inode = this.inodes[idx];
  1259. if(this.should_be_linked(inode))
  1260. {
  1261. return inode.direntries.get("..");
  1262. }
  1263. else
  1264. {
  1265. const foreign_dirid = this.follow_fs(inode).GetParent(inode.foreign_id);
  1266. dbg_assert(foreign_dirid !== -1, "Filesystem: should not have invalid parent ids");
  1267. return this.get_forwarder(inode.mount_id, foreign_dirid);
  1268. }
  1269. };
  1270. // -----------------------------------------------------
  1271. // only support for security.capabilities
  1272. // should return a "struct vfs_cap_data" defined in
  1273. // linux/capability for format
  1274. // check also:
  1275. // sys/capability.h
  1276. // http://lxr.free-electrons.com/source/security/commoncap.c#L376
  1277. // http://man7.org/linux/man-pages/man7/capabilities.7.html
  1278. // http://man7.org/linux/man-pages/man8/getcap.8.html
  1279. // http://man7.org/linux/man-pages/man3/libcap.3.html
  1280. FS.prototype.PrepareCAPs = function(id) {
  1281. var inode = this.GetInode(id);
  1282. if(inode.caps) return inode.caps.length;
  1283. inode.caps = new Uint8Array(20);
  1284. // format is little endian
  1285. // note: getxattr returns -EINVAL if using revision 1 format.
  1286. // note: getxattr presents revision 3 as revision 2 when revision 3 is not needed.
  1287. // magic_etc (revision=0x02: 20 bytes)
  1288. inode.caps[0] = 0x00;
  1289. inode.caps[1] = 0x00;
  1290. inode.caps[2] = 0x00;
  1291. inode.caps[3] = 0x02;
  1292. // lower
  1293. // permitted (first 32 capabilities)
  1294. inode.caps[4] = 0xFF;
  1295. inode.caps[5] = 0xFF;
  1296. inode.caps[6] = 0xFF;
  1297. inode.caps[7] = 0xFF;
  1298. // inheritable (first 32 capabilities)
  1299. inode.caps[8] = 0xFF;
  1300. inode.caps[9] = 0xFF;
  1301. inode.caps[10] = 0xFF;
  1302. inode.caps[11] = 0xFF;
  1303. // higher
  1304. // permitted (last 6 capabilities)
  1305. inode.caps[12] = 0x3F;
  1306. inode.caps[13] = 0x00;
  1307. inode.caps[14] = 0x00;
  1308. inode.caps[15] = 0x00;
  1309. // inheritable (last 6 capabilities)
  1310. inode.caps[16] = 0x3F;
  1311. inode.caps[17] = 0x00;
  1312. inode.caps[18] = 0x00;
  1313. inode.caps[19] = 0x00;
  1314. return inode.caps.length;
  1315. };
  1316. // -----------------------------------------------------
  1317. /**
  1318. * @constructor
  1319. * @param {FS} filesystem
  1320. */
  1321. function FSMountInfo(filesystem)
  1322. {
  1323. /** @type {FS}*/
  1324. this.fs = filesystem;
  1325. /**
  1326. * Maps foreign inode id back to local inode id.
  1327. * @type {!Map<number,number>}
  1328. */
  1329. this.backtrack = new Map();
  1330. }
  1331. FSMountInfo.prototype.get_state = function()
  1332. {
  1333. const state = [];
  1334. state[0] = this.fs;
  1335. state[1] = [...this.backtrack];
  1336. return state;
  1337. };
  1338. FSMountInfo.prototype.set_state = function(state)
  1339. {
  1340. this.fs = state[0];
  1341. this.backtrack = new Map(state[1]);
  1342. };
  1343. /**
  1344. * @private
  1345. * @param {number} idx Local idx of inode.
  1346. * @param {number} mount_id Mount number of the destination fs.
  1347. * @param {number} foreign_id Foreign idx of destination inode.
  1348. */
  1349. FS.prototype.set_forwarder = function(idx, mount_id, foreign_id)
  1350. {
  1351. const inode = this.inodes[idx];
  1352. dbg_assert(inode.nlinks === 0,
  1353. "Filesystem: attempted to convert an inode into forwarder before unlinking the inode");
  1354. if(this.is_forwarder(inode))
  1355. {
  1356. this.mounts[inode.mount_id].backtrack.delete(inode.foreign_id);
  1357. }
  1358. inode.status = STATUS_FORWARDING;
  1359. inode.mount_id = mount_id;
  1360. inode.foreign_id = foreign_id;
  1361. this.mounts[mount_id].backtrack.set(foreign_id, idx);
  1362. };
  1363. /**
  1364. * @private
  1365. * @param {number} mount_id Mount number of the destination fs.
  1366. * @param {number} foreign_id Foreign idx of destination inode.
  1367. * @return {number} Local idx of newly created forwarder.
  1368. */
  1369. FS.prototype.create_forwarder = function(mount_id, foreign_id)
  1370. {
  1371. const inode = this.CreateInode();
  1372. const idx = this.inodes.length;
  1373. this.inodes.push(inode);
  1374. inode.fid = idx;
  1375. this.set_forwarder(idx, mount_id, foreign_id);
  1376. return idx;
  1377. };
  1378. /**
  1379. * @private
  1380. * @param {Inode} inode
  1381. * @return {boolean}
  1382. */
  1383. FS.prototype.is_forwarder = function(inode)
  1384. {
  1385. return inode.status === STATUS_FORWARDING;
  1386. };
  1387. /**
  1388. * Whether the inode it points to is a root of some filesystem.
  1389. * @private
  1390. * @param {number} idx
  1391. * @return {boolean}
  1392. */
  1393. FS.prototype.is_a_root = function(idx)
  1394. {
  1395. return this.GetInode(idx).fid === 0;
  1396. };
  1397. /**
  1398. * Ensures forwarder exists, and returns such forwarder, for the described foreign inode.
  1399. * @private
  1400. * @param {number} mount_id
  1401. * @param {number} foreign_id
  1402. * @return {number} Local idx of a forwarder to described inode.
  1403. */
  1404. FS.prototype.get_forwarder = function(mount_id, foreign_id)
  1405. {
  1406. const mount = this.mounts[mount_id];
  1407. dbg_assert(foreign_id >= 0, "Filesystem get_forwarder: invalid foreign_id: " + foreign_id);
  1408. dbg_assert(mount, "Filesystem get_forwarder: invalid mount number: " + mount_id);
  1409. const result = mount.backtrack.get(foreign_id);
  1410. if(result === undefined)
  1411. {
  1412. // Create if not already exists.
  1413. return this.create_forwarder(mount_id, foreign_id);
  1414. }
  1415. return result;
  1416. };
  1417. /**
  1418. * @private
  1419. * @param {Inode} inode
  1420. */
  1421. FS.prototype.delete_forwarder = function(inode)
  1422. {
  1423. dbg_assert(this.is_forwarder(inode), "Filesystem delete_forwarder: expected forwarder");
  1424. inode.status = STATUS_INVALID;
  1425. this.mounts[inode.mount_id].backtrack.delete(inode.foreign_id);
  1426. };
  1427. /**
  1428. * @private
  1429. * @param {Inode} inode
  1430. * @return {FS}
  1431. */
  1432. FS.prototype.follow_fs = function(inode)
  1433. {
  1434. const mount = this.mounts[inode.mount_id];
  1435. dbg_assert(this.is_forwarder(inode),
  1436. "Filesystem follow_fs: inode should be a forwarding inode");
  1437. dbg_assert(mount, "Filesystem follow_fs: inode<id=" + inode.fid +
  1438. "> should point to valid mounted FS");
  1439. return mount.fs;
  1440. };
  1441. /**
  1442. * Mount another filesystem to given path.
  1443. * @param {string} path
  1444. * @param {FS} fs
  1445. * @return {number} inode id of mount point if successful, or -errno if mounting failed.
  1446. */
  1447. FS.prototype.Mount = function(path, fs)
  1448. {
  1449. dbg_assert(fs.qidcounter === this.qidcounter,
  1450. "Cannot mount filesystem whose qid numbers aren't synchronised with current filesystem.");
  1451. const path_infos = this.SearchPath(path);
  1452. if(path_infos.parentid === -1)
  1453. {
  1454. dbg_log("Mount failed: parent for path not found: " + path, LOG_9P);
  1455. return -ENOENT;
  1456. }
  1457. if(path_infos.id !== -1)
  1458. {
  1459. dbg_log("Mount failed: file already exists at path: " + path, LOG_9P);
  1460. return -EEXIST;
  1461. }
  1462. if(path_infos.forward_path)
  1463. {
  1464. const parent = this.inodes[path_infos.parentid];
  1465. const ret = this.follow_fs(parent).Mount(path_infos.forward_path, fs);
  1466. if(ret < 0) return ret;
  1467. return this.get_forwarder(parent.mount_id, ret);
  1468. }
  1469. const mount_id = this.mounts.length;
  1470. this.mounts.push(new FSMountInfo(fs));
  1471. const idx = this.create_forwarder(mount_id, 0);
  1472. this.link_under_dir(path_infos.parentid, idx, path_infos.name);
  1473. return idx;
  1474. };
  1475. /**
  1476. * @constructor
  1477. */
  1478. function FSLockRegion()
  1479. {
  1480. this.type = P9_LOCK_TYPE_UNLCK;
  1481. this.start = 0;
  1482. this.length = Infinity;
  1483. this.proc_id = -1;
  1484. this.client_id = "";
  1485. }
  1486. FSLockRegion.prototype.get_state = function()
  1487. {
  1488. const state = [];
  1489. state[0] = this.type;
  1490. state[1] = this.start;
  1491. // Infinity is not JSON.stringify-able
  1492. state[2] = this.length === Infinity ? 0 : this.length;
  1493. state[3] = this.proc_id;
  1494. state[4] = this.client_id;
  1495. return state;
  1496. };
  1497. FSLockRegion.prototype.set_state = function(state)
  1498. {
  1499. this.type = state[0];
  1500. this.start = state[1];
  1501. this.length = state[2] === 0 ? Infinity : state[2];
  1502. this.proc_id = state[3];
  1503. this.client_id = state[4];
  1504. };
  1505. /**
  1506. * @return {FSLockRegion}
  1507. */
  1508. FSLockRegion.prototype.clone = function()
  1509. {
  1510. const new_region = new FSLockRegion();
  1511. new_region.set_state(this.get_state());
  1512. return new_region;
  1513. };
  1514. /**
  1515. * @param {FSLockRegion} region
  1516. * @return {boolean}
  1517. */
  1518. FSLockRegion.prototype.conflicts_with = function(region)
  1519. {
  1520. if(this.proc_id === region.proc_id && this.client_id === region.client_id) return false;
  1521. if(this.type === P9_LOCK_TYPE_UNLCK || region.type === P9_LOCK_TYPE_UNLCK) return false;
  1522. if(this.type !== P9_LOCK_TYPE_WRLCK && region.type !== P9_LOCK_TYPE_WRLCK) return false;
  1523. if(this.start + this.length <= region.start) return false;
  1524. if(region.start + region.length <= this.start) return false;
  1525. return true;
  1526. };
  1527. /**
  1528. * @param {FSLockRegion} region
  1529. * @return {boolean}
  1530. */
  1531. FSLockRegion.prototype.is_alike = function(region)
  1532. {
  1533. return region.proc_id === this.proc_id &&
  1534. region.client_id === this.client_id &&
  1535. region.type === this.type;
  1536. };
  1537. /**
  1538. * @param {FSLockRegion} region
  1539. * @return {boolean}
  1540. */
  1541. FSLockRegion.prototype.may_merge_after = function(region)
  1542. {
  1543. return this.is_alike(region) && region.start + region.length === this.start;
  1544. };
  1545. /**
  1546. * @param {number} type
  1547. * @param {number} start
  1548. * @param {number} length
  1549. * @param {number} proc_id
  1550. * @param {string} client_id
  1551. * @return {!FSLockRegion}
  1552. */
  1553. FS.prototype.DescribeLock = function(type, start, length, proc_id, client_id)
  1554. {
  1555. dbg_assert(type === P9_LOCK_TYPE_RDLCK ||
  1556. type === P9_LOCK_TYPE_WRLCK ||
  1557. type === P9_LOCK_TYPE_UNLCK,
  1558. "Filesystem: Invalid lock type: " + type);
  1559. dbg_assert(start >= 0, "Filesystem: Invalid negative lock starting offset: " + start);
  1560. dbg_assert(length > 0, "Filesystem: Invalid non-positive lock length: " + length);
  1561. const lock = new FSLockRegion();
  1562. lock.type = type;
  1563. lock.start = start;
  1564. lock.length = length;
  1565. lock.proc_id = proc_id;
  1566. lock.client_id = client_id;
  1567. return lock;
  1568. };
  1569. /**
  1570. * @param {number} id
  1571. * @param {FSLockRegion} request
  1572. * @return {FSLockRegion} The first conflicting lock found, or null if requested lock is possible.
  1573. */
  1574. FS.prototype.GetLock = function(id, request)
  1575. {
  1576. const inode = this.inodes[id];
  1577. if(this.is_forwarder(inode))
  1578. {
  1579. const foreign_id = inode.foreign_id;
  1580. return this.follow_fs(inode).GetLock(foreign_id, request);
  1581. }
  1582. for(const region of inode.locks)
  1583. {
  1584. if(request.conflicts_with(region))
  1585. {
  1586. return region.clone();
  1587. }
  1588. }
  1589. return null;
  1590. };
  1591. /**
  1592. * @param {number} id
  1593. * @param {FSLockRegion} request
  1594. * @param {number} flags
  1595. * @return {number} One of P9_LOCK_SUCCESS / P9_LOCK_BLOCKED / P9_LOCK_ERROR / P9_LOCK_GRACE.
  1596. */
  1597. FS.prototype.Lock = function(id, request, flags)
  1598. {
  1599. const inode = this.inodes[id];
  1600. if(this.is_forwarder(inode))
  1601. {
  1602. const foreign_id = inode.foreign_id;
  1603. return this.follow_fs(inode).Lock(foreign_id, request, flags);
  1604. }
  1605. request = request.clone();
  1606. // (1) Check whether lock is possible before any modification.
  1607. if(request.type !== P9_LOCK_TYPE_UNLCK && this.GetLock(id, request))
  1608. {
  1609. return P9_LOCK_BLOCKED;
  1610. }
  1611. // (2) Subtract requested region from locks of the same owner.
  1612. for(let i = 0; i < inode.locks.length; i++)
  1613. {
  1614. const region = inode.locks[i];
  1615. dbg_assert(region.length > 0,
  1616. "Filesystem: Found non-positive lock region length: " + region.length);
  1617. dbg_assert(region.type === P9_LOCK_TYPE_RDLCK || region.type === P9_LOCK_TYPE_WRLCK,
  1618. "Filesystem: Found invalid lock type: " + region.type);
  1619. dbg_assert(!inode.locks[i-1] || inode.locks[i-1].start <= region.start,
  1620. "Filesystem: Locks should be sorted by starting offset");
  1621. // Skip to requested region.
  1622. if(region.start + region.length <= request.start) continue;
  1623. // Check whether we've skipped past the requested region.
  1624. if(request.start + request.length <= region.start) break;
  1625. // Skip over locks of different owners.
  1626. if(region.proc_id !== request.proc_id || region.client_id !== request.client_id)
  1627. {
  1628. dbg_assert(!region.conflicts_with(request),
  1629. "Filesytem: Found conflicting lock region, despite already checked for conflicts");
  1630. continue;
  1631. }
  1632. // Pretend region would be split into parts 1 and 2.
  1633. const start1 = region.start;
  1634. const start2 = request.start + request.length;
  1635. const length1 = request.start - start1;
  1636. const length2 = region.start + region.length - start2;
  1637. if(length1 > 0 && length2 > 0 && region.type === request.type)
  1638. {
  1639. // Requested region is already locked with the required type.
  1640. // Return early - no need to modify anything.
  1641. return P9_LOCK_SUCCESS;
  1642. }
  1643. if(length1 > 0)
  1644. {
  1645. // Shrink from right / first half of the split.
  1646. region.length = length1;
  1647. }
  1648. if(length1 <= 0 && length2 > 0)
  1649. {
  1650. // Shrink from left.
  1651. region.start = start2;
  1652. region.length = length2;
  1653. }
  1654. else if(length2 > 0)
  1655. {
  1656. // Add second half of the split.
  1657. // Fast-forward to correct location.
  1658. while(i < inode.locks.length && inode.locks[i].start < start2) i++;
  1659. inode.locks.splice(i, 0,
  1660. this.DescribeLock(region.type, start2, length2, region.proc_id, region.client_id));
  1661. }
  1662. else if(length1 <= 0)
  1663. {
  1664. // Requested region completely covers this region. Delete.
  1665. inode.locks.splice(i, 1);
  1666. i--;
  1667. }
  1668. }
  1669. // (3) Insert requested lock region as a whole.
  1670. // No point in adding the requested lock region as fragmented bits in the above loop
  1671. // and having to merge them all back into one.
  1672. if(request.type !== P9_LOCK_TYPE_UNLCK)
  1673. {
  1674. let new_region = request;
  1675. let has_merged = false;
  1676. let i = 0;
  1677. // Fast-forward to requested position, and try merging with previous region.
  1678. for(; i < inode.locks.length; i++)
  1679. {
  1680. if(new_region.may_merge_after(inode.locks[i]))
  1681. {
  1682. inode.locks[i].length += request.length;
  1683. new_region = inode.locks[i];
  1684. has_merged = true;
  1685. }
  1686. if(request.start <= inode.locks[i].start) break;
  1687. }
  1688. if(!has_merged)
  1689. {
  1690. inode.locks.splice(i, 0, new_region);
  1691. i++;
  1692. }
  1693. // Try merging with the subsequent alike region.
  1694. for(; i < inode.locks.length; i++)
  1695. {
  1696. if(!inode.locks[i].is_alike(new_region)) continue;
  1697. if(inode.locks[i].may_merge_after(new_region))
  1698. {
  1699. new_region.length += inode.locks[i].length;
  1700. inode.locks.splice(i, 1);
  1701. }
  1702. // No more mergable regions after this.
  1703. break;
  1704. }
  1705. }
  1706. return P9_LOCK_SUCCESS;
  1707. };
  1708. FS.prototype.read_dir = function(path)
  1709. {
  1710. const p = this.SearchPath(path);
  1711. if(p.id === -1)
  1712. {
  1713. return undefined;
  1714. }
  1715. const dir = this.GetInode(p.id);
  1716. return Array.from(dir.direntries.keys()).filter(path => path !== "." && path !== "..");
  1717. };
  1718. FS.prototype.read_file = function(file)
  1719. {
  1720. const p = this.SearchPath(file);
  1721. if(p.id === -1)
  1722. {
  1723. return Promise.resolve(null);
  1724. }
  1725. const inode = this.GetInode(p.id);
  1726. return this.Read(p.id, 0, inode.size);
  1727. };