filesystem.js 55 KB

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