Skip to content

Commit

Permalink
fix: hashed names
Browse files Browse the repository at this point in the history
  • Loading branch information
stevennevins committed Nov 30, 2023
1 parent a796fd6 commit 33dc31e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
13 changes: 5 additions & 8 deletions src/FileSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ contract FileSystem is IFileSystem {
bytes32[] calldata _inodeChecksums
) external returns (bytes32 directoryChecksum) {
if (_fileNames.length != _inodeChecksums.length) revert LengthMismatch();
bytes32[] memory hashedFiles = hashFiles(_fileNames, _inodeChecksums);
bytes memory concatenatedFiles = concatenateFiles(_fileNames, _inodeChecksums);

for (uint256 i; i < _inodeChecksums.length; i++) {
if (!inodeExists(_inodeChecksums[i])) revert InodeNotFound();
}
directoryChecksum = keccak256(
bytes.concat(bytes1(uint8(InodeType.Directory)), keccak256(abi.encodePacked(hashedFiles)))
);
directoryChecksum = keccak256(bytes.concat(bytes1(uint8(InodeType.Directory)), keccak256(concatenatedFiles)));
if (inodeExists(directoryChecksum)) return directoryChecksum;
inodes[directoryChecksum].directory = Directory(_fileNames, _inodeChecksums);
emit DirectoryCreated(directoryChecksum, _fileNames, _inodeChecksums);
Expand Down Expand Up @@ -127,18 +125,17 @@ contract FileSystem is IFileSystem {
/**
* @inheritdoc IFileSystem
*/
function hashFiles(
function concatenateFiles(
string[] calldata _fileNames,
bytes32[] calldata _filePointers
) public pure returns (bytes32[] memory hashedFiles) {
) public pure returns (bytes memory concatenatedFiles) {
uint256 length = _fileNames.length;
hashedFiles = new bytes32[](length);
bytes memory filename;
for (uint256 i; i < length; i++) {
filename = bytes(_fileNames[i]);
if (filename.length == 0) revert InvalidFileName();
if (_containsForbiddenChars(filename)) revert InvalidCharacter();
hashedFiles[i] = keccak256(bytes.concat(keccak256(filename), keccak256(bytes.concat(_filePointers[i]))));
concatenatedFiles = abi.encodePacked(concatenatedFiles, keccak256(filename), _filePointers[i]);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/IFileSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ interface IFileSystem {
* @notice Hashes a list of file names in the directory
* @param _fileNames List of file names
* @param _inodeChecksums List of checksums for the inodes
* @return Hashed names
* @return The concatenated files
*/
function hashFiles(
function concatenateFiles(
string[] calldata _fileNames,
bytes32[] calldata _inodeChecksums
) external view returns (bytes32[] memory);
) external view returns (bytes memory);

/**
* @notice Mapping of checksum pointer to Inode struct
Expand Down
4 changes: 2 additions & 2 deletions test/FileSystem/CreateDirectory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ contract CreateDirectory is FileSystemTest {
bytes internal metadata;
bytes internal fileContent;
bytes32[] internal chunkChecksums;
bytes32[] internal hashedFiles;
bytes internal hashedFiles;
bytes32 internal fileChecksum;

function setUp() public override {
Expand All @@ -23,7 +23,7 @@ contract CreateDirectory is FileSystemTest {

function test_CreateDirectory() public {
bytes32 checksum = fileSystem.createDirectory(fileNames, filePointers);
hashedFiles = fileSystem.hashFiles(fileNames, filePointers);
hashedFiles = fileSystem.concatenateFiles(fileNames, filePointers);
checksum = keccak256(
abi.encodePacked(bytes1(uint8(InodeType.Directory)), keccak256(abi.encodePacked(hashedFiles)))
);
Expand Down
4 changes: 2 additions & 2 deletions test/FileSystem/ReadDirectory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.8.23;
import "test/FileSystem/FileSystemTest.t.sol";

contract ReadDirectory is FileSystemTest {
bytes32[] internal hashedFiles;
bytes internal hashedFiles;
bytes internal metadata;
bytes internal fileContent;
bytes32[] internal chunkChecksums;
Expand All @@ -24,7 +24,7 @@ contract ReadDirectory is FileSystemTest {

function test_ReadDirectory() public {
fileSystem.createDirectory(fileNames, filePointers);
hashedFiles = fileSystem.hashFiles(fileNames, filePointers);
hashedFiles = fileSystem.concatenateFiles(fileNames, filePointers);
checksum = keccak256(
abi.encodePacked(bytes1(uint8(InodeType.Directory)), keccak256(abi.encodePacked(hashedFiles)))
);
Expand Down

0 comments on commit 33dc31e

Please sign in to comment.