Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should force xstat of directory with existing (different) uuid', async () => {
let xstat
await xattr.setAsync(tmpdir, 'user.fruitmix', JSON.stringify({ uuid: uuidArr[0] }))
// xstat = await readXstatAsync(tmpdir)
// expect(xstat.uuid).to.equal(uuidArr[0])
xstat = await forceDriveXstatAsync(tmpdir, uuidArr[2])
expect(xstat).to.deep.equal(pre)
})
})
let xstats = []
await Promise.delay(100)
for (let prop in tree) {
if (tree.hasOwnProperty(prop)) {
let npath = path.join(tmpdir, prop)
// console.log(npath)
if (tree[prop].type === 'directory') {
await mkdirpAsync(npath)
xattr.setAsync(npath, 'user.fruitmix', JSON.stringify({
uuid: tree[prop].uuid
}))
}
else {
await fs.writeFileAsync(npath, '\n')
xattr.setAsync(npath, 'user.fruitmix', JSON.stringify({
uuid: tree[prop].uuid,
magic: 0
}))
}
xstats.push(await readXstatAsync(npath))
}
}
xstats.sort((a, b) => a.name.localeCompare(b.name))
let stats = await fs.lstatAsync(dir)
let mtime = stats.mtime.getTime()
return { mtime, xstats }
}
// drop hash if outdated
if(stats.isFile() && attr.htime && attr.htime !== stats.mtime) {
dirty = true
delete attr.hash
delete attr.htime
}
} catch(e) {
if(e.code !== 'ENODATA' && !(e instanceof SyntaxError)) throw e
dirty = true
attr = { uuid : UUID.v4()}
if(stats.isFile()) attr.magic = fileMagic(target)
}
// save new attr if dirty
if(dirty) await xattr.setAsync(target, FRUITMIX, JSON.stringify(attr))
//remove props not passed to caller
if(stats.isFile() && attr.htime) delete attr.htime
if(stats.isFile() && typeof attr.magic === 'number') delete attr.magic
return Object.assign(stats, attr, {abspath : target})
}
const createFileMapAsync = async ({ size, segmentsize, dirUUID, sha256, name, userUUID}) => {
// fallocate -l 10G bigfile
let folderPath = path.join(paths.get('filemap'), userUUID)
try{
// if(!fs.existsSync(folderPath))
await mkdirpAsync(folderPath)
let taskId = UUID.v4()
let filepath = path.join(folderPath, taskId)
await child.execAsync('fallocate -l ' + size +' ' + filepath)
let segments = []
for(let i = 0; i < Math.ceil(size/segmentsize); i++){
segments.push(0)
}
let attr = { size, segmentsize, segments, dirUUID, sha256, name, userUUID }
await xattr.setAsync(filepath, FILEMAP, JSON.stringify(attr))
return Object.assign({},attr,{ taskid: taskId })
}
catch(e){ throw e }
}
it('should read xstat from file with attr containing uuid, type, htime, and hash', async() => {
let stats = await fs.statAsync(fpath)
xattr.setAsync(fpath, 'user.fruitmix', JSON.stringify({
uuid: uuidArr[1],
hash: S[0].hash,
htime: stats.mtime.getTime(),
magic: 'JPEG'
}))
let xstat = await readXstatAsync(fpath)
expect(xstat).to.deep.equal({
uuid: uuidArr[1],
type: 'file',
name: 'gg_gps.jpg',
mtime: stats.mtime.getTime(),
size: 80603,
magic: 'JPEG',
hash: S[0].hash
it('should return hash if hash is valid', async () => {
let stats = await fs.lstatAsync(filepath)
let attr = {
hash: sha256,
htime: stats.mtime.getTime()
}
await xattr.setAsync(filepath, 'user.fruitmix', JSON.stringify(attr))
let hash = await repo.storeFileAsync(filepath)
expect(hash).to.equal(sha256)
})
it('should drop non-json attr', async () => {
await xattr.setAsync(tmpdir, 'user.fruitmix', 'hello')
let stats = await fs.statAsync(tmpdir)
let xstat = await readXstatAsync(tmpdir)
expect(xstat).to.deep.equal({
uuid: uuidArr[2],
type: 'directory',
name: 'tmptest',
mtime: stats.mtime.getTime()
})
})
})
if(segments[start] === 1){
console.log('该段以上传', segments)
throw new E.EEXIST()
}
let segmentSize = attr.segmentsize
let segmentLength = segments.length > start + 1 ? segmentSize : (attr.size - start * segmentSize)
let position = attr.segmentsize * start
let updater = new SegmentUpdater(fpath, req, position, segmentHash, segmentLength)
await updater.startAsync()
try{
attr = JSON.parse(await xattr.getAsync(fpath, FILEMAP))
attr.segments[start] = 1
await xattr.setAsync(fpath, FILEMAP, JSON.stringify(attr))
if(attr.segments.includes(0)) return false
let fname = await autoRenameAsync(attr.userUUID, attr.dirUUID, attr.name)
console.log('----------------' + fname)
await moveFileMapAsync(attr.userUUID, attr.dirUUID, fname, fpath, attr.sha256)
return true
}catch(e){
console.log(e)
throw e
}
}