cheerpOS.js 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258
  1. // Copyright 2017-2021 Leaning Technologies Ltd. All Rights Reserved.
  2. // An array of {path:"/path",handler:handlerObj} ordered by the most specific to the least
  3. var cheerpjFSMounts = [];
  4. // An array of resources loaded from the runtime
  5. var cheerpjRuntimeResources = [];
  6. // Resource ranges loaded from the runtime
  7. var cosRuntimeResourcesRanges = {};
  8. var cosFileWatchPrefixes = {};
  9. function cheerpjFSInit(appUrlPrefix, jarJsOverridePath, loaderPath, useIframe)
  10. {
  11. // Keep these ordered from the most specific to the least
  12. cheerpjFSMounts.push(new CheerpJWebFolder("/app/", appUrlPrefix ? appUrlPrefix : "", jarJsOverridePath, false, DirectDownloader));
  13. cheerpjFSMounts.push(new CheerpJIndexedDBFolder("/files/"));
  14. cheerpjFSMounts.push(new CheerpJWebFolder("/lt/", loaderPath, null, true, useIframe ? IFrameProxyDownloader : DirectDownloader));
  15. cheerpjFSMounts.push(new CheerpJDataFolder("/str/"));
  16. cheerpjFSMounts.push(new CheerpJDevFolder("/dev/"));
  17. cheerpjFSMounts.push(new CheerpJDataFolder("/proc/"));
  18. cheerpjFSMounts.push(new CheerpJRootFolder("/"));
  19. cheerpOSAddStringFile("/proc/mounts", "none / ext4 rw,relatime 0 0\n");
  20. if(useIframe)
  21. ipfCreateIFrame(loaderPath);
  22. }
  23. function cheerpOSCreateWebFolder(appUrlPrefix)
  24. {
  25. return new CheerpJWebFolder("/app/", appUrlPrefix ? appUrlPrefix : "", null, false, DirectDownloader);
  26. }
  27. function cheerpOSNetInit(path_, tailscaleLoginUrlCb, tailscaleAuthKey, tailscaleControlUrl, tailscaleDnsIp, ipMap, tailscaleNetmapUpdateCb, cb)
  28. {
  29. self.cjEnableTailscale = true;
  30. import(path_).then((net) => {
  31. var state = net.State;
  32. var stateUpdate = (s) => {
  33. if (s == state.Running)
  34. {
  35. // Resolve promise
  36. return cb();
  37. }
  38. };
  39. var loginCb = tailscaleLoginUrlCb;
  40. if(loginCb == null)
  41. {
  42. // Use an empty callback by default
  43. loginCb = () => {};
  44. }
  45. var controlUrl = tailscaleControlUrl;
  46. if(controlUrl == null)
  47. {
  48. // Reset to undefined, it will be interpreted as the default Tailscale control plane
  49. controlUrl = undefined;
  50. }
  51. if (!ipMap)
  52. {
  53. // Reset to an empty object since when not used.
  54. ipMap = {}
  55. }
  56. net.autoConf({loginUrlCb: loginCb, stateUpdateCb: stateUpdate, netmapUpdateCb: tailscaleNetmapUpdateCb, authKey: tailscaleAuthKey, controlUrl: controlUrl, dnsIp: tailscaleDnsIp, ipMap: ipMap})
  57. .then((netExports) => {
  58. self.cjTailscaleSocket=netExports.tcpSocket;
  59. self.cjTailscaleUdpSocket=netExports.udpSocket;
  60. self.cjTailscaleParseIp=netExports.parseIP;
  61. self.cjTailscaleDumpIp=netExports.dumpIP;
  62. // NOTE: Not exporting resolve, which was used in CJ2
  63. netExports.up();
  64. });
  65. })
  66. }
  67. function cheerpOSNetDisable()
  68. {
  69. // explicit API call to the set the global state to false
  70. self.cjEnableTailscale = false;
  71. }
  72. function CheerpJFileData(parent, path, len, inodeId, permType, lastModified, uid, gid)
  73. {
  74. this.refCount = 0;
  75. this.cacheRefCount = 0;
  76. this.length = len | 0;
  77. this.dirty = 0;
  78. this.parent = parent;
  79. // path is local to the mount point
  80. this.path = path;
  81. this.inodeId = inodeId | 0;
  82. // data is linear not chunked data
  83. this.data = null;
  84. // chunks is for chunked data
  85. this.chunks = null;
  86. // mount contains the ops structure
  87. this.mount = null;
  88. // permissions and file type following 'stat' conventions
  89. this.permType = permType | 0;
  90. this.uid = uid;
  91. this.gid = gid;
  92. // last modification time, seconds from epoch
  93. this.lastModified = lastModified | 0;
  94. this.flags = 0;
  95. }
  96. CheerpJFileData.S_IFCHR = 0x2000;
  97. CheerpJFileData.S_IFDIR = 0x4000;
  98. CheerpJFileData.S_IFREG = 0x8000;
  99. CheerpJFileData.S_IFSOCK = 0xc000;
  100. CheerpJFileData.S_IFMT = 0xf000;
  101. CheerpJFileData.O_NONBLOCK = 0x800;
  102. function CheerpJFolder(mp)
  103. {
  104. this.mountPoint = mp;
  105. this.isSplit = false;
  106. // { statAsync, listAsync, makeFileData, createDirAsync, renameAsync, unlinkAsync }
  107. this.mountOps = null;
  108. // { readAsync, writeAsync, close };
  109. this.inodeOps = null;
  110. this.devId = CheerpJFolder.lastDevId;
  111. // Contains inodes
  112. this.fileCache = {};
  113. this.cacheThreads = {};
  114. // Contains CheerpJFileData entries
  115. this.inodeCache = [];
  116. CheerpJFolder.lastDevId = CheerpJFolder.lastDevId + 1|0
  117. }
  118. function folderDecRefCached(fileName, fileData)
  119. {
  120. fileData.cacheRefCount = fileData.cacheRefCount-1|0;
  121. if(fileData.cacheRefCount==0)
  122. {
  123. assert(this.inodeCache[fileData.inodeId]);
  124. delete this.inodeCache[fileData.inodeId];
  125. }
  126. }
  127. function folderGetCached(fileName)
  128. {
  129. var c = this.fileCache;
  130. if(!c.hasOwnProperty(fileName))
  131. return null;
  132. var inodeId = c[fileName];
  133. var ret = this.inodeCache[inodeId];
  134. assert(ret);
  135. return ret;
  136. }
  137. function folderSetCached(fileName, fileData)
  138. {
  139. var c = this.fileCache;
  140. fileData.cacheRefCount = fileData.cacheRefCount+1|0;
  141. if(c.hasOwnProperty(fileName))
  142. this.decRefCached(fileName, c[fileName]);
  143. var inodeId = fileData.inodeId;
  144. c[fileName] = inodeId;
  145. this.inodeCache[inodeId] = fileData;
  146. }
  147. function folderClearCached(fileName)
  148. {
  149. var c = this.fileCache;
  150. if(c.hasOwnProperty(fileName))
  151. {
  152. var inodeId = c[fileName];
  153. var data = this.inodeCache[inodeId];
  154. assert(data);
  155. this.decRefCached(fileName, data);
  156. delete c[fileName];
  157. }
  158. }
  159. CheerpJFolder.lastDevId = 1;
  160. CheerpJFolder.prototype.getCached = folderGetCached;
  161. CheerpJFolder.prototype.setCached = folderSetCached;
  162. CheerpJFolder.prototype.decRefCached = folderDecRefCached;
  163. CheerpJFolder.prototype.clearCached = folderClearCached;
  164. function CheerpJRootFolder(mp)
  165. {
  166. CheerpJFolder.call(this, mp);
  167. this.mountOps = RootOps;
  168. this.inodeOps = RootInodeOps;
  169. }
  170. CheerpJRootFolder.prototype = Object.create(CheerpJFolder.prototype);
  171. function rootStatAsync(mp, path, fileRef, cb)
  172. {
  173. if(path == "/")
  174. {
  175. fileRef.permType = CheerpJFileData.S_IFDIR | 0555;
  176. // Use the dev id as the inode id
  177. fileRef.inodeId = 0;
  178. return cb();
  179. }
  180. // Iterate over the mount points
  181. for(var i=0;i<cheerpjFSMounts.length;i++)
  182. {
  183. var f = cheerpjFSMounts[i];
  184. if(f.mountPoint == path + "/")
  185. {
  186. fileRef.permType = CheerpJFileData.S_IFDIR | 0555;
  187. // Use the dev id as the inode id
  188. fileRef.inodeId = f.devId;
  189. return cb();
  190. }
  191. }
  192. fileRef.permType = 0;
  193. return cb();
  194. }
  195. function rootListAsync(mp, path, fileRef, cb)
  196. {
  197. // Only the root itself can be listed
  198. if(path != "/"){
  199. return cb();
  200. }
  201. for(var i=0;i<cheerpjFSMounts.length-1;i++)
  202. {
  203. var f = cheerpjFSMounts[i];
  204. fileRef.push(f.mountPoint.substr(1, f.mountPoint.length - 2));
  205. }
  206. return cb();
  207. }
  208. function rootMakeFileData(mp, path, mode, uid, gid, cb)
  209. {
  210. // Only the root itself can be opened
  211. if(path != "/"){
  212. return cb(null);
  213. }
  214. var ret = new CheerpJFileData(mp, path, 0, 0, CheerpJFileData.S_IFDIR | 0777, /*lastModified*/0, /*uid*/0, /*gid*/0);
  215. ret.mount = mp.inodeOps;
  216. return cb(ret);
  217. }
  218. var RootOps = { statAsync: rootStatAsync, listAsync: rootListAsync, makeFileData: rootMakeFileData, createDirAsync: null, renameAsync: null, linkAsync: null, unlinkAsync: null };
  219. var RootInodeOps = { readAsync: null, writeAsync: null, close: null };
  220. function CheerpJWebFolder(mp, basePath, jarJSOverride, resTrace, downloader)
  221. {
  222. CheerpJFolder.call(this, mp);
  223. this.mountOps = WebOps;
  224. this.inodeOps = WebInodeOps;
  225. this.basePath = basePath;
  226. this.jarJSOverride = jarJSOverride;
  227. // Make this a member as it needs to be overridden for jnlp support
  228. this.mapPath = webMapPath;
  229. // We need to assign unique ids to each file, and they must persist over the lifetime of the application
  230. this.inodeMap = {}
  231. this.lastInode = 1;
  232. // True if we should keep track of required resource ranges
  233. this.resTrace = resTrace;
  234. this.downloader = downloader;
  235. this.chunkSize = 128 * 1024;
  236. }
  237. CheerpJWebFolder.prototype = Object.create(CheerpJFolder.prototype);
  238. function webGetInode(mp, path)
  239. {
  240. var ret = mp.inodeMap[path];
  241. if(ret !== undefined){
  242. return ret;
  243. }
  244. ret = {inodeId:mp.lastInode, permType:0, fileLength:0};
  245. mp.lastInode = mp.lastInode + 1|0;
  246. mp.inodeMap[path] = ret;
  247. return ret;
  248. }
  249. function DirectDownloader(url, metaDataOnly, chunkSize, rangeHeader)
  250. {
  251. this.url = url;
  252. this.metaDataOnly = metaDataOnly;
  253. this.responseURL = null;
  254. this.response = null;
  255. this.fileLength = -1;
  256. // The effective length of the downloaded data
  257. this.downloadLength = 0;
  258. this.failCount = 0;
  259. this.chunkSize = chunkSize;
  260. this.chunk = null;
  261. this.chunkOffset = 0;
  262. this.chunkCount = 0;
  263. this.rangeHeader = rangeHeader;
  264. }
  265. function cheerpOSSafeContentLength(contentRangeStr)
  266. {
  267. if(contentRangeStr == null)
  268. return -1;
  269. // Partial content with a Content-Range header
  270. if(!contentRangeStr.startsWith("bytes "))
  271. return -1;
  272. var segments = contentRangeStr.substr(6).split("/");
  273. if(segments.length != 2)
  274. return -1;
  275. return parseInt(segments[1]);
  276. }
  277. function ddlOnLoad(resp, downloader)
  278. {
  279. if(resp.status == 206)
  280. {
  281. downloader.responseURL = resp.url;
  282. downloader.lastModified = resp.headers.get("Last-Modified");
  283. if(downloader.responseURL.endsWith("/"))
  284. {
  285. downloader.fileLength = 0;
  286. downloader.onEnd(null);
  287. }
  288. else if(resp.headers.get("Content-Encoding") !== null)
  289. {
  290. // Be defensive against broken servers that mix up compression and partial content,
  291. // that is forbidden since it's ambiguous if the partial content is over the compressed
  292. // or uncompressed data
  293. ddlOnError(downloader, "HTTP server returned compressed partial data. That should not happen. CheerpJ cannot run.", /*fatal*/true);
  294. }
  295. else
  296. {
  297. var safeContentLength = cheerpOSSafeContentLength(resp.headers.get("Content-Range"));
  298. if(safeContentLength < 0)
  299. {
  300. // Partial content without Content-Range. The server is broken.
  301. ddlOnError(downloader, "HTTP server does not support the 'Content-Range' header. CheerpJ cannot run.", /*fatal*/true);
  302. }
  303. downloader.fileLength = safeContentLength;
  304. downloader.chunk = new Uint8Array(downloader.chunkSize);
  305. ddlOnData(downloader, resp.body.getReader());
  306. }
  307. return;
  308. }
  309. else if(resp.status == 200)
  310. {
  311. // It's only ever supposed to happen for directory redirects and index.list
  312. downloader.responseURL = resp.url;
  313. if(downloader.responseURL.endsWith("/"))
  314. {
  315. downloader.fileLength = 0;
  316. downloader.onEnd(null);
  317. }
  318. else
  319. {
  320. if(downloader.chunkSize > 0)
  321. {
  322. // Corner case, when the file is empty even reasonable servers (e.g. nginx) reply with 200
  323. if(resp.headers.get("Content-Length") != "0")
  324. {
  325. // We expected a chunk, we got a full response. The server is broken.
  326. ddlOnError(downloader, "HTTP server does not support the 'Range' header. CheerpJ cannot run.", /*fatal*/true);
  327. return;
  328. }
  329. }
  330. // Accumulate the data
  331. downloader.chunk = [];
  332. ddlOnData(downloader, resp.body.getReader());
  333. }
  334. downloader.lastModified = resp.headers.get("Last-Modified");
  335. return;
  336. }
  337. else if(resp.status == 416)
  338. {
  339. // Existing, but empty
  340. downloader.responseURL = resp.url;
  341. downloader.fileLength = 0;
  342. downloader.lastModified = resp.headers.get("Last-Modified");
  343. downloader.onEnd(null);
  344. return;
  345. }
  346. else if(resp.status == 204 || resp.status == 231 || resp.status == 403 || resp.status == 404)
  347. {
  348. downloader.responseURL = resp.url;
  349. if(resp.status == 231)
  350. downloader.responseURL += "/";
  351. downloader.onEnd(null);
  352. return;
  353. }
  354. else if(resp.status >= 500 && resp.status < 600)
  355. {
  356. ddlOnError(downloader, "Internal server error: " + resp.status, /*fatal*/false);
  357. return;
  358. }
  359. else
  360. {
  361. debugger;
  362. }
  363. }
  364. function ddlOnData(downloader, reader)
  365. {
  366. reader.read()
  367. .then(function(result){
  368. if(!result.done)
  369. {
  370. var data = result.value;
  371. if(downloader.chunkSize == 0)
  372. {
  373. downloader.chunk.push(data);
  374. downloader.downloadLength += data.length;
  375. }
  376. else
  377. {
  378. // Copy data in chunks
  379. var dataLen = data.length;
  380. var dataOff = 0;
  381. var chunkAvail = downloader.chunkSize - downloader.chunkOffset;
  382. while(dataLen > 0)
  383. {
  384. if(chunkAvail > dataLen)
  385. chunkAvail = dataLen;
  386. downloader.chunk.set(data.subarray(dataOff, dataOff + chunkAvail), downloader.chunkOffset);
  387. downloader.chunkOffset += chunkAvail;
  388. downloader.downloadLength += chunkAvail;
  389. dataOff += chunkAvail;
  390. dataLen -= chunkAvail;
  391. if(downloader.chunkOffset == downloader.chunkSize)
  392. {
  393. downloader.onData(new Uint8Array(downloader.chunk));
  394. downloader.chunkOffset = 0;
  395. chunkAvail = downloader.chunkSize;
  396. }
  397. }
  398. }
  399. ddlOnData(downloader, reader);
  400. }
  401. else
  402. {
  403. if(downloader.fileLength == -1)
  404. {
  405. downloader.fileLength = downloader.downloadLength;
  406. }
  407. if(downloader.chunkSize == 0)
  408. {
  409. downloader.onEnd(downloader.chunk);
  410. }
  411. else
  412. {
  413. if(downloader.chunkOffset != 0)
  414. downloader.onData(new Uint8Array(downloader.chunk.subarray(0, downloader.chunkOffset)));
  415. downloader.onEnd(null);
  416. }
  417. }
  418. });
  419. }
  420. function ddlOnError(downloader, err, fatal)
  421. {
  422. downloader.failCount++;
  423. if(downloader.failCount > 5 || fatal)
  424. {
  425. console.error("Network error for " + downloader.responseURL + ": " + err);
  426. downloader.onEnd(null);
  427. return;
  428. }
  429. // Try again
  430. downloader.send();
  431. }
  432. function ddlSend()
  433. {
  434. var downloader = this;
  435. var headers = {};
  436. var method = "GET";
  437. if(downloader.rangeHeader)
  438. headers["Range"] = downloader.rangeHeader;
  439. else if(downloader.metaDataOnly)
  440. headers["Range"] = "bytes=0-0";
  441. fetch(downloader.url, {"method": method, "mode": "cors", "headers" : headers})
  442. .then(function(response){
  443. ddlOnLoad(response, downloader);
  444. })
  445. .catch(function(err){
  446. ddlOnError(downloader, err);
  447. });
  448. }
  449. DirectDownloader.prototype.send = ddlSend;
  450. DirectDownloader.prototype.onData = function(value){};
  451. DirectDownloader.prototype.onEnd = function(allChunks){};
  452. function IFrameProxyDownloader(url, metaDataOnly, chunkSize, rangeHeader)
  453. {
  454. this.url = url;
  455. this.metaDataOnly = metaDataOnly;
  456. this.responseURL = null;
  457. this.response = null;
  458. this.fileLength = -1;
  459. this.chunkSize = chunkSize;
  460. this.chunk = null;
  461. this.chunkOffset = 0;
  462. this.rangeHeader = rangeHeader;
  463. assert(IFrameProxyDownloader.iframe);
  464. }
  465. function ipfMessage(e)
  466. {
  467. var d = e.data;
  468. var dl = IFrameProxyDownloader.pending[d.idx];
  469. dl.responseURL = d.responseURL;
  470. dl.fileLength = d.fileLength;
  471. if(d.from === "onData")
  472. {
  473. dl.onData(d.resp);
  474. }
  475. else if(d.from === "onEnd")
  476. {
  477. IFrameProxyDownloader.pending[d.idx] = null;
  478. dl.onEnd(d.resp);
  479. }
  480. return;
  481. }
  482. function ipfCreateIFrame(loaderPath)
  483. {
  484. var ret = document.createElement("iframe");
  485. ret.onload = function(e)
  486. {
  487. clearInterval(IFrameProxyDownloader.intervalId);
  488. var i = e.target;
  489. var c = new MessageChannel();
  490. var q = IFrameProxyDownloader.portOrQueue;
  491. c.port1.onmessage = ipfMessage;
  492. IFrameProxyDownloader.portOrQueue = c.port1;
  493. i.contentWindow.postMessage({t:"port",port:c.port2}, loaderPath, [c.port2]);
  494. // Dispatch pending loads
  495. for(var i=0;i<q.length;i=i+1|0)
  496. q[i].send();
  497. };
  498. ret.src = loaderPath + "/c.html";
  499. ret.width = "0px";
  500. ret.height = "0px";
  501. ret.style.border = "0px";
  502. ret.style.position = "fixed";
  503. ret.style.visibility = "hidden";
  504. IFrameProxyDownloader.iframe = ret;
  505. if(document.body)
  506. document.body.appendChild(ret);
  507. else
  508. document.addEventListener("DOMContentLoaded", function(e) { document.body.appendChild(IFrameProxyDownloader.iframe); })
  509. IFrameProxyDownloader.intervalId = setInterval(function(){
  510. IFrameProxyDownloader.iframe.src = loaderPath + "/c.html";
  511. }, 10000);
  512. }
  513. function ipfSend()
  514. {
  515. if(Array.isArray(IFrameProxyDownloader.portOrQueue))
  516. {
  517. // Not ready, add to queue
  518. IFrameProxyDownloader.portOrQueue.push(this);
  519. return;
  520. }
  521. var dict = {t: "load", url: this.url, metaDataOnly: this.metaDataOnly, chunkSize: this.chunkSize, rangeHeader: this.rangeHeader};
  522. for(var i = 0; i < IFrameProxyDownloader.pending.length; ++i)
  523. {
  524. if(IFrameProxyDownloader.pending[i] === null)
  525. {
  526. IFrameProxyDownloader.pending[i] = this;
  527. dict.idx = i;
  528. IFrameProxyDownloader.portOrQueue.postMessage(dict);
  529. return;
  530. }
  531. }
  532. IFrameProxyDownloader.pending.push(this);
  533. dict.idx = IFrameProxyDownloader.pending.length - 1;
  534. IFrameProxyDownloader.portOrQueue.postMessage(dict);
  535. }
  536. // Global data
  537. IFrameProxyDownloader.iframe = null;
  538. IFrameProxyDownloader.portOrQueue = [];
  539. IFrameProxyDownloader.pending = [];
  540. IFrameProxyDownloader.intervalId = 0;
  541. IFrameProxyDownloader.prototype.send = ipfSend;
  542. IFrameProxyDownloader.prototype.onData = function(value){};
  543. IFrameProxyDownloader.prototype.onEnd = function(allChunks){};
  544. function webStatAsyncImpl(mp, path, fileRef, onData, cb)
  545. {
  546. var ci = mp.inodeMap[path];
  547. if(ci !== undefined)
  548. {
  549. fileRef.fileLength = ci.fileLength | 0;
  550. fileRef.inodeId = ci.inodeId | 0;
  551. fileRef.permType = ci.permType | 0;
  552. return cb();
  553. }
  554. if(path == "")
  555. {
  556. var ci = webGetInode(mp, path);
  557. fileRef.inodeId = ci.inodeId;
  558. fileRef.permType = ci.permType = CheerpJFileData.S_IFDIR | 0555;
  559. return cb();
  560. }
  561. var url = mp.mapPath(mp, path);
  562. var rangeHeader = null;
  563. if(onData)
  564. {
  565. // The caller can also store data, download the first chunk
  566. // For small files we can avoid further requests by doing this
  567. rangeHeader = "bytes=0-"+(mp.chunkSize-1);
  568. }
  569. var dl = new mp.downloader(url, /*metaDataOnly*/true, mp.chunkSize, /*rangeHeader*/rangeHeader);
  570. if(onData)
  571. dl.onData = onData;
  572. dl.onEnd = function(allChunks) {
  573. var inodeId = 0;
  574. var permType = 0;
  575. if(this.responseURL && this.responseURL.endsWith("/"))
  576. {
  577. var ci = webGetInode(mp, path);
  578. inodeId = ci.inodeId;
  579. permType = ci.permType = CheerpJFileData.S_IFDIR | 0555;
  580. }
  581. else if(this.fileLength >= 0)
  582. {
  583. var ci = webGetInode(mp, path);
  584. inodeId = ci.inodeId;
  585. permType = ci.permType = CheerpJFileData.S_IFREG | 0444;
  586. fileRef.fileLength = ci.fileLength = this.fileLength;
  587. }
  588. fileRef.permType = permType | 0;
  589. fileRef.inodeId = inodeId | 0;
  590. if(this.lastModified)
  591. fileRef.lastModified = (Date.parse(this.lastModified) / 1000) | 0;
  592. else
  593. fileRef.lastModified = 0;
  594. return cb();
  595. };
  596. dl.send();
  597. }
  598. function webStatAsync(mp, path, fileRef, cb)
  599. {
  600. webStatAsyncImpl(mp, path, fileRef, null, cb);
  601. }
  602. function webMapPath(mp, path)
  603. {
  604. if(mp.resTrace)
  605. {
  606. var res = mp.mountPoint + path.substr(1);
  607. if(cosRuntimeResourcesRanges[res] === undefined)
  608. {
  609. cheerpjRuntimeResources.push(res);
  610. cosRuntimeResourcesRanges[res] = [];
  611. }
  612. }
  613. if(path.endsWith(".jar.js") && mp.jarJSOverride){
  614. return mp.jarJSOverride + path;
  615. }else{
  616. return mp.basePath + path;
  617. }
  618. }
  619. function webMakeFileData(mp, path, mode, uid, gid, cb)
  620. {
  621. if(mode != "r")
  622. return cb(null);
  623. var fileRef={}
  624. var chunks = [];
  625. function onData(value)
  626. {
  627. chunks.push(value);
  628. }
  629. webStatAsyncImpl(mp, path, fileRef, onData, function(){
  630. if(fileRef.permType === 0)
  631. return cb(null);
  632. var fileData = new CheerpJFileData(mp, path, fileRef.fileLength|0, fileRef.inodeId|0, fileRef.permType|0, fileRef.lastModified, /*uid*/0, /*gid*/0);
  633. fileData.mount = mp.inodeOps;
  634. fileData.chunks = chunks;
  635. if((fileRef.permType & CheerpJFileData.S_IFMT) != CheerpJFileData.S_IFREG){
  636. return cb(fileData);
  637. }
  638. // Prepare chunks
  639. var numChunks = ((fileRef.fileLength + (mp.chunkSize-1)) / mp.chunkSize) | 0;
  640. // NOTE: The first chunk might be already populated
  641. for(var i=chunks.length;i<numChunks;i++)
  642. chunks[i] = null;
  643. return cb(fileData);
  644. });
  645. }
  646. function webReadAsync(fileData, fileOffset, buf, off, len, flags, cb)
  647. {
  648. assert(fileOffset <= fileData.length);
  649. if(fileOffset + len > fileData.length) len = fileData.length - fileOffset;
  650. // EOF
  651. if(len == 0){
  652. return cb(0);
  653. }
  654. var mp = fileData.parent;
  655. var chunkSize = mp.chunkSize;
  656. var startChunk = fileOffset / chunkSize | 0;
  657. var endChunk = (fileOffset + len - 1) / chunkSize | 0;
  658. var fileRef = {pendingLoads: 0};
  659. if(mp.resTrace){
  660. var startOffset = startChunk * chunkSize;
  661. var endOffset = (endChunk + 1) * chunkSize
  662. var res = mp.mountPoint + fileData.path.substr(1);
  663. // Keep the array ordered and remove duplicates, ranges are exclusive
  664. var ranges = cosRuntimeResourcesRanges[res];
  665. var i = 0;
  666. for(;i<ranges.length;i+=2)
  667. {
  668. if(ranges[i] < startOffset)
  669. continue;
  670. break;
  671. }
  672. if(i > 0 && ranges[i-1] >= startOffset)
  673. {
  674. // Merge on the left
  675. if(ranges[i-1] < endOffset)
  676. {
  677. ranges[i-1] = endOffset;
  678. // Check if we also need to merge on the right and remove an entry
  679. if(ranges[i] <= endOffset)
  680. {
  681. if(ranges[i+1] > endOffset)
  682. {
  683. ranges[i-1] = ranges[i+1];
  684. }
  685. ranges.splice(i, 2);
  686. }
  687. }
  688. }
  689. else if(i != ranges.length && ranges[i] <= endOffset)
  690. {
  691. // Merge on the right
  692. ranges[i] = startOffset;
  693. }
  694. else
  695. {
  696. // Add an entry
  697. ranges.splice(i, 0, startOffset, endOffset);
  698. }
  699. // Sanity checks
  700. for(var i=1;i<ranges.length;i++)
  701. {
  702. assert(ranges[i] > ranges[i-1]);
  703. }
  704. }
  705. return webReadAsyncLoop(fileData, fileOffset, buf, off, len, fileRef, startChunk, endChunk, cb);
  706. }
  707. function webBuildRange(fileData, chunkStart, chunkEnd)
  708. {
  709. var startByte = chunkStart * fileData.parent.chunkSize;
  710. var endByte = ((chunkEnd + 1) * fileData.parent.chunkSize) - 1;
  711. if(endByte >= fileData.length)
  712. endByte = fileData.length - 1;
  713. return "bytes="+startByte+"-"+endByte;
  714. }
  715. function webDownloadRange(fileData, fileOffset, buf, off, len, startChunk, endChunk, fileRef, cb)
  716. {
  717. var url = fileData.path;
  718. var rangeHeader = webBuildRange(fileData, startChunk, endChunk);
  719. url = webMapPath(fileData.parent, url);
  720. var dl = new fileData.parent.downloader(url, /*metaDataOnly*/false, fileData.parent.chunkSize, rangeHeader);
  721. dl.curChunk = startChunk;
  722. dl.onData = function(value)
  723. {
  724. var tmp = fileData.chunks[this.curChunk];
  725. fileData.chunks[this.curChunk] = value;
  726. this.curChunk++;
  727. for(var k = 0; k < tmp.length; k++)
  728. {
  729. tmp[k]();
  730. }
  731. // NOTE: pendingLoads can go negative due to read-ahead
  732. fileRef.pendingLoads--;
  733. if(fileRef.pendingLoads !== 0)
  734. return;
  735. webReadAsyncEnd(fileData, fileOffset, buf, off, len, cb);
  736. };
  737. dl.send();
  738. }
  739. function webReadAsyncLoop(fileData, fileOffset, buf, off, len, fileRef, startChunk, endChunk, cb)
  740. {
  741. var pendingStartChunk = startChunk;
  742. for(var curChunk=startChunk;curChunk<=endChunk;curChunk++){
  743. var curData = fileData.chunks[curChunk];
  744. if(curData)
  745. {
  746. if(pendingStartChunk < curChunk)
  747. {
  748. // Download a range up to this chunk, which we already have
  749. webDownloadRange(fileData, fileOffset, buf, off, len, pendingStartChunk, curChunk - 1, fileRef, cb)
  750. }
  751. pendingStartChunk = curChunk + 1;
  752. if(curData instanceof Uint8Array)
  753. {
  754. continue;
  755. }
  756. else
  757. {
  758. // The double increment here is required to avoid waking up threads too early
  759. // due to the read-ahead block arriving sooner
  760. fileRef.pendingLoads+=2;
  761. curData.push(function()
  762. {
  763. // If the read-ahead has already completed we will reach -1
  764. fileRef.pendingLoads-=2;
  765. if(fileRef.pendingLoads > 0)
  766. return;
  767. webReadAsyncEnd(fileData, fileOffset, buf, off, len, cb);
  768. });
  769. }
  770. }
  771. else
  772. {
  773. // Nothing to do, accumulate the range
  774. fileRef.pendingLoads++;
  775. fileData.chunks[curChunk] = [];
  776. }
  777. }
  778. // Do read-ahead, but do not wait for the results
  779. var fwdAhead = endChunk + 1;
  780. var bwdAhead = startChunk - 1;
  781. var bwdDownloader = null;
  782. if(fwdAhead < fileData.chunks.length && fileData.chunks[fwdAhead] == null)
  783. {
  784. // Extend the range, but don't increment pendingLoads
  785. endChunk = fwdAhead;
  786. fileData.chunks[fwdAhead] = [];
  787. }
  788. else if(bwdAhead >= 0 && fileData.chunks[bwdAhead] == null)
  789. {
  790. var url = fileData.path;
  791. var rangeHeader = webBuildRange(fileData, bwdAhead, bwdAhead);
  792. url = webMapPath(fileData.parent, url);
  793. bwdDownloader = new fileData.parent.downloader(url, /*metaDataOnly*/false, fileData.parent.chunkSize, rangeHeader);
  794. fileData.chunks[bwdAhead] = [];
  795. bwdDownloader.curChunk = bwdAhead;
  796. bwdDownloader.onData = function(value)
  797. {
  798. var tmp = fileData.chunks[this.curChunk];
  799. fileData.chunks[this.curChunk] = value;
  800. for(var k = 0; k < tmp.length; k++)
  801. {
  802. tmp[k]();
  803. }
  804. };
  805. }
  806. if(pendingStartChunk <= endChunk)
  807. {
  808. webDownloadRange(fileData, fileOffset, buf, off, len, pendingStartChunk, endChunk, fileRef, cb)
  809. }
  810. if(bwdDownloader)
  811. {
  812. bwdDownloader.send();
  813. }
  814. if(fileRef.pendingLoads !== 0){
  815. return;
  816. }
  817. return webReadAsyncEnd(fileData, fileOffset, buf, off, len, cb);
  818. }
  819. function webReadAsyncEnd(fileData, fileOffset, buf, off, len, cb)
  820. {
  821. if(buf.length == 0)
  822. {
  823. // Defend against buffer invalidation
  824. return cb(/*EFAULT*/-14);
  825. }
  826. // All chunks are now loaded
  827. var chunkSize = fileData.parent.chunkSize;
  828. var curChunk = fileOffset / chunkSize | 0;
  829. var curOffset = fileOffset - (curChunk*chunkSize) | 0;
  830. var i=0;
  831. while(i<len)
  832. {
  833. var c = fileData.chunks[curChunk];
  834. if((len-i) < (chunkSize-curOffset))
  835. chunkSize = curOffset+(len-i);
  836. for(var j=curOffset;j<chunkSize;j++)
  837. {
  838. buf[off+i>>>0]=c[j];
  839. i++;
  840. }
  841. curChunk++;
  842. curOffset=0;
  843. }
  844. assert(i===len);
  845. return cb(len);
  846. }
  847. function webListAsync(mp, path, fileRef, cb)
  848. {
  849. var url = mp.mapPath(mp, path+"/index.list");
  850. var dl = new mp.downloader(url, /*metaDataOnly*/false, /*chunkSize*/0, /*rangeHeader*/null);
  851. dl.fileRef = fileRef;
  852. dl.onEnd = function(allChunks)
  853. {
  854. if(allChunks != null)
  855. {
  856. var res = "";
  857. for(var i=0;i<allChunks.length;i++)
  858. {
  859. var chunk = allChunks[i];
  860. var chunkLen = chunk.length;
  861. for(var j=0;j<chunkLen;j++)
  862. res += String.fromCharCode(chunk[j]);
  863. }
  864. var files = res.split("\n");
  865. var filesLen = files.length;
  866. for(var i=0;i<filesLen;i++)
  867. {
  868. if(files[i].length)
  869. fileRef.push(files[i]);
  870. }
  871. }
  872. cb();
  873. };
  874. dl.send();
  875. }
  876. var WebOps = { statAsync: webStatAsync, listAsync: webListAsync, makeFileData: webMakeFileData, createDirAsync: null, renameAsync: null, linkAsync: null, unlinkAsync: null };
  877. var WebInodeOps = { readAsync: webReadAsync, writeAsync: null, close: null };
  878. function CheerpJIndexedDBFolder(mp)
  879. {
  880. CheerpJFolder.call(this, mp);
  881. this.mountOps = IdbOps;
  882. this.inodeOps = IdbInodeOps;
  883. this.isSplit = true;
  884. this.dbConnection = null;
  885. this.dEntries = {};
  886. }
  887. CheerpJIndexedDBFolder.prototype = Object.create(CheerpJFolder.prototype);
  888. function idbTrap(e)
  889. {
  890. debugger
  891. }
  892. function idbStatAsync(mp, path, fileRef, cb)
  893. {
  894. idbEnsureDBConnection(mp, function()
  895. {
  896. // We get here after dbConnection is valid
  897. assert(path[path.length-1]!='/');
  898. var tx = mp.dbConnection.transaction("files", "readonly");
  899. var store = tx.objectStore("files");
  900. // Find the directory
  901. var req = store.get(path);
  902. assert(req.readyState != "done");
  903. req.fileRef = fileRef;
  904. req.onerror = idbTrap;
  905. function gotRes(fileRef, res)
  906. {
  907. if(!res)
  908. {
  909. fileRef.permType = 0;
  910. }
  911. else
  912. {
  913. fileRef.inodeId = res.inodeId;
  914. fileRef.uid = res.uid;
  915. fileRef.gid = res.gid;
  916. if(res.hasOwnProperty("permType"))
  917. fileRef.permType = res.permType;
  918. else if(res.type == "dir")
  919. fileRef.permType = CheerpJFileData.S_IFDIR | 0777;
  920. else if(res.type == "s")
  921. fileRef.permType = CheerpJFileData.S_IFSOCK | 0666;
  922. else
  923. {
  924. assert(res.type == "file");
  925. fileRef.permType = CheerpJFileData.S_IFREG | 0666;
  926. }
  927. if(res.type == "file")
  928. fileRef.fileLength = res.contents == null ? 0 : res.contents.length;
  929. else if(res.type == "special")
  930. fileRef.contents = res.contents;
  931. fileRef.lastModified = res.lastModified|0;
  932. }
  933. return cb();
  934. }
  935. req.onsuccess = function(e)
  936. {
  937. this.onerror = null;
  938. this.onsuccess = null;
  939. var res = this.result;
  940. if(typeof(res) == "number")
  941. {
  942. assert(store != null);
  943. var req = store.get(res);
  944. assert(req.readyState != "done");
  945. req.fileRef = this.fileRef;
  946. req.onerror = idbTrap;
  947. req.onsuccess = function(e)
  948. {
  949. this.onerror = null;
  950. this.onsuccess = null;
  951. var res = this.result;
  952. return gotRes(this.fileRef, res);
  953. };
  954. return;
  955. }
  956. return gotRes(this.fileRef, res);
  957. }
  958. });
  959. }
  960. function idbListAsync(mp,path, fileRef, cb)
  961. {
  962. idbEnsureDBConnection(mp, function()
  963. {
  964. // We get here after dbConnection is valid
  965. assert(path[path.length-1]!='/');
  966. var tx = mp.dbConnection.transaction("files", "readonly");
  967. var store = tx.objectStore("files");
  968. // Find the directory
  969. var req = store.get(path);
  970. assert(req.readyState != "done");
  971. req.fileRef = fileRef;
  972. req.onerror = idbTrap;
  973. req.onsuccess = function(e)
  974. {
  975. this.onerror = null;
  976. this.onsuccess = null;
  977. var res = this.result;
  978. if(res && res.type == "dir")
  979. {
  980. var c = res.contents;
  981. for(var i=0;i<c.length;i++)
  982. fileRef.push(c[i].substr(1));
  983. }
  984. // If there is no event, this is an sync handling
  985. if(e){
  986. return cb();
  987. }
  988. }
  989. if(req.readyState == "done"){
  990. req.onsuccess(null);
  991. return cb();
  992. }else{
  993. return;
  994. }
  995. });
  996. }
  997. function idbCreateDirAsync(mp, path, fileRef, mode, uid, gid, cb)
  998. {
  999. idbEnsureDBConnection(mp, function(){
  1000. // We get here after dbConnection is valid
  1001. assert(path[path.length-1]!='/');
  1002. // Find where the parent directory ends
  1003. var parentEnd = path.lastIndexOf('/');
  1004. assert(parentEnd >= 0);
  1005. var parentPath = path.substring(0, parentEnd);
  1006. var tx = mp.dbConnection.transaction("files", "readwrite");
  1007. var store = tx.objectStore("files");
  1008. function gotParent(res, asyncThread)
  1009. {
  1010. if(!res)
  1011. fileRef.exists = 0;
  1012. else
  1013. fileRef.exists = (res.type == "dir" ? 5 : 3);
  1014. if(fileRef.exists !== 5)
  1015. {
  1016. // Parent directory does not exist or it's not a directory
  1017. if(asyncThread)
  1018. {
  1019. fileRef.exists = 0;
  1020. return asyncThread();
  1021. }
  1022. return;
  1023. }
  1024. res = idbAddDEntry(mp, parentPath, res);
  1025. // Add this new object to the parent Path
  1026. var childPath = path.substring(parentPath.length);
  1027. assert(childPath[0]=='/');
  1028. for(var i=0;i<res.contents.length;i++)
  1029. {
  1030. if(res.contents[i] == childPath)
  1031. {
  1032. if(asyncThread)
  1033. {
  1034. fileRef.exists = 5;
  1035. return asyncThread();
  1036. }
  1037. return;
  1038. }
  1039. }
  1040. res.contents.push(childPath);
  1041. store.put(res, parentPath);
  1042. var cachedDE = idbCheckDEntry(mp, "");
  1043. var curInodeId = cachedDE.nextInode++;
  1044. store.put(cachedDE, "");
  1045. store.put({ type: "dir", contents:[], inodeId: curInodeId, permType: CheerpJFileData.S_IFDIR | (mode & 01777), uid: uid, gid: gid, lastModified: (Date.now() / 1000)|0}, path);
  1046. if(asyncThread)
  1047. {
  1048. fileRef.exists = 5;
  1049. return asyncThread();
  1050. }
  1051. }
  1052. var cachedDE = idbCheckDEntry(mp, parentPath);
  1053. if(cachedDE)
  1054. {
  1055. gotParent(cachedDE, null);
  1056. return cb();
  1057. }
  1058. else
  1059. {
  1060. // Find the parent
  1061. var req = store.get(parentPath);
  1062. if(req.readyState == "done")
  1063. {
  1064. gotParent(req.result, null);
  1065. return cb();
  1066. }
  1067. else
  1068. {
  1069. req.onerror = idbTrap;
  1070. req.onsuccess = function(e) { this.onerror = null; this.onsuccess = null; gotParent(this.result, cb); }
  1071. return;
  1072. }
  1073. }
  1074. });
  1075. }
  1076. function idbCheckDEntry(mp, path)
  1077. {
  1078. // Does it exists already?
  1079. var ret = mp.dEntries[path];
  1080. if(!ret){
  1081. return null;
  1082. }
  1083. // Yep, mark it for LRU
  1084. ret.ts = Date.now();
  1085. return ret.res;
  1086. }
  1087. function idbAddDEntry(mp, path, res)
  1088. {
  1089. var ret = idbCheckDEntry(mp, path);
  1090. if(ret){
  1091. return ret;
  1092. }
  1093. mp.dEntries[path] = { res: res, ts: Date.now() };
  1094. return res;
  1095. }
  1096. function idbDelDEntry(mp, path)
  1097. {
  1098. delete mp.dEntries[path];
  1099. }
  1100. function idbDecRefIndirect(store, fileRef)
  1101. {
  1102. fileRef.refCount = fileRef.refCount-1|0;
  1103. if(fileRef.refCount == 0)
  1104. store.delete(fileRef.inodeId);
  1105. else
  1106. store.put(fileRef, fileRef.inodeId);
  1107. }
  1108. function idbResolve(mp, path, fileRef, cb)
  1109. {
  1110. var parentPath = null;
  1111. // We get here after dbConnection is valid
  1112. if(path.length == 0)
  1113. {
  1114. // Getting the root directory, special case this
  1115. parentPath = path;
  1116. }
  1117. else
  1118. {
  1119. assert(path[path.length-1]!='/');
  1120. // Find where the parent directory ends
  1121. var parentEnd = path.lastIndexOf('/');
  1122. assert(parentEnd >= 0);
  1123. parentPath = path.substring(0, parentEnd);
  1124. }
  1125. function gotFile(parentRes, res, asyncThread, store)
  1126. {
  1127. if(typeof(res) == "number")
  1128. {
  1129. assert(store != null);
  1130. assert(asyncThread != null);
  1131. var req = store.get(res);
  1132. assert(req.readyState != "done");
  1133. req.onerror = idbTrap;
  1134. req.onsuccess = function(e)
  1135. {
  1136. this.onerror = null;
  1137. this.onsuccess = null;
  1138. var res = this.result;
  1139. gotFile(parentRes, res, asyncThread, store);
  1140. }
  1141. return;
  1142. }
  1143. fileRef.parentPath = parentPath;
  1144. fileRef.parentRes = parentRes;
  1145. fileRef.res = res;
  1146. if(asyncThread)
  1147. {
  1148. return asyncThread();
  1149. }
  1150. }
  1151. function gotParent(res, asyncThread, store)
  1152. {
  1153. if(!res || res.type != "dir")
  1154. {
  1155. // Parent directory does not exist or it's not a directory
  1156. fileRef.fileData = null;
  1157. if(asyncThread)
  1158. {
  1159. return asyncThread();
  1160. }
  1161. return false;
  1162. }
  1163. var parentRes = idbAddDEntry(mp, parentPath, res);
  1164. // The file may already exists
  1165. var filePath = path.substr(parentPath.length);
  1166. var fileIndex = res.contents.indexOf(filePath);
  1167. if(filePath == "")
  1168. {
  1169. gotFile(parentRes, parentRes, asyncThread, store);
  1170. return false;
  1171. }
  1172. else if(fileIndex < 0)
  1173. {
  1174. gotFile(parentRes, null, asyncThread, store);
  1175. return false;
  1176. }
  1177. else
  1178. {
  1179. if(store == null)
  1180. {
  1181. var tx = mp.dbConnection.transaction("files", "readonly");
  1182. var store = tx.objectStore("files");
  1183. }
  1184. var req = store.get(path);
  1185. assert(req.readyState != "done");
  1186. req.onerror = idbTrap;
  1187. req.onsuccess = function(e)
  1188. {
  1189. this.onerror = null;
  1190. this.onsuccess = null;
  1191. var res = this.result;
  1192. gotFile(parentRes, res, asyncThread ? asyncThread : cb, store);
  1193. }
  1194. return true;
  1195. }
  1196. }
  1197. var cachedDE = idbCheckDEntry(mp, parentPath);
  1198. var doAsync = false;
  1199. if(cachedDE)
  1200. {
  1201. doAsync = gotParent(cachedDE, null, null);
  1202. }
  1203. else
  1204. {
  1205. // Find the parent
  1206. var tx = mp.dbConnection.transaction("files", "readonly");
  1207. var store = tx.objectStore("files");
  1208. var req = store.get(parentPath);
  1209. assert(req.readyState != "done");
  1210. if(req.readyState == "done")
  1211. doAsync = gotParent(null, null, store);
  1212. else
  1213. {
  1214. req.onerror = idbTrap;
  1215. req.onsuccess = function(e) { this.onerror = null; this.onsuccess = null; gotParent(this.result, cb, this.source); }
  1216. doAsync = true;
  1217. assert(req.readyState != "done");
  1218. }
  1219. }
  1220. if(doAsync)
  1221. {
  1222. return;
  1223. }
  1224. return cb();
  1225. }
  1226. function idbMakeFileData(mp, path, mode, uid, gid, cb)
  1227. {
  1228. assert(mp.isSplit);
  1229. var fileRef={fileData:null, parentPath: null, parentRes:null, res:null};
  1230. idbEnsureDBConnection(mp, function(){
  1231. idbResolve(mp, path, fileRef, function(){
  1232. var tx = mp.dbConnection.transaction("files", "readwrite");
  1233. var store = tx.objectStore("files");
  1234. if(fileRef.parentRes == null)
  1235. {
  1236. // Parent directory does not exists
  1237. return cb(null);
  1238. }
  1239. else
  1240. {
  1241. var parentPath = fileRef.parentPath;
  1242. var parentRes = fileRef.parentRes;
  1243. var res = fileRef.res;
  1244. var fileExists = res && (res.type == "file" || res.type == "dir");
  1245. var fileIsIndirect = res && res.hasOwnProperty("refCount");
  1246. if(mode == "r" || ((mode == "r+" || mode == "w") && fileExists))
  1247. {
  1248. if(fileExists)
  1249. {
  1250. if(res.type == "file")
  1251. {
  1252. var data = res.contents;
  1253. var chunks = [];
  1254. if(mode == "w")
  1255. data = null;
  1256. else if(data)
  1257. {
  1258. var cur = 0;
  1259. var len = data.length;
  1260. var chunkSize = 1024*1024;
  1261. while(cur < len)
  1262. {
  1263. var thisChunkSize = (len - cur) < chunkSize ? len - cur : chunkSize;
  1264. var c = new Uint8Array(chunkSize);
  1265. c.set(data.subarray(cur, cur+thisChunkSize));
  1266. chunks.push(c);
  1267. cur += thisChunkSize;
  1268. }
  1269. assert(cur == len);
  1270. }
  1271. var permType = res.permType ? res.permType : CheerpJFileData.S_IFREG | 0666;
  1272. if(mp.inodeCache[res.inodeId])
  1273. {
  1274. fileRef.fileData = mp.inodeCache[res.inodeId];
  1275. fileRef.path = null;
  1276. }
  1277. else
  1278. {
  1279. fileRef.fileData = new CheerpJFileData(mp, fileIsIndirect ? null : path, data?data.length:0, res.inodeId, permType, res.lastModified|0, res.uid, res.gid);
  1280. fileRef.fileData.mount = mp.inodeOps;
  1281. fileRef.fileData.chunks = chunks;
  1282. }
  1283. if(mode == "w")
  1284. fileRef.fileData.dirty = 1;
  1285. }
  1286. else if(res.type == "dir")
  1287. {
  1288. // It is legal to get an fd to a directory
  1289. var permType = res.permType ? res.permType : CheerpJFileData.S_IFDIR | 0777;
  1290. // It not legal to have multiple names for the same directory
  1291. assert(!mp.inodeCache[res.inodeId]);
  1292. fileRef.fileData = new CheerpJFileData(mp, path, 0, res.inodeId, permType, res.lastModified|0, res.uid, res.gid);
  1293. fileRef.fileData.mount = mp.inodeOps;
  1294. }
  1295. }
  1296. else
  1297. fileRef.fileData = null;
  1298. }
  1299. else if(mode == "r+" || mode == "w")
  1300. {
  1301. // w is truncate, so we always update the file
  1302. assert(!fileExists);
  1303. {
  1304. // Add this new object to the parent Path
  1305. var childPath = path.substring(parentPath.length);
  1306. assert(childPath[0]=='/');
  1307. assert(parentRes.contents);
  1308. parentRes.contents.push(childPath);
  1309. store.put(parentRes, parentPath);
  1310. }
  1311. // TODO: Check if the file is actually a dir
  1312. var cachedDE = idbCheckDEntry(mp, "");
  1313. var curInodeId = cachedDE.nextInode++;
  1314. store.put(cachedDE, "");
  1315. var lastModified = (Date.now() / 1000)|0;
  1316. store.put({ type: "file", contents:null, inodeId: curInodeId, uid: uid, gid: gid, lastModified: lastModified }, path);
  1317. fileRef.fileData = new CheerpJFileData(mp, path, 0, curInodeId, CheerpJFileData.S_IFREG | 0666, lastModified, uid, gid);
  1318. fileRef.fileData.dirty = 1;
  1319. fileRef.fileData.mount = mp.inodeOps;
  1320. fileRef.fileData.chunks = [];
  1321. }
  1322. else if(mode == "s")
  1323. {
  1324. if(fileExists)
  1325. fileRef.fileData = null;
  1326. else
  1327. {
  1328. // Add this new object to the parent Path
  1329. var childPath = path.substring(parentPath.length);
  1330. assert(childPath[0]=='/');
  1331. assert(parentRes.contents);
  1332. parentRes.contents.push(childPath);
  1333. store.put(parentRes, parentPath);
  1334. var cachedDE = idbCheckDEntry(mp, "");
  1335. var curInodeId = cachedDE.nextInode++;
  1336. store.put(cachedDE, "");
  1337. var lastModified = (Date.now() / 1000)|0;
  1338. store.put({ type: "special", contents:null, inodeId: curInodeId, uid: uid, gid: gid, lastModified: lastModified }, path);
  1339. fileRef.fileData = new CheerpJFileData(mp, path, 0, curInodeId, CheerpJFileData.S_IFSOCK | 0666, lastModified, uid, gid);
  1340. fileRef.fileData.dirty = 1;
  1341. fileRef.fileData.mount = mp.inodeOps;
  1342. }
  1343. }
  1344. else
  1345. debugger
  1346. }
  1347. // The returned value may be null if the file can't be created
  1348. return cb(fileRef.fileData);
  1349. });
  1350. });
  1351. }
  1352. function idbRenameAsync(mp, srcPath, dstPath, cb)
  1353. {
  1354. idbEnsureDBConnection(mp, function(){
  1355. // The whole operation should be atomic, using a single transaction should do it
  1356. // TODO: What about the file cache atomicity? That is handled in the caller.
  1357. var srcFileRef={fileData:null, parentPath: null, parentRes:null, res:null};
  1358. idbResolve(mp, srcPath, srcFileRef, function(){
  1359. if(srcFileRef.res == null){
  1360. // File does not exists
  1361. return cb(0);
  1362. }
  1363. var dstFileRef={fileData:null, parentPath: null, parentRes:null, res:null};
  1364. idbResolve(mp, dstPath, dstFileRef, function()
  1365. {
  1366. assert(dstFileRef.res == null || !dstFileRef.res.hasOwnProperty("refCount"));
  1367. if(dstFileRef.parentRes == null){
  1368. // Parent directory does not exists
  1369. return cb(0);
  1370. }
  1371. // TODO: Support renaming directories
  1372. if(srcFileRef.res.type == "dir"){
  1373. return cb(0);
  1374. }
  1375. var tx = mp.dbConnection.transaction("files", "readwrite");
  1376. var store = tx.objectStore("files");
  1377. // 1) Remove src file from parent directory
  1378. var srcChildPath = srcPath.substr(srcFileRef.parentPath.length);
  1379. var srcParentIndex = srcFileRef.parentRes.contents.indexOf(srcChildPath);
  1380. assert(srcParentIndex >= 0);
  1381. srcFileRef.parentRes.contents.splice(srcParentIndex, 1);
  1382. store.put(srcFileRef.parentRes, srcFileRef.parentPath);
  1383. // 2) Remove src from the DB
  1384. store.delete(srcPath);
  1385. // 3) Add the file to the dst directory
  1386. var dstChildPath = dstPath.substr(dstFileRef.parentPath.length);
  1387. var dstParentIndex = dstFileRef.parentRes.contents.indexOf(dstChildPath);
  1388. if(dstParentIndex < 0){
  1389. dstFileRef.parentRes.contents.push(dstChildPath);
  1390. store.put(dstFileRef.parentRes, dstFileRef.parentPath);
  1391. }
  1392. // 4) Store the contents to the dst path
  1393. store.put(srcFileRef.res, dstPath);
  1394. tx.oncomplete = function(e){
  1395. return cb(1);
  1396. }
  1397. });
  1398. });
  1399. });
  1400. }
  1401. function idbLinkAsync(mp, srcPath, dstPath, cb)
  1402. {
  1403. idbEnsureDBConnection(mp, function(){
  1404. // The whole operation should be atomic, using a single transaction should do it
  1405. var srcFileRef={fileData:null, parentPath: null, parentRes:null, res:null};
  1406. idbResolve(mp, srcPath, srcFileRef, function(){
  1407. if(srcFileRef.res == null){
  1408. // File does not exists
  1409. return cb(0);
  1410. }
  1411. var dstFileRef={fileData:null, parentPath: null, parentRes:null, res:null};
  1412. idbResolve(mp, dstPath, dstFileRef, function()
  1413. {
  1414. if(dstFileRef.res != null){
  1415. // Dest file already exists
  1416. return cb(0);
  1417. }
  1418. if(dstFileRef.parentRes == null){
  1419. // Parent directory does not exists
  1420. return cb(0);
  1421. }
  1422. // Hard-linking directories is not supported
  1423. if(srcFileRef.res.type == "dir"){
  1424. return cb(0);
  1425. }
  1426. var tx = mp.dbConnection.transaction("files", "readwrite");
  1427. var store = tx.objectStore("files");
  1428. // 1) Check if srcFileRef needs to become indirect
  1429. if(!srcFileRef.res.hasOwnProperty("refCount")){
  1430. var res = srcFileRef.res;
  1431. var inodeId = res.inodeId;
  1432. assert(inodeId);
  1433. store.put(inodeId, srcPath);
  1434. res.refCount = 1;
  1435. var cachedFileData = mp.inodeCache[inodeId];
  1436. if(cachedFileData){
  1437. assert(cachedFileData.path);
  1438. cachedFileData.path = null;
  1439. }
  1440. }
  1441. // 2) Add the file to the dst directory
  1442. var dstChildPath = dstPath.substr(dstFileRef.parentPath.length);
  1443. var dstParentIndex = dstFileRef.parentRes.contents.indexOf(dstChildPath);
  1444. assert(dstParentIndex < 0);
  1445. dstFileRef.parentRes.contents.push(dstChildPath);
  1446. store.put(dstFileRef.parentRes, dstFileRef.parentPath);
  1447. // 3) Store the contents to the dst path, as indirect
  1448. srcFileRef.res.refCount = srcFileRef.res.refCount+1|0;
  1449. store.put(srcFileRef.res, srcFileRef.res.inodeId);
  1450. store.put(srcFileRef.res.inodeId, dstPath);
  1451. tx.oncomplete = function(e){
  1452. return cb(1);
  1453. }
  1454. });
  1455. });
  1456. });
  1457. }
  1458. function idbReadAsync(fileData, fileOffset, buf, off, len, flags, cb)
  1459. {
  1460. var chunkSize = 1024*1024;
  1461. var curChunk = fileOffset / chunkSize | 0;
  1462. if(fileOffset + len > fileData.length)
  1463. len = fileData.length - fileOffset;
  1464. if(len<=0){
  1465. return cb(0);
  1466. }
  1467. var endChunk = (fileOffset + len - 1) / chunkSize | 0;
  1468. var fileRef={cheerpjDownload:null};
  1469. while(len && curChunk <= endChunk){
  1470. assert(fileData.chunks[curChunk]);
  1471. var curChunk = curChunk+1|0;
  1472. }
  1473. // All chunks are now loaded
  1474. var curChunk = fileOffset / chunkSize | 0;
  1475. var curOffset = fileOffset - (curChunk*chunkSize) | 0;
  1476. var i=0;
  1477. while(i<len)
  1478. {
  1479. var c = fileData.chunks[curChunk];
  1480. if((len-i) < (chunkSize-curOffset))
  1481. chunkSize = curOffset+(len-i);
  1482. for(var j=curOffset;j<chunkSize;j++)
  1483. {
  1484. buf[off+i>>>0]=c[j];
  1485. i++;
  1486. }
  1487. curChunk++;
  1488. curOffset=0;
  1489. }
  1490. assert(i==len);
  1491. return cb(len);
  1492. }
  1493. function idbWriteAsync(fileData, fileOffset, buf, off, len, cb)
  1494. {
  1495. var chunkSize = 1024*1024;
  1496. var curChunk = 0;
  1497. var endChunk = (fileOffset + len) / chunkSize | 0;
  1498. // Create all chunks first
  1499. while(curChunk <= endChunk)
  1500. {
  1501. if(fileData.chunks[curChunk])
  1502. {
  1503. var curChunk = curChunk+1|0;
  1504. continue;
  1505. }
  1506. fileData.chunks[curChunk]=new Uint8Array(chunkSize);
  1507. curChunk = curChunk+1|0;
  1508. }
  1509. var curChunk = fileOffset / chunkSize | 0;
  1510. var curOffset = fileOffset - (curChunk*chunkSize) | 0;
  1511. var i=0;
  1512. while(i<len)
  1513. {
  1514. var c = fileData.chunks[curChunk];
  1515. if((len-i) < (chunkSize-curOffset))
  1516. chunkSize = curOffset+(len-i);
  1517. for(var j=curOffset;j<chunkSize;j++)
  1518. {
  1519. c[j]=buf[off+i>>>0];
  1520. i++;
  1521. }
  1522. curChunk++;
  1523. curOffset=0;
  1524. }
  1525. assert(i==len);
  1526. if((fileOffset+len|0) > fileData.length)
  1527. {
  1528. fileData.length = fileOffset + len | 0;
  1529. }
  1530. return cb(len);
  1531. }
  1532. function idbCommitFileData(fileData, cb)
  1533. {
  1534. if (!fileData.dirty)
  1535. return cb();
  1536. var mp = fileData.parent;
  1537. assert(mp.isSplit);
  1538. assert(mp.dbConnection);
  1539. var fileRef = null;
  1540. var filePath = fileData.path;
  1541. if(filePath == null){
  1542. assert(fileData.inodeId > 0);
  1543. filePath = fileData.inodeId;
  1544. }
  1545. // For non-files we only update permType
  1546. if((fileData.permType & CheerpJFileData.S_IFMT) != CheerpJFileData.S_IFREG){
  1547. fileRef={};
  1548. idbResolve(mp, filePath, fileRef, function()
  1549. {
  1550. assert(fileRef.res);
  1551. fileRef = fileRef.res;
  1552. fileRef.permType = fileData.permType;
  1553. if((fileData.permType & CheerpJFileData.S_IFMT) != CheerpJFileData.S_IFDIR){
  1554. fileRef.contents = fileData.chunks;
  1555. }
  1556. var tx = mp.dbConnection.transaction("files", "readwrite");
  1557. var store = tx.objectStore("files");
  1558. tx.oncomplete = function()
  1559. {
  1560. cb();
  1561. };
  1562. fileData.dirty = 0;
  1563. store.put(fileRef, filePath);
  1564. });
  1565. }else{
  1566. var contents = new Uint8Array(fileData.length);
  1567. var chunkSize = 1024*1024;
  1568. var curChunk = 0;
  1569. for(var i=0;i<fileData.length;)
  1570. {
  1571. var c = fileData.chunks[curChunk];
  1572. if(fileData.length - i < chunkSize)
  1573. c = c.subarray(0, fileData.length-i)
  1574. contents.set(c, i);
  1575. curChunk++;
  1576. i += c.length;
  1577. }
  1578. fileRef = { type: "file", contents:contents, inodeId: fileData.inodeId, permType: fileData.permType, uid: fileData.uid, gid: fileData.gid, lastModified: (Date.now() / 1000)|0 };
  1579. var tx = mp.dbConnection.transaction("files", "readwrite");
  1580. var store = tx.objectStore("files");
  1581. tx.oncomplete = function()
  1582. {
  1583. cb();
  1584. };
  1585. fileData.dirty = 0;
  1586. store.put(fileRef, filePath);
  1587. }
  1588. }
  1589. function idbUnlinkAsync(mp, path, cb)
  1590. {
  1591. assert(mp.isSplit);
  1592. var fileRef={fileData:null, parentPath: null, parentRes:null, res:null};
  1593. idbEnsureDBConnection(mp, function(){
  1594. idbResolve(mp, path, fileRef, function(){
  1595. if(fileRef.res == null)
  1596. {
  1597. // File does not exists
  1598. return cb(false);
  1599. }
  1600. else
  1601. {
  1602. var parentPath = fileRef.parentPath;
  1603. var parentRes = fileRef.parentRes;
  1604. var res = fileRef.res;
  1605. if(res.type == "dir")
  1606. {
  1607. // We can only delete empty directories
  1608. if(res.contents.length > 0)
  1609. return cb(false);
  1610. // Remove the cached entry
  1611. idbDelDEntry(mp, path);
  1612. }
  1613. var filePath = path.substr(parentPath.length);
  1614. var fileIndex = parentRes.contents.indexOf(filePath);
  1615. assert(filePath.length > 0);
  1616. assert(fileIndex >= 0);
  1617. parentRes.contents.splice(fileIndex, 1);
  1618. var tx = mp.dbConnection.transaction("files", "readwrite");
  1619. var store = tx.objectStore("files");
  1620. tx.onerror = idbTrap;
  1621. tx.onabort = idbTrap;
  1622. tx.oncomplete = function()
  1623. {
  1624. this.onerror = null;
  1625. this.onabort = null;
  1626. this.oncomplete = null;
  1627. // If we get here the deletion was successfull
  1628. return cb(true);
  1629. };
  1630. store.put(parentRes, parentPath);
  1631. store.delete(path);
  1632. if(fileRef.res.hasOwnProperty("refCount"))
  1633. idbDecRefIndirect(store, fileRef.res);
  1634. }
  1635. });
  1636. });
  1637. }
  1638. function idbEnsureDBConnection(mp, cb)
  1639. {
  1640. if(mp.dbConnection == null){
  1641. // Async connection to the DB
  1642. var openResult = indexedDB.open("cjFS_"+mp.mountPoint);
  1643. assert(openResult.readyState != "done");
  1644. openResult.folder = mp;
  1645. openResult.onerror = idbTrap;
  1646. openResult.onsuccess = function(e)
  1647. {
  1648. this.onerror = null;
  1649. this.onsuccess = null;
  1650. this.onupgradeneeded = null;
  1651. this.folder.dbConnection = this.result;
  1652. var tx = this.folder.dbConnection.transaction("files", "readonly");
  1653. // Retrieve the first unallocated inode
  1654. var store = tx.objectStore("files");
  1655. var inodeReq = store.get("");
  1656. assert(inodeReq.readyState != "done");
  1657. inodeReq.folder = this.folder;
  1658. inodeReq.onerror = idbTrap;
  1659. inodeReq.onsuccess = function()
  1660. {
  1661. idbAddDEntry(this.folder, "", this.result);
  1662. this.folder.inodeId = this.result.nextInode;
  1663. return cb();
  1664. };
  1665. }
  1666. openResult.onupgradeneeded = function(e)
  1667. {
  1668. this.onerror = null;
  1669. this.onsuccess = null;
  1670. this.onupgradeneeded = null;
  1671. var db = this.result;
  1672. db.onerror = idbTrap;
  1673. db.onabort = idbTrap;
  1674. db.onclose = idbTrap;
  1675. db.onversionchange = idbTrap;
  1676. var store=db.createObjectStore("files");
  1677. // Initialize the root dir
  1678. var req = store.add({ type: "dir", contents:[], uid: 0, gid: 0, lastModified: (Date.now() / 1000)|0, inodeId: 1, nextInode:2 }, "");
  1679. assert(req.readyState != "done");
  1680. // TODO: What if this fails?
  1681. req.onabort = idbTrap;
  1682. this.transaction.oncomplete = function()
  1683. {
  1684. this.oncomplete = null;
  1685. openResult.folder.dbConnection = this.db;
  1686. return cb();
  1687. }
  1688. }
  1689. }else{
  1690. return cb();
  1691. }
  1692. }
  1693. var IdbOps = { statAsync: idbStatAsync, listAsync: idbListAsync, makeFileData: idbMakeFileData, createDirAsync: idbCreateDirAsync, renameAsync: idbRenameAsync, linkAsync: idbLinkAsync, unlinkAsync: idbUnlinkAsync };
  1694. var IdbInodeOps = { readAsync: idbReadAsync, writeAsync: idbWriteAsync, close: idbCommitFileData };
  1695. function CheerpJDataFolder(mp)
  1696. {
  1697. CheerpJFolder.call(this, mp);
  1698. this.mountOps = StrOps;
  1699. this.inodeOps = StrInodeOps;
  1700. this.files = {};
  1701. this.lastInode = 2;
  1702. }
  1703. CheerpJDataFolder.prototype = Object.create(CheerpJFolder.prototype);
  1704. function strStatAsync(mp, path, fileRef, cb)
  1705. {
  1706. if(path == "")
  1707. {
  1708. fileRef.permType = CheerpJFileData.S_IFDIR | 0555;
  1709. // Use the dev id as the inode id
  1710. fileRef.inodeId = 1;
  1711. return cb();
  1712. }
  1713. if(mp.files.hasOwnProperty(path))
  1714. {
  1715. var file = mp.files[path];
  1716. fileRef.fileLength = file.data.length;
  1717. fileRef.permType = CheerpJFileData.S_IFREG | 0444;
  1718. fileRef.inodeId = file.inodeId;
  1719. }
  1720. else
  1721. {
  1722. fileRef.permType = 0;
  1723. }
  1724. return cb();
  1725. }
  1726. function strListAsync(mp, path, fileRef, cb)
  1727. {
  1728. // Only the root itself can be listed
  1729. if(path != ""){
  1730. return cb();
  1731. }
  1732. for(var p in mp.files)
  1733. {
  1734. fileRef.push(p.substring(1));
  1735. }
  1736. return cb();
  1737. }
  1738. function strMakeFileData(mp, path, mode, uid, gid, cb)
  1739. {
  1740. if(path == "")
  1741. {
  1742. var ret = new CheerpJFileData(mp, path, 0, 0, CheerpJFileData.S_IFDIR | 0777, /*lastModified*/0, /*uid*/0, /*gid*/0);
  1743. ret.mount = mp.inodeOps;
  1744. return cb(ret);
  1745. }
  1746. if(!mp.files.hasOwnProperty(path))
  1747. {
  1748. return cb(null);
  1749. }
  1750. var ret = mp.files[path].data;
  1751. assert(ret instanceof Uint8Array);
  1752. var inodeId = mp.files[path].inodeId
  1753. var fileData = new CheerpJFileData(mp, path, ret.length|0, inodeId|0, CheerpJFileData.S_IFREG | 0444, /*lastModified*/0, /*uid*/0, /*gid*/0);
  1754. fileData.mount = mp.inodeOps;
  1755. fileData.data = ret;
  1756. return cb(fileData);
  1757. }
  1758. function strReadAsync(fileData, fileOffset, buf, off, len, flags, cb)
  1759. {
  1760. if(len > fileData.data.length - fileOffset)
  1761. len = fileData.data.length - fileOffset;
  1762. // EOF
  1763. if(len == 0){
  1764. return cb(0);
  1765. }
  1766. for(var i=0;i<len;i++)
  1767. buf[off+i>>>0] = fileData.data[fileOffset+i|0];
  1768. return cb(len);
  1769. }
  1770. var StrOps = { statAsync: strStatAsync, listAsync: strListAsync, makeFileData: strMakeFileData, createDirAsync: null, renameAsync: null, linkAsync: null, unlinkAsync: null };
  1771. var StrInodeOps = { readAsync: strReadAsync, writeAsync: null, close: null };
  1772. function CheerpJDevFolder(mp, basePath)
  1773. {
  1774. CheerpJFolder.call(this, mp);
  1775. this.mountOps = DevOps;
  1776. this.inodeOps = null;
  1777. this.isSplit = true;
  1778. this.devMap = {};
  1779. // Reserve a few ides for random/urandom/nul;
  1780. this.nextInode = 4;
  1781. }
  1782. CheerpJDevFolder.prototype = Object.create(CheerpJFolder.prototype);
  1783. function devStatAsync(mp, path, fileRef, cb)
  1784. {
  1785. if(path == "/random" || path == "/urandom" || path == "/null")
  1786. {
  1787. fileRef.permType = CheerpJFileData.S_IFCHR | 0666;
  1788. }
  1789. else
  1790. {
  1791. fileRef.permType = 0;
  1792. }
  1793. return cb();
  1794. }
  1795. function devMakeFileData(mp, path, mode, uid, gid, cb)
  1796. {
  1797. var fileData = null;
  1798. if(path == "/random")
  1799. {
  1800. fileData = new CheerpJFileData(mp, path, 0xffffffff, 1, CheerpJFileData.S_IFCHR | 0666, /*lastModified*/0, /*uid*/0, /*gid*/0);
  1801. fileData.mount = DevRandomInodeOps;
  1802. }
  1803. else if(path == "/urandom")
  1804. {
  1805. fileData = new CheerpJFileData(mp, path, 0xffffffff, 2, CheerpJFileData.S_IFCHR | 0666, /*lastModified*/0, /*uid*/0, /*gid*/0);
  1806. fileData.mount = DevRandomInodeOps;
  1807. }
  1808. else if(path == "/null")
  1809. {
  1810. fileData = new CheerpJFileData(mp, path, 0xffffffff, 3, CheerpJFileData.S_IFCHR | 0666, /*lastModified*/0, /*uid*/0, /*gid*/0);
  1811. fileData.mount = DevNullInodeOps;
  1812. }
  1813. else if(mp.devMap.hasOwnProperty(path))
  1814. {
  1815. fileData = new CheerpJFileData(mp, path, 0xffffffff, mp.devMap[path].inodeId, CheerpJFileData.S_IFCHR | 0666, /*lastModified*/0, /*uid*/0, /*gid*/0);
  1816. fileData.mount = mp.devMap[path];
  1817. }
  1818. return cb(fileData);
  1819. }
  1820. function devRandomRead(fileData, fileOffset, buf, off, len, flags, cb)
  1821. {
  1822. var data = buf.subarray(off, off+len);
  1823. crypto.getRandomValues(data);
  1824. return cb(len);
  1825. }
  1826. function devRandomWrite(fileData, fileOffset, buf, off, len, cb)
  1827. {
  1828. return cb(len);
  1829. }
  1830. var DevRandomInodeOps = { readAsync: devRandomRead, writeAsync: devRandomWrite, close: null };
  1831. function devNullRead(fileData, fileOffset, buf, off, len, flags, cb)
  1832. {
  1833. return cb(0);
  1834. }
  1835. function devNullWrite(fileData, fileOffset, buf, off, len, cb)
  1836. {
  1837. return cb(len);
  1838. }
  1839. var DevNullInodeOps = { readAsync: devNullRead, writeAsync: devNullWrite, close: null };
  1840. var DevOps = { statAsync: devStatAsync, listAsync: null, makeFileData: devMakeFileData, createDirAsync: null, renameAsync: null, linkAsync: null, unlinkAsync: null };
  1841. function conReadAsync(fileData, fileOffset, buf, off, len, flags, cb)
  1842. {
  1843. if(!fileData.inCallback)
  1844. return;
  1845. fileData.inCallback(fileData.param, fileOffset, buf, off, len, cb);
  1846. }
  1847. function conWriteAsync(fileData, fileOffset, buf, off, len, cb)
  1848. {
  1849. fileData.outCallback(fileData.param, fileOffset, buf, off, len, cb);
  1850. }
  1851. var ConInodeOps = { readAsync: conReadAsync, writeAsync: conWriteAsync, close: null };
  1852. function cheerpjCreateConsole(param, inCallback, outCallback)
  1853. {
  1854. var fileData = new CheerpJFileData(null, null, 0, 0, CheerpJFileData.S_IFCHR | 0666, /*lastModified*/0, /*uid*/0, /*gid*/0);
  1855. fileData.mount = ConInodeOps;
  1856. // Add custom fields
  1857. fileData.inCallback = inCallback;
  1858. fileData.outCallback = outCallback;
  1859. fileData.param = param;
  1860. return fileData;
  1861. }
  1862. function cheerpjDefaultConsoleWrite(param, fileOffset, buf, off, len, cb)
  1863. {
  1864. // Console output, either to debug console or DOM console
  1865. var strBytes = ""
  1866. for(var i=0;i<len;i++)
  1867. {
  1868. var b = buf[i+off]&0xff;
  1869. var h = b.toString(16);
  1870. if(b < 16)
  1871. h = "0" + h;
  1872. strBytes += "%" + h;
  1873. }
  1874. strBytes = decodeURIComponent(strBytes);
  1875. var c = self.document ? document.getElementById("console") : null;
  1876. if(c)
  1877. c.textContent += strBytes;
  1878. else
  1879. console.log(strBytes);
  1880. return cb(len);
  1881. }
  1882. function cheerpOSAddStringFile(name, str)
  1883. {
  1884. var mount = cheerpjGetFSMountForPath(name);
  1885. var fileName = name.substr(mount.mountPoint.length-1);
  1886. if(fileName.lastIndexOf("/") != 0)
  1887. console.error(`CheerpOS: Directories are not supported ${name}`);
  1888. else
  1889. cheerpOSAddStringFileMain(mount, fileName, str);
  1890. }
  1891. function cheerpOSAddStringFileMain(mount, fileName, str)
  1892. {
  1893. assert(mount instanceof CheerpJDataFolder);
  1894. if(!(str instanceof Uint8Array))
  1895. {
  1896. var enc = new TextEncoder();
  1897. str = enc.encode(str.toString());
  1898. }
  1899. mount.files[fileName] = {inodeId:mount.lastInode, data:str};
  1900. mount.lastInode = mount.lastInode + 1|0;
  1901. mount.clearCached(fileName);
  1902. }
  1903. function cheerpOSRemoveStringFile(name)
  1904. {
  1905. var mount = cheerpjGetFSMountForPath(name);
  1906. assert(mount instanceof CheerpJDataFolder);
  1907. var fileName = name.substr(mount.mountPoint.length-1);
  1908. delete mount.files[fileName];
  1909. mount.clearCached(fileName);
  1910. }
  1911. function cheerpjAddStringFile(name, str)
  1912. {
  1913. console.warn("Deprecated: Use cheerpOSAddStringFile");
  1914. return cheerpOSAddStringFile(name, str);
  1915. }
  1916. function cheerpjRemoveStringFile(name)
  1917. {
  1918. console.warn("Deprecated: Use cheerpjRemoveStringFile");
  1919. return cheerpOSRemoveStringFile(name);
  1920. }
  1921. function cheerpjGetFSMountForPath(path)
  1922. {
  1923. for(var i=0;i<cheerpjFSMounts.length;i++)
  1924. {
  1925. var mount = cheerpjFSMounts[i];
  1926. if(path.startsWith(mount.mountPoint)){
  1927. return mount;
  1928. }
  1929. }
  1930. debugger
  1931. return null;
  1932. }
  1933. function cheerpjNormalizePath(path)
  1934. {
  1935. var parts = path.split('/');
  1936. var newParts = []
  1937. var partsUnderflow = 0;
  1938. for(var i=0;i<parts.length;i++)
  1939. {
  1940. if(parts[i] == '.' || parts[i] == "")
  1941. continue;
  1942. else if(parts[i] == '..')
  1943. {
  1944. if(newParts.length)
  1945. newParts.pop();
  1946. else
  1947. partsUnderflow++;
  1948. }
  1949. else
  1950. newParts.push(parts[i]);
  1951. }
  1952. if(!path.startsWith('/') && partsUnderflow == 0)
  1953. {
  1954. // TODO: Support cwd
  1955. newParts.unshift("files");
  1956. }
  1957. return "/" + newParts.join("/");
  1958. }
  1959. function cheerpOSStat(path_, fileRef, cb)
  1960. {
  1961. var path = cheerpjNormalizePath(path_)
  1962. var mount = cheerpjGetFSMountForPath(path + "/");
  1963. cheerpOSStatMain(mount, path.substr(mount.mountPoint.length-1), fileRef, cb);
  1964. }
  1965. function cheerpOSStatMain(mount, fileName, fileRef, cb)
  1966. {
  1967. fileRef.parent = mount;
  1968. mount.mountOps.statAsync(mount, fileName, fileRef, cb);
  1969. }
  1970. function cheerpOSListFiles(path_, fileRef, cb)
  1971. {
  1972. var path = cheerpjNormalizePath(path_)
  1973. var mount = cheerpjGetFSMountForPath(path + "/");
  1974. cheerpOSListFilesMain(mount, path.substr(mount.mountPoint.length-1), fileRef, cb);
  1975. }
  1976. function cheerpOSListFilesMain(mount, path_, fileRef, cb)
  1977. {
  1978. // Some backends (like Web) do not implement list
  1979. if(!mount.mountOps.listAsync){
  1980. return;
  1981. }
  1982. mount.mountOps.listAsync(mount, path_, fileRef, cb);
  1983. }
  1984. function cheerpOSCreateDir(path_, fileRef, mode, cb)
  1985. {
  1986. var path = cheerpjNormalizePath(path_)
  1987. var mount = cheerpjGetFSMountForPath(path);
  1988. cheerpOSCreateDirMain(mount, path.substr(mount.mountPoint.length-1), fileRef, mode, /*uid*/0, /*gid*/0, cb);
  1989. }
  1990. function cheerpOSCreateDirMain(mount, path_, fileRef, mode, uid, gid, cb)
  1991. {
  1992. if(!mount.mountOps.createDirAsync){
  1993. return cb(-1);
  1994. }
  1995. mount.mountOps.createDirAsync(mount, path_, fileRef, mode, uid, gid, cb);
  1996. }
  1997. function cheerpOSInitFds(fds)
  1998. {
  1999. var conFileData = cheerpjCreateConsole(null, null, cheerpjDefaultConsoleWrite);
  2000. var conFD = { fileData: conFileData, offset: 0, flags: 0 };
  2001. fds[0] = conFD;
  2002. fds[1] = conFD;
  2003. fds[2] = conFD;
  2004. conFileData.refCount+=3;
  2005. }
  2006. function cheerpOSCopyFds(dstFds, srcFds)
  2007. {
  2008. for(var i=0;i<srcFds.length;i++)
  2009. {
  2010. var fdObj = srcFds[i];
  2011. var fileData = fdObj.fileData;
  2012. dstFds[i] = {fileData: fileData, offset: fdObj.offset, flags: fdObj.flags};
  2013. fileData.refCount=fileData.refCount+1|0;
  2014. }
  2015. }
  2016. function cheerpOSAllocateFd(fds, fileData)
  2017. {
  2018. var newFD = {fileData: fileData, offset: 0, flags: 0};
  2019. for(var i=0;i<fds.length;i++)
  2020. {
  2021. if(fds[i]===null)
  2022. {
  2023. fds[i] = newFD;
  2024. return i;
  2025. }
  2026. }
  2027. fds.push(newFD);
  2028. return fds.length-1|0;
  2029. }
  2030. function cheerpOSDupFd(fds, oldFd, newFd, cb)
  2031. {
  2032. cheerpOSClose(fds, newFd, function()
  2033. {
  2034. var fdObj = fds[oldFd];
  2035. var fileData = fdObj.fileData;
  2036. fds[newFd] = {fileData: fileData, offset: fdObj.offset, flags: fdObj.flags};
  2037. fileData.refCount=fileData.refCount+1|0;
  2038. cb(newFd);
  2039. });
  2040. }
  2041. function cheerpjWakeOpenThreads (fileName, mount)
  2042. {
  2043. var tmp = mount.cacheThreads[fileName];
  2044. delete mount.cacheThreads[fileName];
  2045. for(var i = 0; i < tmp.length; i++)
  2046. {
  2047. tmp[i]();
  2048. }
  2049. }
  2050. function cheerpOSOpen(fds, path_, mode, cb)
  2051. {
  2052. var path = cheerpjNormalizePath(path_)
  2053. var mount = cheerpjGetFSMountForPath(path + "/");
  2054. var fileName = path.substr(mount.mountPoint.length-1);
  2055. if(mount.cacheThreads[fileName]){
  2056. mount.cacheThreads[fileName].push(function()
  2057. {
  2058. cheerpOSOpen(fds, path_, mode, cb);
  2059. });
  2060. return;
  2061. }
  2062. return cheerpOSOpenMain(fileName, mount, fds, mode, /*uid*/0, /*gid*/0, cb);
  2063. }
  2064. function cheerpOSOpenMain(fileName, mount, fds, mode, uid, gid, cb)
  2065. {
  2066. var fileData = mount.getCached(fileName);
  2067. if(fileData === null)
  2068. {
  2069. mount.cacheThreads[fileName] = [];
  2070. var fileRef = {};
  2071. assert(mount.mountOps.makeFileData);
  2072. mount.mountOps.makeFileData(mount, fileName, mode, uid, gid, function(fileData){
  2073. if(fileData == null){
  2074. cheerpjWakeOpenThreads(fileName, mount);
  2075. return cb(-1);
  2076. }
  2077. mount.setCached(fileName, fileData);
  2078. cheerpjWakeOpenThreads(fileName, mount);
  2079. fileData.refCount=fileData.refCount+1|0;
  2080. return cb(cheerpOSAllocateFd(fds, fileData));
  2081. });
  2082. return;
  2083. }else if(mode != "r" && !fileData.parent.isSplit){
  2084. // Make sure we don't allow opening for write a cached file that does not support it
  2085. return cb(-1);
  2086. }else if(mode == "w"){
  2087. // Truncation of a cached file
  2088. fileData.dirty = 1;
  2089. fileData.chunks = [];
  2090. fileData.length = 0;
  2091. }
  2092. fileData.refCount=fileData.refCount+1|0;
  2093. return cb(cheerpOSAllocateFd(fds, fileData));
  2094. }
  2095. function cheerpOSRead(fds, fd, buf, off, len, cb)
  2096. {
  2097. if(fd < 0){
  2098. return cb(-1);
  2099. }
  2100. var fdObj = fds[fd];
  2101. assert(fdObj);
  2102. assert(fdObj.fileData.mount);
  2103. assert(off + len <= buf.length);
  2104. fdObj.fileData.mount.readAsync(fdObj.fileData, fdObj.offset, buf, off, len, fdObj.flags, function(ret){
  2105. if(ret<=0){
  2106. return cb(ret);
  2107. }
  2108. fdObj.offset = fdObj.offset + ret|0;
  2109. return cb(ret);
  2110. });
  2111. }
  2112. function cheerpOSWrite(fds, fd, buf, off, len, cb)
  2113. {
  2114. if(fd < 0){
  2115. return cb(-1);
  2116. }
  2117. var fdObj = fds[fd];
  2118. assert(fdObj);
  2119. assert(fdObj.fileData.mount);
  2120. assert(off + len <= buf.length);
  2121. fdObj.fileData.dirty = 1;
  2122. fdObj.fileData.mount.writeAsync(fdObj.fileData, fdObj.offset, buf, off, len, function(ret){
  2123. if(ret<=0){
  2124. return cb(ret);
  2125. }
  2126. fdObj.offset = fdObj.offset + ret|0;
  2127. return cb(ret);
  2128. });
  2129. }
  2130. function cheerpOSRename(srcPath_, dstPath_, cb)
  2131. {
  2132. var srcPath = cheerpjNormalizePath(srcPath_);
  2133. var dstPath = cheerpjNormalizePath(dstPath_);
  2134. var srcMount = cheerpjGetFSMountForPath(srcPath + "/");
  2135. var dstMount = cheerpjGetFSMountForPath(dstPath + "/");
  2136. // Only allowed on the same mount point
  2137. if(srcMount !== dstMount){
  2138. return cb(0);
  2139. }
  2140. var prefixLen = srcMount.mountPoint.length-1;
  2141. var srcFileName = srcPath.substr(prefixLen);
  2142. var dstFileName = dstPath.substr(prefixLen);
  2143. return cheerpOSRenameMain(srcMount, srcFileName, dstFileName, cb);
  2144. }
  2145. function cheerpOSRenameMain(mount, srcPath_, dstPath_, cb)
  2146. {
  2147. if(!mount.mountOps.renameAsync){
  2148. return cb(0);
  2149. }
  2150. mount.mountOps.renameAsync(mount, srcPath_, dstPath_, function(ret){
  2151. if(ret == 0){
  2152. return cb(0);
  2153. }
  2154. mount.clearCached(srcPath_);
  2155. mount.clearCached(dstPath_);
  2156. return cb(1);
  2157. });
  2158. }
  2159. function cheerpOSLink(srcPath_, dstPath_, cb)
  2160. {
  2161. var srcPath = cheerpjNormalizePath(srcPath_);
  2162. var dstPath = cheerpjNormalizePath(dstPath_);
  2163. var srcMount = cheerpjGetFSMountForPath(srcPath + "/");
  2164. var dstMount = cheerpjGetFSMountForPath(dstPath + "/");
  2165. // Only allowed on the same mount point
  2166. if(srcMount !== dstMount){
  2167. return cb(0);
  2168. }
  2169. var prefixLen = srcMount.mountPoint.length-1;
  2170. return cheerpOSLinkMain(srcMount, srcPath.substr(prefixLen), dstPath.substr(prefixLen), cb);
  2171. }
  2172. function cheerpOSLinkMain(mount, srcPath_, dstPath_, cb)
  2173. {
  2174. if(!mount.mountOps.linkAsync){
  2175. return cb(0);
  2176. }
  2177. mount.mountOps.linkAsync(mount, srcPath_, dstPath_, cb);
  2178. }
  2179. function cheerpOSUnlink(path_, cb)
  2180. {
  2181. var path = cheerpjNormalizePath(path_, false);
  2182. var mount = cheerpjGetFSMountForPath(path);
  2183. var fileName = path.substr(mount.mountPoint.length-1);
  2184. return cheerpOSUnlinkMain(mount, fileName, cb);
  2185. }
  2186. function cheerpOSUnlinkMain(mount, path_, cb)
  2187. {
  2188. mount.clearCached(path_);
  2189. if(!mount.mountOps.unlinkAsync)
  2190. return cb(-1);
  2191. mount.mountOps.unlinkAsync(mount, path_, cb);
  2192. }
  2193. var cjGraceTimeQueue = []
  2194. function cjGraceTimeExpire()
  2195. {
  2196. // Only get the first item on the queue
  2197. assert(cjGraceTimeQueue.length);
  2198. var fileData = cjGraceTimeQueue.shift();
  2199. if(fileData == null)
  2200. {
  2201. // The file lifetime has been bumped
  2202. return;
  2203. }
  2204. if(fileData.refCount > 0)
  2205. {
  2206. // The file has been resurrected
  2207. return;
  2208. }
  2209. // File dead, free the memory
  2210. var mount = fileData.parent;
  2211. for(var f in mount.fileCache)
  2212. {
  2213. if(fileData.inodeId == mount.fileCache[f])
  2214. mount.clearCached(f);
  2215. }
  2216. assert(fileData.cacheRefCount == 0);
  2217. }
  2218. function cheerpOSClose(fds, fd, cb)
  2219. {
  2220. if(fd < 0 || fd >= fds.length){
  2221. cb();
  2222. return;
  2223. }
  2224. var fdObj = fds[fd];
  2225. if(fdObj === null)
  2226. {
  2227. cb();
  2228. return;
  2229. }
  2230. var fileData = fdObj.fileData;
  2231. // No fileData may be there for FDs which are not files
  2232. if(fileData)
  2233. {
  2234. fileData.refCount = fileData.refCount-1|0;
  2235. if(fileData.refCount===0 && fileData.parent != null)
  2236. {
  2237. if(fileData.dirty)
  2238. {
  2239. for(var p in cosFileWatchPrefixes)
  2240. {
  2241. assert(fileData.path.startsWith("/"));
  2242. var path = fileData.parent.mountPoint + fileData.path.substr(1);
  2243. if(path === p || path.startsWith(p + "/"))
  2244. cosFileWatchPrefixes[p](path);
  2245. }
  2246. }
  2247. // Commit the data immediately to ensure persistence
  2248. if(fileData.mount.close)
  2249. {
  2250. fileData.mount.close(fileData, cb);
  2251. // Reset the callback to not call it again at the end of the function
  2252. cb = null;
  2253. }
  2254. // Make sure there is only one entry in the queue for each file,
  2255. // if there is an existing one, null it out
  2256. var prevIdx = cjGraceTimeQueue.indexOf(fileData);
  2257. if(prevIdx >= 0)
  2258. cjGraceTimeQueue[prevIdx] = null;
  2259. // Put the cached file in the queue, and give it a 10 sec grace time before freeing the memory
  2260. cjGraceTimeQueue.push(fileData);
  2261. setTimeout(cjGraceTimeExpire, 10000);
  2262. }
  2263. }
  2264. fds[fd] = null;
  2265. if(cb)
  2266. cb();
  2267. }
  2268. function httpSocketReadAsync(fileData, fileOffset, buf, off, len, flags, cb)
  2269. {
  2270. assert(len != 0);
  2271. let cnt = off;
  2272. if(fileData.currData === null){
  2273. return cb(0);
  2274. }
  2275. return httpSocketReadLoop(fileData, fileOffset, buf, off, len, flags, cnt, cb);
  2276. }
  2277. function httpSocketReadLoop(fileData, fileOffset, buf, off, len, flags, cnt, cb)
  2278. {
  2279. if(buf.length == 0)
  2280. {
  2281. // Defend against buffer invalidation
  2282. return cb(/*EFAULT*/-14);
  2283. }
  2284. while(1){
  2285. if(cnt === off+len){
  2286. break;
  2287. }
  2288. if(!fileData.currData || fileData.currPos >= fileData.currData.length){
  2289. while(1){
  2290. if(fileData.chunks.length !== 0){
  2291. break;
  2292. }
  2293. if(cnt>off)
  2294. return cb(cnt-off);
  2295. fileData.blockedThread = function(){
  2296. return httpSocketReadLoop(fileData, fileOffset, buf, off, len, flags, cnt, cb);
  2297. };
  2298. return;
  2299. }
  2300. fileData.currData = fileData.chunks.shift();
  2301. fileData.currPos = 0;
  2302. if(!(fileData.currData instanceof Uint8Array)){
  2303. if(fileData.currData == 'cjRemoteError')
  2304. {
  2305. fileData.currData = null;
  2306. return cb(cnt-off);
  2307. }
  2308. else
  2309. {
  2310. return cb(cnt-off);
  2311. }
  2312. }
  2313. }
  2314. buf[cnt] = fileData.currData[fileData.currPos];
  2315. fileData.currPos = fileData.currPos + 1 | 0;
  2316. cnt++;
  2317. }
  2318. return cb(cnt-off);
  2319. }
  2320. function cheerpNetReadFetchOS(reader,fileData)
  2321. {
  2322. reader.read()
  2323. .then(function(result){
  2324. console.log(result);
  2325. if(result.done)
  2326. {
  2327. fileData.chunks.push(null);
  2328. }
  2329. else
  2330. {
  2331. if(result.value instanceof ArrayBuffer)
  2332. {
  2333. fileData.chunks.push(new Uint8Array(result.value));
  2334. }
  2335. else
  2336. {
  2337. fileData.chunks.push(result.value);
  2338. }
  2339. }
  2340. if(fileData.blockedThread)
  2341. {
  2342. var blockedThread=fileData.blockedThread;
  2343. fileData.blockedThread=null;
  2344. blockedThread();
  2345. }
  2346. if(!result.done)
  2347. {
  2348. cheerpNetReadFetchOS(reader, fileData);
  2349. }
  2350. });
  2351. }
  2352. function httpSocketWriteAsync(fileData, fileOffset, buf, off, len, cb)
  2353. {
  2354. let i = 0;
  2355. let cnt = 0;
  2356. if(fileData.currRequest.headersNewLines < 1)
  2357. {
  2358. /*Consume bytes*/
  2359. for(i = off; i < off+len && fileData.currRequest.headersNewLines < 1; i++)
  2360. {
  2361. cnt++;
  2362. let currChar = String.fromCharCode(buf[i]);
  2363. if(currChar === '\n'){ /* Parse */
  2364. let line = fileData.currRequest.currString;
  2365. if(line.startsWith('\r'))
  2366. {
  2367. fileData.currRequest.headersNewLines++;
  2368. }
  2369. else
  2370. {
  2371. if(fileData.currRequest.nLines === 0 && line.indexOf("HTTP/1.1") !== -1)
  2372. {
  2373. let firstLine = line.split(" ");
  2374. fileData.currRequest.method = firstLine[0];
  2375. fileData.currRequest.url = firstLine[1];
  2376. }
  2377. else if(!line.toLowerCase().startsWith("cookie"))
  2378. {
  2379. let key = line.substr(0,line.indexOf(':'));
  2380. let value = line.substr(line.indexOf(':')+2);
  2381. if(key.toLowerCase() === "host")
  2382. {
  2383. if(window.location.protocol == "https:")
  2384. {
  2385. fileData.currRequest.url = "https://" + value + fileData.currRequest.url;
  2386. }
  2387. else
  2388. {
  2389. fileData.currRequest.url = "http://" + value + fileData.currRequest.url;
  2390. }
  2391. }
  2392. else
  2393. {
  2394. fileData.currRequest.headers.append(key, value);
  2395. }
  2396. }
  2397. }
  2398. fileData.currRequest.currString = "";
  2399. fileData.currRequest.nLines++;
  2400. }else
  2401. {
  2402. fileData.currRequest.currString += currChar;
  2403. }
  2404. fileData.currRequest.prevChar = currChar;
  2405. }
  2406. off = off + i - 1;
  2407. }
  2408. if(fileData.currRequest.headersNewLines >= 1)
  2409. {
  2410. if(fileData.currRequest.method === "POST")
  2411. {
  2412. if(!fileData.currRequest.data)
  2413. {
  2414. fileData.currRequest.data = new Uint8Array(parseInt(fileData.currRequest.headers.get("Content-Length")));
  2415. }
  2416. fileData.currRequest.data.set(buf.subarray(off, len), fileData.currRequest.dataIdx);
  2417. fileData.currRequest.dataIdx += len-off+1;
  2418. cnt += len-off;
  2419. }
  2420. if((fileData.currRequest.method === "POST" && fileData.currRequest.dataIdx < fileData.currRequest.headers.get("Content-Length"))
  2421. || (fileData.currRequest.method !== "POST" && fileData.currRequest.headersNewLines < 1))
  2422. {
  2423. return cb(cnt);
  2424. }
  2425. let dict = {credential: 'include', method: fileData.currRequest.method, headers: fileData.currRequest.headers};
  2426. if(fileData.currRequest.data)
  2427. {
  2428. dict.body = fileData.currRequest.data;
  2429. }
  2430. fetch(fileData.currRequest.url, dict)
  2431. .then(function(response)
  2432. {
  2433. let cjStr2AB = function(str) {
  2434. let bufView = new Uint8Array(str.length);
  2435. for (var i=0; i < str.length; i++) {
  2436. bufView[i] = str.charCodeAt(i);
  2437. }
  2438. return bufView;
  2439. };
  2440. if(response.ok)
  2441. {
  2442. let responseStatus = "HTTP/1.1 " + response.status + " " + response.statusText;
  2443. let responseHeaders = response.headers.entries();
  2444. fileData.chunks.push(cjStr2AB(responseStatus+'\r\n'));
  2445. let entry = responseHeaders.next();
  2446. while (!entry.done)
  2447. {
  2448. if(entry.value[0].toLowerCase() === "transfer-encoding")
  2449. {
  2450. entry = responseHeaders.next();
  2451. continue;
  2452. }
  2453. let he = entry.value[0] + ': ' + entry.value[1] + '\r\n';
  2454. fileData.chunks.push(cjStr2AB(he));
  2455. entry = responseHeaders.next();
  2456. }
  2457. fileData.chunks.push(cjStr2AB("\r\n"));
  2458. const reader = response.body.getReader();
  2459. cheerpNetReadFetchOS(reader, fileData);
  2460. }else
  2461. {
  2462. fileData.chunks.push('cjRemoteError');
  2463. }
  2464. })
  2465. .catch(function(err)
  2466. {
  2467. fileData.chunks.push('cjRemoteError');
  2468. });
  2469. }
  2470. else if(fileData.currRequest.currString.length >= 4)
  2471. {
  2472. // Not even 1 line available, can this even be HTTP?
  2473. if(!fileData.currRequest.currString.startsWith("HTTP")) {
  2474. fileData.chunks.push('cjRemoteError');
  2475. return cb(-1);
  2476. }
  2477. }
  2478. return cb(cnt);
  2479. }
  2480. function httpSocketConnect(fileData, ipAddr, ipPort)
  2481. {
  2482. // Pretend that the connection succeeds, we'll try to parse an HTTP session in the write handler.
  2483. return 0;
  2484. }
  2485. async function tcpSocketReadAsync(fileData, fileOffset, buf, off, len, flags, cb)
  2486. {
  2487. assert(len != 0);
  2488. while(true)
  2489. {
  2490. var ret = fileData.data.recv(buf, off, len);
  2491. if (ret != cjTailscaleSocket.Eagain)
  2492. return cb(ret);
  2493. await fileData.net.canRead(fileData);
  2494. }
  2495. }
  2496. function tcpSocketReadAvailable(fileData)
  2497. {
  2498. return fileData.data.readAvailable();
  2499. }
  2500. function tcpSocketListen(fileData)
  2501. {
  2502. var ret = fileData.data.listen();
  2503. return ret == 0? 0 : -1;
  2504. }
  2505. function tcpSocketConnect(fileData, ipAddr, ipPort)
  2506. {
  2507. fileData.data.bind(0);
  2508. var ret = fileData.data.connect(cjTailscaleParseIp(ipAddr), ipPort);
  2509. return ret == 0? 0 : -1;
  2510. }
  2511. function tcpSocketAccept(fileData)
  2512. {
  2513. return fileData.data.accept();
  2514. }
  2515. function socketBind(fileData, port)
  2516. {
  2517. return fileData.data.bind(port);
  2518. }
  2519. function tcpSocketShutdown(fileData, how)
  2520. {
  2521. switch(how)
  2522. {
  2523. case 0:
  2524. ret = fileData.data.shutdownRx();
  2525. break;
  2526. case 1:
  2527. ret = fileData.data.shutdownTx();
  2528. break;
  2529. case 2:
  2530. var ret1 = fileData.data.shutdownRx();
  2531. var ret2 = fileData.data.shutdownTx();
  2532. ret = ret1 != 0? ret1 : ret2;
  2533. break;
  2534. }
  2535. return ret;
  2536. }
  2537. async function tcpSocketWriteAsync(fileData, fileOffset, buf, off, len, cb)
  2538. {
  2539. while(true)
  2540. {
  2541. var ret = fileData.data.send(buf, off, len);
  2542. if (ret != cjTailscaleSocket.Eagain)
  2543. return cb(ret);
  2544. await fileData.net.canWrite(fileData);
  2545. }
  2546. }
  2547. function socketRecv(fileData, fileOffset, buf, off, len, addrInfo)
  2548. {
  2549. return fileData.data.recv(fileOffset, buf, off, len, addrInfo);
  2550. }
  2551. function socketSendTo(fileData, buf, addr, port)
  2552. {
  2553. return fileData.data.sendto(buf, cjTailscaleParseIp(addr), port);
  2554. }
  2555. function socketClose(fileData, cb)
  2556. {
  2557. if (fileData.data)
  2558. {
  2559. fileData.data.close();
  2560. fileData.data.delete();
  2561. fileData.data = null;
  2562. }
  2563. cb();
  2564. }
  2565. async function socketCanRead(fileData)
  2566. {
  2567. if (!fileData.incomingPromise)
  2568. {
  2569. fileData.incomingPromise = fileData.data.waitIncoming().then(ret => {
  2570. fileData.incomingPromise = null;
  2571. return ret;
  2572. });
  2573. }
  2574. var ret = await fileData.incomingPromise;
  2575. return ret;
  2576. }
  2577. async function socketCanWrite(fileData)
  2578. {
  2579. if (!fileData.outgoingPromise)
  2580. {
  2581. fileData.outgoingPromise = fileData.data.waitOutgoing().then(ret => {
  2582. fileData.outgoingPromise = null;
  2583. return ret;
  2584. });
  2585. }
  2586. var ret = await fileData.outgoingPromise;
  2587. return ret;
  2588. }
  2589. function internalSocketReadAsync(fileData, fileOffset, buf, off, len, flags, cb)
  2590. {
  2591. if(buf.length == 0)
  2592. {
  2593. // Defend against buffer invalidation
  2594. return cb(/*EFAULT*/-14);
  2595. }
  2596. if(fileData.chunks.length == 0)
  2597. {
  2598. if(fileData.flags & CheerpJFileData.O_NONBLOCK)
  2599. return cb(/*EAGAIN*/-11);
  2600. fileData.blockedThread = function()
  2601. {
  2602. return internalSocketReadAsync(fileData, fileOffset, buf, off, len, flags, cb);
  2603. };
  2604. return;
  2605. }
  2606. // TODO: Read more if possible
  2607. var first = fileData.chunks[0];
  2608. if(first.length < len)
  2609. len = first.length;
  2610. for(var i=0;i<len;i++)
  2611. buf[off + i] = first[i];
  2612. if(len == first.length)
  2613. fileData.chunks.shift();
  2614. else
  2615. fileData.chunks[0] = first.subarray(len);
  2616. return cb(len);
  2617. }
  2618. function internalSocketWriteAsync(fileData, fileOffset, buf, off, len, cb)
  2619. {
  2620. if(len > 0)
  2621. {
  2622. var socket = fileData.data;
  2623. var data = new Uint8Array(buf.subarray(off, off + len));
  2624. socket.peerFileData.chunks.push(data);
  2625. if (socket.peerFileData.blockedThread)
  2626. {
  2627. var thread = socket.peerFileData.blockedThread;
  2628. socket.peerFileData.blockedThread = null;
  2629. thread();
  2630. }
  2631. }
  2632. return cb(len);
  2633. }
  2634. async function internalSocketCanRead(fileData)
  2635. {
  2636. if(fileData.chunks.length != 0 || fileData.data.acceptQueue.length != 0)
  2637. {
  2638. return 0;
  2639. }
  2640. let { promise, resolve, reject } = Promise.withResolvers();
  2641. fileData.blockedThread = resolve;
  2642. await promise;
  2643. return 0;
  2644. }
  2645. function internalSocketClose(fileData, cb)
  2646. {
  2647. debugger;
  2648. }
  2649. function internalSocketConnect(fileData, addr, port)
  2650. {
  2651. var peer = InternalSocket.boundSockets[port];
  2652. if(peer && peer.data.listening)
  2653. {
  2654. var newSocket = new InternalSocket();
  2655. newSocket.peerFileData = fileData;
  2656. peer.data.acceptQueue.push({socket:newSocket, addr:addr, port:port});
  2657. if (peer.blockedThread)
  2658. {
  2659. var thread = peer.blockedThread;
  2660. peer.blockedThread = null;
  2661. thread();
  2662. }
  2663. return 0;
  2664. }
  2665. return -1;
  2666. }
  2667. function internalSocketAccept(fileData)
  2668. {
  2669. if (fileData.data.acceptQueue.length == 0)
  2670. return null;
  2671. var s = fileData.data.acceptQueue.shift();
  2672. return s;
  2673. }
  2674. function internalSocketBind(fileData, port)
  2675. {
  2676. if(port == 0)
  2677. {
  2678. // Used to assign a port, nothing to do
  2679. return 0;
  2680. }
  2681. InternalSocket.boundSockets[port] = fileData;
  2682. return 0;
  2683. }
  2684. function internalSocketListen(fileData)
  2685. {
  2686. fileData.data.listening = true;
  2687. }
  2688. function InternalSocket()
  2689. {
  2690. this.peerFileData = null;
  2691. this.listening = false;
  2692. this.acceptQueue = [];
  2693. }
  2694. InternalSocket.boundSockets = {};
  2695. function netOpsUnimplemented()
  2696. {
  2697. debugger;
  2698. }
  2699. function netOpsAlwaysReady()
  2700. {
  2701. return Promise.resolve(0);
  2702. }
  2703. var TcpSocketInodeOps = { readAsync: tcpSocketReadAsync, writeAsync: tcpSocketWriteAsync, close: socketClose };
  2704. var TcpSocketNetOps = { canWrite: socketCanWrite, canRead: socketCanRead, readAvailable: tcpSocketReadAvailable, recv: netOpsUnimplemented, sendto: netOpsUnimplemented, connect: tcpSocketConnect, listen: tcpSocketListen, accept: tcpSocketAccept, bind: socketBind, shutdown: tcpSocketShutdown };
  2705. var UdpSocketInodeOps = { readAsync: netOpsUnimplemented, writeAsync: netOpsUnimplemented, close: socketClose };
  2706. var UdpSocketNetOps = { canWrite: netOpsAlwaysReady, canRead: socketCanRead, readAvailable: netOpsUnimplemented, recv: socketRecv, sendto: socketSendTo, connect: netOpsUnimplemented, listen: netOpsUnimplemented, accept: netOpsUnimplemented, bind: socketBind, shutdown: null };
  2707. var HttpSocketInodeOps = { readAsync: httpSocketReadAsync, writeAsync: httpSocketWriteAsync, close: null };
  2708. var HttpSocketNetOps = { canWrite: netOpsAlwaysReady, canRead: netOpsUnimplemented, readAvailable: null, recv: netOpsUnimplemented, sendto: netOpsUnimplemented, connect: httpSocketConnect, listen: netOpsUnimplemented, accept: netOpsUnimplemented, bind: netOpsUnimplemented, shutdown: netOpsUnimplemented };
  2709. var InternalSocketInodeOps = { readAsync: internalSocketReadAsync, writeAsync: internalSocketWriteAsync, close: internalSocketClose };
  2710. var InternalSocketNetOps = { canWrite: netOpsAlwaysReady, canRead: internalSocketCanRead, readAvailable: null, recv: netOpsUnimplemented, sendto: netOpsUnimplemented, connect: internalSocketConnect, listen: internalSocketListen, accept: internalSocketAccept, bind: internalSocketBind, shutdown: netOpsUnimplemented };
  2711. function cheerpOSSocketOpenInternal(fds, mode, socket)
  2712. {
  2713. var fileData = new CheerpJFileData(null, null, 0, 0, 0, 0, 0, 0);
  2714. // Internal sockets are supported unconditionally
  2715. if(mode == 2)
  2716. {
  2717. fileData.mount = InternalSocketInodeOps;
  2718. fileData.net = InternalSocketNetOps;
  2719. fileData.data = socket? socket : new InternalSocket();
  2720. if (fileData.data.peerFileData)
  2721. {
  2722. var peer = fileData.data.peerFileData;
  2723. peer.data.peerFileData = fileData;
  2724. }
  2725. }
  2726. else
  2727. {
  2728. if(!cjEnableTailscale)
  2729. {
  2730. if (mode != 0)
  2731. return -1;
  2732. fileData.mount = HttpSocketInodeOps;
  2733. fileData.net = HttpSocketNetOps;
  2734. fileData.currRequest = {
  2735. headers: new Headers(),
  2736. url: "",
  2737. method: "",
  2738. currString: "",
  2739. prevChar: "",
  2740. firstFetchCall: true,
  2741. headersNewLines: 0,
  2742. dataIdx: 0,
  2743. nLines: 0,
  2744. };
  2745. }
  2746. else
  2747. {
  2748. if (mode == 1)
  2749. {
  2750. fileData.mount = UdpSocketInodeOps;
  2751. fileData.net = UdpSocketNetOps;
  2752. fileData.data = socket? socket : new cjTailscaleUdpSocket();
  2753. }
  2754. else
  2755. {
  2756. fileData.mount = TcpSocketInodeOps;
  2757. fileData.net = TcpSocketNetOps;
  2758. fileData.data = socket? socket : new cjTailscaleSocket();
  2759. }
  2760. }
  2761. }
  2762. fileData.chunks = [];
  2763. fileData.currData = undefined;
  2764. fileData.currPos = 0;
  2765. fileData.refCount=fileData.refCount+1|0;
  2766. return cheerpOSAllocateFd(fds, fileData);
  2767. }
  2768. function cheerpOSSocketOpen(fds, mode, cb)
  2769. {
  2770. return cb(cheerpOSSocketOpenInternal(fds, mode, undefined));
  2771. }
  2772. function cheerpOSSocketShutdown(fds, fd, how, cb)
  2773. {
  2774. if(fd < 0)
  2775. return cb(-1);
  2776. var fileDesc=fds[fd];
  2777. if(fileDesc == null)
  2778. return cb(-1);
  2779. var fileData=fileDesc.fileData;
  2780. var ret = 0;
  2781. if (!fileData.net || !fileData.net.shutdown)
  2782. return cb(-1);
  2783. return cb(fileData.net.shutdown(fileData, how));
  2784. }
  2785. async function cheerpOSPoll(fds, fd, timeout, cb)
  2786. {
  2787. if(fd < 0)
  2788. return cb(-1);
  2789. var fileDesc=fds[fd];
  2790. if(fileDesc == null)
  2791. return cb(-1);
  2792. var fileData = fileDesc.fileData;
  2793. var canReadPromise;
  2794. if (fileData.net)
  2795. canReadPromise = fileData.net.canRead(fileData);
  2796. else
  2797. canReadPromise = Promise.resolve(0);
  2798. var promises = [];
  2799. var timeoutId = 0;
  2800. if(timeout >= 0)
  2801. {
  2802. const timoutPromise = new Promise(resolve => {
  2803. timeoutId = setTimeout(resolve, timeout);
  2804. });
  2805. promises.push(timoutPromise.then(() => {
  2806. return 0;
  2807. }));
  2808. }
  2809. promises.push(canReadPromise.then(() => {
  2810. if (timeoutId)
  2811. clearTimeout(timeoutId);
  2812. fileData.incomingPromise = null;
  2813. return 1;
  2814. }));
  2815. return cb(await Promise.race(promises))
  2816. }
  2817. function cheerpOSReadAvailable(fds, fd, cb)
  2818. {
  2819. if(fd < 0)
  2820. return cb(-1);
  2821. var fileDesc=fds[fd];
  2822. if(fileDesc == null)
  2823. return cb(-1);
  2824. var tot = 0;
  2825. var data = fileDesc.fileData;
  2826. if (data.net && data.net.readAvailable)
  2827. {
  2828. return cb(data.net.readAvailable(data));
  2829. }
  2830. for(var i = 0; i < data.chunks.length; i++)
  2831. {
  2832. if(data.chunks[i])
  2833. tot += data.chunks[i].length;
  2834. }
  2835. if(data.currData)
  2836. {
  2837. tot += data.currData.length - data.currPos;
  2838. }
  2839. return cb(tot);
  2840. }
  2841. async function cheerpOSSocketConnect(fds, fd, ipAddr, ipPort, cb)
  2842. {
  2843. if(fd < 0)
  2844. return cb(-1);
  2845. var fileDesc=fds[fd];
  2846. if(fileDesc == null)
  2847. return cb(-1);
  2848. var fileData=fileDesc.fileData;
  2849. if (!fileData.net)
  2850. return cb(-1);
  2851. var ret = fileData.net.connect(fileData, ipAddr, ipPort);
  2852. if (ret != 0)
  2853. return cb(-1);
  2854. var ret = await fileData.net.canWrite(fileData);
  2855. return cb(ret);
  2856. }
  2857. function cheerpOSSocketListen(fds, fd, backlog, cb)
  2858. {
  2859. if(fd < 0)
  2860. return cb(-1);
  2861. var fileDesc=fds[fd];
  2862. if(fileDesc == null)
  2863. return cb(-1);
  2864. var fileData=fileDesc.fileData;
  2865. if(!fileData.net)
  2866. return cb(-1);
  2867. return cb(fileData.net.listen(fileData));
  2868. }
  2869. async function cheerpOSSocketAccept(fds, fd, cb)
  2870. {
  2871. if(fd < 0)
  2872. return cb(-1);
  2873. var fileDesc=fds[fd];
  2874. if(fileDesc == null)
  2875. return cb(-1);
  2876. var fileData=fileDesc.fileData;
  2877. if(!fileData.net)
  2878. return cb(-1);
  2879. var newSocket = fileData.net.accept(fileData);
  2880. while (newSocket == null) {
  2881. await fileData.net.canRead(fileData);
  2882. newSocket = fileData.net.accept(fileData);
  2883. }
  2884. var newFd = cheerpOSSocketOpenInternal(fds, (newSocket.socket instanceof InternalSocket) ? 2 : 0, newSocket.socket);
  2885. return cb({newFd,addr:newSocket.addr,port:newSocket.port});
  2886. }
  2887. function cheerpOSSocketBind(fds, fd, localPort, cb)
  2888. {
  2889. if(fd < 0)
  2890. return cb(-1);
  2891. var fileDesc=fds[fd];
  2892. if(fileDesc == null)
  2893. return cb(-1);
  2894. var fileData=fileDesc.fileData;
  2895. if(!fileData.net)
  2896. return cb(-1);
  2897. return cb(fileData.net.bind(fileData, localPort));
  2898. }
  2899. async function cheerpOSSocketSendTo(fds, fd, data, ipAddr, ipPort, cb)
  2900. {
  2901. if(fd < 0)
  2902. return cb(-1);
  2903. var fileDesc=fds[fd];
  2904. if(fileDesc == null)
  2905. return cb(-1);
  2906. var fileData=fileDesc.fileData;
  2907. if(!fileData.net)
  2908. return cb(-1);
  2909. var ret = fileData.net.sendto(fileData, data, ipAddr, ipPort);
  2910. while(ret == cjTailscaleUdpSocket.Eagain) {
  2911. ret = await fileData.net.canWrite(fileData);
  2912. if (ret < 0)
  2913. return cb(-1);
  2914. ret = fileData.data.sendto(fileData, data, ipAddr, ipPort);
  2915. }
  2916. return cb(ret);
  2917. }
  2918. async function cheerpOSSocketRecv(fds, fd, buf, off, bufLen, cb)
  2919. {
  2920. var ret = {length:-1,addr:0,port:0};
  2921. if(fd < 0)
  2922. return cb(ret);
  2923. var fileDesc=fds[fd];
  2924. if(fileDesc == null)
  2925. return cb(ret);
  2926. var fileData=fileDesc.fileData;
  2927. if(!fileData.net)
  2928. return cb(ret);
  2929. ret.length = fileData.net.recv(fileData, buf, off, bufLen, ret);
  2930. while(ret.length == cjTailscaleUdpSocket.Eagain)
  2931. {
  2932. ret.length = await fileData.net.canRead(fileData);
  2933. if (ret.length < 0)
  2934. return cb(ret);
  2935. ret.length = fileData.net.recv(fileData, buf, off, bufLen, ret);
  2936. }
  2937. return cb(ret);
  2938. }
  2939. function cheerpOSResolveHost(hostName, cb)
  2940. {
  2941. if(hostName=='cheerpjhost')
  2942. hostName = "127.0.0.1";
  2943. if(!cjEnableTailscale)
  2944. return cb((192<<24)|(168<<16)|(1<<8)|1);
  2945. cjTailscaleResolve(hostName).then((ip) =>
  2946. {
  2947. var ip0 = ip & 0xff;
  2948. var ip1 = (ip>>8) & 0xff;
  2949. var ip2 = (ip>>16) & 0xff;
  2950. var ip3 = (ip>>24) & 0xff;
  2951. var javaIp = (ip0<<24) | (ip1<<16) | (ip2<<8) | ip3;
  2952. console.log("host ",hostName, " is ",ip0,ip1,ip2,ip3)
  2953. cb(javaIp);
  2954. });
  2955. }
  2956. var cheerpOSMimeMap = { htm: "text/html", html: "text/html", jpeg: "image/jpeg", jpg: "image/jpeg", js: "text/javascript", pdf: "application/pdf", png: "image/png", rtf: "application/rtf", xml: "text/xml" };
  2957. function cheerpOSGetFileBlobFd(fds, fd, path, cb)
  2958. {
  2959. if(fd < 0){
  2960. return cb(null);
  2961. }
  2962. // Compute file length
  2963. var fileRef = {};
  2964. cheerpOSFStat(fds, fd, fileRef, function()
  2965. {
  2966. var len = fileRef.fileLength;
  2967. var buf = new Uint8Array(len);
  2968. // Read the whole file
  2969. cheerpOSRead(fds, fd, buf, 0, len, function(r)
  2970. {
  2971. assert(len == r);
  2972. var mimeType = null;
  2973. var extIndex = path.lastIndexOf(".");
  2974. if(extIndex >= 0){
  2975. var ext = path.substr(extIndex+1);
  2976. var m = cheerpOSMimeMap[ext];
  2977. if(m)
  2978. mimeType = m;
  2979. }
  2980. cheerpOSClose(fds, fd, function(){});
  2981. var blob=new Blob([buf], {type: mimeType});
  2982. return cb(blob);
  2983. });
  2984. });
  2985. }
  2986. function cheerpOSGetFileBlob(fds, path, cb)
  2987. {
  2988. cheerpOSOpen(fds, path, "r", function(fd)
  2989. {
  2990. return cheerpOSGetFileBlobFd(fds, fd, path, cb);
  2991. });
  2992. }
  2993. function cheerpOSWatchFiles(prefix, cb)
  2994. {
  2995. var prefix = cheerpjNormalizePath(prefix)
  2996. if(cb == null)
  2997. delete cosFileWatchPrefixes[prefix];
  2998. else
  2999. cosFileWatchPrefixes[prefix] = cb;
  3000. }
  3001. function cheerpOSSeek(fds, fd, offset, whence, cb)
  3002. {
  3003. if(fd < 0)
  3004. {
  3005. return cb(-1);
  3006. }
  3007. var fdObj = fds[fd];
  3008. assert(fdObj);
  3009. var newOffset = offset;
  3010. if(whence == 0 /*SEEK_SET*/)
  3011. {
  3012. // Nothing to do
  3013. }
  3014. else if(whence == 2 /*SEEK_END*/)
  3015. {
  3016. assert(offset == 0);
  3017. var fileLength = fdObj.fileData.length;
  3018. newOffset = fileLength;
  3019. }
  3020. else
  3021. {
  3022. assert(whence == 1 /*SEEK_CUR*/);
  3023. var fileOffset = fdObj.offset;
  3024. newOffset = fileOffset + offset;
  3025. }
  3026. fdObj.offset = newOffset;
  3027. return cb(newOffset);
  3028. }
  3029. function cheerpOSFStat(fds, fd, fileRef, cb)
  3030. {
  3031. var fileData = fds[fd].fileData;
  3032. assert(fileData);
  3033. fileRef.fileLength = fileData.length;
  3034. fileRef.permType = fileData.permType;
  3035. fileRef.inodeId = fileData.inodeId;
  3036. fileRef.lastModified = fileData.lastModified;
  3037. return cb();
  3038. }
  3039. function cheerpOSGetFullPath(fds, fd, cb)
  3040. {
  3041. var fileData = fds[fd].fileData;
  3042. assert(fileData);
  3043. fullPath = fileData.parent.mountPoint + fileData.path;
  3044. return cb(fullPath);
  3045. }
  3046. function cheerpOSGetFlags(fds, fd, cb)
  3047. {
  3048. if(fd < 0 || fd >= fds.length)
  3049. {
  3050. return cb(-1);
  3051. }
  3052. var fdObj = fds[fd];
  3053. if(fdObj === null)
  3054. {
  3055. return cb(-1);
  3056. }
  3057. var fileData = fdObj.fileData;
  3058. return cb(fileData.flags);
  3059. }
  3060. function cheerpOSSetFlags(fds, fd, value, cb)
  3061. {
  3062. if(fd < 0 || fd >= fds.length)
  3063. {
  3064. return cb(-1);
  3065. }
  3066. var fdObj = fds[fd];
  3067. if(fdObj === null)
  3068. {
  3069. return cb(-1);
  3070. }
  3071. var fileData = fdObj.fileData;
  3072. fileData.flags = value;
  3073. return cb(0);
  3074. }
  3075. function assert(cond)
  3076. {
  3077. if(!cond)
  3078. debugger
  3079. }