diff --git a/docker/setup/files/trash.mjs b/docker/setup/files/trash.mjs index 1093e01..c3d01ce 100644 --- a/docker/setup/files/trash.mjs +++ b/docker/setup/files/trash.mjs @@ -1,4 +1,4 @@ -import { promises as fs } from 'node:fs'; +import { constants, promises as fs } from 'node:fs'; import path from 'node:path'; import { randomUUID, createHash } from 'node:crypto'; import { fail, inside, normalizePath } from './paths.mjs'; @@ -22,7 +22,14 @@ export function createTrash({policy,guard,sizes,git,now=Date.now,recordEvent=()= const marker=await fs.lstat(path.join(policy.workspace,'.git'));let directory; if(marker.isDirectory())directory=path.join(policy.workspace,'.git'); else if(marker.isFile()){const result=await git.command(policy.workspace,['rev-parse','--absolute-git-dir']);directory=result.stdout.trim();} - if(directory){const info=path.join(directory,'info');await fs.mkdir(info,{recursive:true});const exclude=path.join(info,'exclude'),before=await maybeStat(exclude);if(before?.isSymbolicLink())fail('invalid','Git’s exclude file is a link. Add .t3-trash/ in Git before using workspace Trash.');const content=await fs.readFile(exclude,'utf8').catch(error=>{if(error.code==='ENOENT')return '';throw error;});if(!content.split('\n').includes('.t3-trash/'))await fs.appendFile(exclude,(content&&!content.endsWith('\n')?'\n':'')+'.t3-trash/\n');} + if(directory){const info=path.join(directory,'info');await fs.mkdir(info,{recursive:true});const exclude=path.join(info,'exclude'),before=await maybeStat(exclude);if(before?.isSymbolicLink())fail('invalid','Git’s exclude file is a link. Add .t3-trash/ in Git before using workspace Trash.'); + // Read and append through one O_NOFOLLOW descriptor so a link swapped in + // after the check above cannot redirect the write, as in text.mjs. + let handle;try{handle=await fs.open(exclude,constants.O_RDWR|constants.O_CREAT|constants.O_NOFOLLOW|constants.O_APPEND,0o666);} + catch(error){if(error.code==='ELOOP')fail('invalid','Git’s exclude file is a link. Add .t3-trash/ in Git before using workspace Trash.');throw error;} + try{const stat=await handle.stat({bigint:true});if(!stat.isFile())fail('invalid','Git’s exclude file is not a regular file. Add .t3-trash/ in Git before using workspace Trash.'); + const content=await handle.readFile('utf8');if(!content.split('\n').includes('.t3-trash/'))await handle.write((content&&!content.endsWith('\n')?'\n':'')+'.t3-trash/\n',null,'utf8');} + finally{await handle.close();}} } return root; }; diff --git a/docker/setup/files/uploads.mjs b/docker/setup/files/uploads.mjs index c243d6c..782abee 100644 --- a/docker/setup/files/uploads.mjs +++ b/docker/setup/files/uploads.mjs @@ -95,6 +95,9 @@ export function createUploads({policy,guard,trash,now=Date.now,statfs=value=>fs. const previous=[...sessions.values()].find(row=>!row.result&&row.dest===dest&&row.relative===item.path&&row.size===item.size&&row.lastModified===item.lastModified); if(previous){const actual=await maybeStat(previous.temp);if(actual?.isFile()&&!actual.isSymbolicLink()&&identity(actual)===previous.identity&&Number(actual.size)<=previous.size){const landed=Number(actual.size);if(landed>previous.received)release(previous,landed-previous.received);previous.received=landed;previous.at=now();results.push({...previous,conflict:await conflict(previous)});continue;}fail('changed','An unfinished upload changed on disk. Cancel it before restarting.');} const parent=await parents(dest,item.parts.slice(0,-1),input),target=path.join(parent,item.parts.at(-1)),id='u_'+randomUUID(),temp=path.join(parent,'.t3-upload-'+id+'.part'); + // parents() may resolve into another filesystem, so measure the one the + // staging file will actually land on, the same parent chunks check. + await enough(parent,item.size+reservedBytes); const row=await guard.run(input,[{path:target,mode:'write',missing:true,inUse:false}],async([target])=>{ const handle=await fs.open(path.join(target.pin.parent,path.basename(temp)),'wx',0o600); try{const stat=await handle.stat({bigint:true});return {id,dest,parent:target.pin.parent,parentIdentity:identity(target.pin.stat),relative:item.path,target:target.path,temp,identity:identity(stat),size:item.size,lastModified:item.lastModified,received:0,at:now(),existingRevision:target.stat?statRevision(target.stat):null};}finally{await handle.close();} diff --git a/tests/files-trash.test.mjs b/tests/files-trash.test.mjs index 21e1620..1ec7d84 100644 --- a/tests/files-trash.test.mjs +++ b/tests/files-trash.test.mjs @@ -22,3 +22,22 @@ test('emptying Trash needs confirmation and deletes only the explicitly selected const {workspace,trash}=await filesFixture(t),a=path.join(workspace,'a'),b=path.join(workspace,'b');await fs.writeFile(a,'a');await fs.writeFile(b,'b');const first=await trash.move({paths:[a]}),second=await trash.move({paths:[b]}); await assert.rejects(trash.empty({ids:first.ids}),{code:'conflicts'});await trash.empty({ids:first.ids,confirmed:true});assert.deepEqual((await trash.list()).items.map(row=>row.id),second.ids); }); + +test('workspace Trash refuses to append through an exclude link',async t=>{ + const f=await filesFixture(t); + await fs.mkdir(path.join(f.workspace,'.git','info'),{recursive:true}); + const victim=path.join(f.root,'victim');await fs.writeFile(victim,'precious'); + await fs.symlink(victim,path.join(f.workspace,'.git','info','exclude')); + const target=path.join(f.workspace,'a');await fs.writeFile(target,'a'); + await assert.rejects(f.trash.move({paths:[target]}),{code:'invalid'}); + assert.equal(await fs.readFile(victim,'utf8'),'precious');assert.equal(await fs.readFile(target,'utf8'),'a'); +}); + +test('workspace Trash records its ignore line in a regular exclude file',async t=>{ + const f=await filesFixture(t); + await fs.mkdir(path.join(f.workspace,'.git','info'),{recursive:true}); + await fs.writeFile(path.join(f.workspace,'.git','info','exclude'),'*.log\n'); + const target=path.join(f.workspace,'a');await fs.writeFile(target,'a'); + const moved=await f.trash.move({paths:[target]});assert.equal(moved.ids.length,1); + assert.equal(await fs.readFile(path.join(f.workspace,'.git','info','exclude'),'utf8'),'*.log\n.t3-trash/\n'); +}); diff --git a/tests/files-uploads.test.mjs b/tests/files-uploads.test.mjs index 1d5d579..9fbe649 100644 --- a/tests/files-uploads.test.mjs +++ b/tests/files-uploads.test.mjs @@ -145,3 +145,18 @@ test('fresh module recovery reserves all unfinished uploads before accepting chu assert.ok(results.every(row=>row.status==='rejected'&&row.reason.code==='no-space')); const ids=started.uploads.map(row=>row.id);await recovered.cancel({ids});await f.uploads.cancel({ids}); }); + +test('start measures the mount the upload lands on, not the destination',async t=>{ + const f=await filesFixture(t),stick=path.join(f.workspace,'stick');await fs.mkdir(stick); + // The destination looks roomy but the stick subdir is nearly full. + const statfs=async value=>value.split(path.sep).includes('stick')?{bavail:1024,bsize:1024}:{bavail:1024**3+6*1024**3,bsize:1}; + const uploads=createUploads({policy:f.policy,guard:f.guard,trash:f.trash,statfs}),big=5*1024**3; + await assert.rejects(uploads.start({dest:f.workspace,items:[item('stick/big.bin',big)]}), + error=>error.code==='no-space'&&error.fields.availableBytes===1024*1024&&error.fields.requiredBytes===big); + // Nothing staged on the stick and nothing left behind in this start. + assert.deepEqual(await fs.readdir(stick),[]);assert.equal(uploads.list().length,0); + // The failed start reserved nothing: a 2 GiB upload still fits the 6 GiB of + // headroom, which a leaked 5 GiB reservation would have denied. + const next=await uploads.start({dest:f.workspace,items:[item('after.bin',2*1024**3)]}); + await uploads.cancel({ids:next.uploads.map(row=>row.id)}); +});