cppadvanced60 minutes

Advanced File System Simulator with Permissions and Command Parsing

Create a mini file system simulator in C++ that supports file creation, deletion, directory navigation, and permission handling with a command-line interface.

Challenge prompt

Build a command-line based file system simulator in C++ that allows users to execute commands to create files and directories, navigate directories, delete files/directories, and manage user permissions (read, write, execute). The file system should be represented in-memory with appropriate data structures, supporting nested directories. Implement commands including mkdir, touch, cd, ls, rm, chmod, and stat. Permissions should be checked on all applicable operations, and the program should provide feedback for invalid operations or permission denials.

Guidance

  • Represent the file system as a tree data structure with nodes for directories and files.
  • Implement permission checks for each command based on user roles and file permissions.
  • Parse and handle complex command input while maintaining state of the current working directory.

Hints

  • Use enums or bit flags to represent permissions and simplify permission checking logic.
  • Design a base class or struct for file system nodes with derived classes or types for files and directories.
  • Consider using a stack or string manipulation to handle path resolution for 'cd' and related commands.

Starter code

#include <iostream>
#include <string>
#include <vector>
#include <memory>

// Define Permission flags
enum Permission {
  READ = 1 << 0,
  WRITE = 1 << 1,
  EXECUTE = 1 << 2
};

// Base class for FileSystem node
class FSNode {
public:
    std::string name;
    int permissions; // bit flags
    FSNode* parent;

    FSNode(const std::string& name, int perms, FSNode* parent)
        : name(name), permissions(perms), parent(parent) {}

    virtual bool isDirectory() const = 0;
    virtual ~FSNode() {}
};

// TODO: Implement Directory and File classes inheriting FSNode
// TODO: Implement commands and file system logic

int main() {
    // TODO: Initialize root directory and simulate command input and output loop
    std::cout << "File system simulator started. Enter commands:\n";
    return 0;
}

Expected output

File system simulator started. Enter commands: > mkdir projects > cd projects > touch readme.txt > ls readme.txt > chmod readme.txt 6 > stat readme.txt Name: readme.txt Permissions: Read, Write > cd .. > rm projects Error: Permission denied

Core concepts

file system simulationbitwise operationscommand parsingtree data structures

Challenge a Friend

Send this duel to someone else and see if they can solve it.