From 4846a5d69d7d9917eeb33fb9313e3aca63f60602 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 3 Sep 2022 13:37:49 +0300 Subject: [PATCH] fixing up file store (needs refactoring and cleanup)... Signed-off-by: Alex A. Naanou --- pwiki/page.js | 16 +++++++++++--- pwiki/store/file.js | 52 +++++++++++++++++++++++++++++++++++---------- pwiki2.html | 7 ++++-- pwiki2.js | 3 ++- 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/pwiki/page.js b/pwiki/page.js index 20c29ad..cecdd82 100755 --- a/pwiki/page.js +++ b/pwiki/page.js @@ -187,11 +187,16 @@ object.Constructor('BasePage', { return pwpath.dirname(this.path) }, //set dir(value){ }, - get title(){ return async function(){ - return (await this.data).title - ?? this.path }.call(this) }, + /*/ XXX TITLE... + // NOTE: .__title is intentionally not persistent... + __title: undefined, + get title(){ + return this.__title + ?? this.path }, set title(value){ + this.__title = value this.__update__({title: value}) }, + //*/ get isPattern(){ return this.path.includes('*') }, @@ -265,6 +270,11 @@ object.Constructor('BasePage', { // single page... // XXX ENERGETIC... var res = await this.store.get(this.path, !!this.strict, !!await this.energetic) + /*/ XXX TITLE... + // load the title if set... + res.title + ?? (this.__title = res.title) + //*/ return typeof(res) == 'function' ? res.bind(this) : res }).call(this) }, diff --git a/pwiki/store/file.js b/pwiki/store/file.js index 41821a8..6bf9c15 100755 --- a/pwiki/store/file.js +++ b/pwiki/store/file.js @@ -9,6 +9,7 @@ var fs = require('fs') var glob = require('glob') +var cp = require('child_process') var object = require('ig-object') var types = require('ig-types') @@ -18,6 +19,8 @@ var pwpath = require('../path') var base = require('./base') + + //--------------------------------------------------------------------- // @@ -97,7 +100,8 @@ async function(base, sub, options){ var mkdir = module.mkdir = async function(base, sub, options){ - if(typeof(sub) != 'string'){ + if(typeof(sub) != 'string' + && !(sub instanceof Array)){ options = sub ?? options sub = base base = null } @@ -146,7 +150,7 @@ async function(base, sub, data, options){ target = pwpath.join(target, index) } // create path / parts of path... } else { - var levels = pwpath.split(target) + var levels = pwpath.split(sub) levels.pop() // ensure the parent dir exists... await module.mkdir( @@ -524,10 +528,16 @@ module.FileStoreRO = { // XXX __path__: 'data/fs', + __pwiki_path__: '/.pwiki/', + __metadata_path__: '$PWIKI/metadata', // XXX should this be "index" or ".index"??? __directory_text__: '.index', + expandPWikiPath: function(...path){ + return pwpath.join(...path) + .replace(/\$PWIKI/, this.__pwiki_path__) }, + // XXX do we remove the extension??? // XXX cache??? __paths__: async function(){ @@ -548,12 +558,14 @@ module.FileStoreRO = { && path }, __get__: async function(path){ var p = pwpath.join(this.__path__, path) + var m = this.expandPWikiPath(this.__path__, this.__metadata_path__, path) var {atimeMs, mtimeMs, ctimeMs, birthtimeMs} = await fs.promises.stat(p) return { atime: atimeMs, mtime: mtimeMs, ctime: ctimeMs, - text: await module.read(p, {index: this.__directory_text__}) + text: await module.read(p, {index: this.__directory_text__}), + ...JSON.parse(await module.read(m, {index: this.__directory_text__}) || '{}'), } }, __update__: function(){}, @@ -569,12 +581,14 @@ module.FileStore = { // XXX __path__: 'data/fs', - __backup_path__: '/.pwiki/backup', - __lock_path__: '/.pwiki/lock', + __pwiki_path__: '/.pwiki', + __backup_path__: '$PWIKI/backup', + __lock_path__: '$PWIKI/lock', - // XXX should this be "index" or ".index"??? __directory_text__: '.index', + // prevent more than one handler to write to a store... + // __clear_lock__: [ `SIGINT`, `SIGUSR1`, @@ -586,17 +600,20 @@ module.FileStore = { //`uncaughtException`, ], __exit_lock_handler: undefined, - // prevent more than one handler to write to a store... ensureLock: async function(){ var that = this - var lock = this.__path__ + this.__lock_path__ + var lock = this.expandPWikiPath(this.__path__, this.__lock_path__) // check lock... if(fs.existsSync(lock)){ if(await module.read(lock) != process.pid){ throw new Error('attempting to write to a locked store:', this.__path__) } // set lock... } else { - module.update(lock, `${process.pid}`) + await module.update(lock, `${process.pid}`) + // keep the pwiki dir hidden on windows... + if(process.platform == 'win32'){ + cp.execSync('attrib +h '+ + this.expandPWikiPath(this.__path__, this.__pwiki_path__)) } this.__exit_lock_handler = this.__exit_lock_handler // NOTE: this must be sync as deferred calls might @@ -611,12 +628,25 @@ module.FileStore = { // XXX do we write all the data or only the .text??? __update__: async function(path, data, mode='update'){ this.ensureLock() + // metadata... + module.update( + this.__path__, + this.expandPWikiPath(this.__metadata_path__, path), + JSON.stringify(data), + {index: this.__directory_text__}) + // text... return module.update( this.__path__, path, data.text, {index: this.__directory_text__}) }, __delete__: async function(path){ this.ensureLock() + // metadata... + module.clear( + this.__path__, + this.expandPWikiPath(this.__metadata_path__, path), + {index: this.__directory_text__}) + // text... return module.clear( this.__path__, path, {index: this.__directory_text__}) }, @@ -634,7 +664,7 @@ module.FileStore = { this.__path__, path, { index: this.__directory_text__, - backup: this.__backup_path__, + backup: this.expandPWikiPath(this.__backup_path__), ...options, }) }, restore: async function(path='**', options={}){ @@ -643,7 +673,7 @@ module.FileStore = { this.__path__, path, { index: this.__directory_text__, - backup: this.__backup_path__, + backup: this.expandPWikiPath(this.__backup_path__), ...options, }) }, } diff --git a/pwiki2.html b/pwiki2.html index eff8b29..f33b664 100755 --- a/pwiki2.html +++ b/pwiki2.html @@ -173,12 +173,15 @@ require(['./browser'], function(browser){ pwiki .onBeforeNavigate(function(){ saveNow() }) - .onNavigate(function(){ + .onNavigate(async function(){ // NOTE: we do not need to directly update location.hash here as // that will push an extra history item... history.replaceState( {path: this.location}, - this.title, + // XXX TITLE .title must be sync as history will not wait for + // the value even it await is given... + //this.title, + this.path, '#'+this.location +(this.hash ? '#'+this.hash diff --git a/pwiki2.js b/pwiki2.js index 2066a91..6590af3 100755 --- a/pwiki2.js +++ b/pwiki2.js @@ -24,8 +24,9 @@ * - count + block-offset (preferred) * - count + elem-offset * - from + to +* XXX TITLE revise how title is handled... +* ...do we need a separate title and path??? * XXX revise/update sort... -* XXX fs store: metadata and cache... * XXX prevent paths from using reserved chars like: ":", "#", ... * XXX ASAP: MetaStore: need to correctly integrate the following store * methods: