So this answer got me most of what I needed:
https://stackoverflow.com/a/49433633
Basically what I'm trying to do is pre-allocate disk space for a set of files in their given directories. I'm ensuring that each directory and sub directory is created recursively, and I do actually have files being saved to disk that appear to be of the right size. The problem I'm seeing is that while the file created is 100GB, it's only taking up 4KB on the actual disk. Is there something I can do so it actually occupies the full space? For reference, here's the method I've created to pre-allocate disk space for a file:
export const preallocateFile = (rootPath: string, file: File): Promise<boolean | Error> => {
const { Filelocation: fileLocation, FileName: fileName, FileSize: size } = file
return new Promise((resolve, reject) => {
console.log('allocate!', rootPath, file)
// process asynchronously
setTimeout(() => {
try {
// Create the necessary directories ahead of writing the file
fs.mkdirSync(`${rootPath}${fileLocation}`, { recursive: true })
// Open the file for writing; 'w' creates the file
// (if it doesn't exist) or truncates it (if it exists)
const allocatedFile = fs.openSync(`${rootPath}${fileLocation}${fileName}`, 'w')
if (size > 0) {
// Write one byte (with code 0) at the desired offset
// This forces the expanding of the file and fills the gap
// with characters with code 0
fs.writeSync(allocatedFile, Buffer.alloc(1), 0, 1, size - 1)
}
// Close the file to commit the changes to the file system
fs.closeSync(allocatedFile)
console.log('about to resolve!')
resolve(true)
} catch (error) {
console.log('nope!', error)
reject(error)
}
// Create the file after the processing of the current JavaScript event loop
}, 0)
})
}
While my code is working, if I'm not actually occupying disk space then the pre-allocation will not work properly.