or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-9/

File Size Manager

Build a utility function that manages file sizes in a memory filesystem by implementing truncation operations.

Requirements

Your task is to create a function resizeFile that takes a filesystem instance, a file path, and a target size, and resizes the file to exactly that size. The function should:

  1. Resize the file at the given path to the specified number of bytes
  2. When increasing file size, the new space should be filled with zeros
  3. When decreasing file size, content should be truncated from the end
  4. Return the new size of the file after the operation
  5. Throw an error if the file doesn't exist

Test Cases

  • Given a file with content "Hello, World!" (13 bytes), resizing to 5 bytes results in "Hello" @test
  • Given a file with content "Hi" (2 bytes), resizing to 10 bytes results in "Hi" followed by 8 zero bytes @test
  • Given a file with content "Test" (4 bytes), resizing to 0 bytes results in an empty file @test
  • Attempting to resize a non-existent file throws an error @test

Implementation

@generates

API

/**
 * Resizes a file to the specified target size.
 *
 * @param {Object} fs - The filesystem instance (metro-memory-fs)
 * @param {string} filePath - The path to the file to resize
 * @param {number} targetSize - The desired size in bytes
 * @returns {number} The new size of the file
 * @throws {Error} If the file doesn't exist
 */
function resizeFile(fs, filePath, targetSize) {
  // Implementation here
}

module.exports = { resizeFile };

Dependencies { .dependencies }

metro-memory-fs { .dependency }

Provides in-memory filesystem operations for testing.

@satisfied-by