Skip to content

Latest commit

History

32 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Network File Editor (NFE)

A sophisticated, multi-threaded client-server file management system that implements four distinct concurrency control models for secure, collaborative file access. This system allows multiple clients to view, edit, upload, download, and manage files in a shared directory with configurable access policies, ensuring data integrity and preventing conflicts through advanced locking mechanisms.

Table of Contents

Key Features

  • Four Concurrency Models: Implements EREW, CREW, ERCW, and CRCW models for flexible access control.
  • Real-Time Lock Status: Detailed reporting of file lock states, including lock type (READ/WRITE) and active users.
  • Robust Server Architecture: Handles abrupt client disconnections gracefully, with automatic cleanup of stale locks.
  • External Editor Integration: Seamless integration with nano or vim for remote file editing via the dedicated client.
  • Thread-Safe Logging: Comprehensive logging with timestamps, user tracking, and color-coded console output for easy monitoring.
  • Multi-Threaded Design: Efficiently manages multiple concurrent clients using C++ threads and mutexes.
  • Cross-Platform Compatibility: Designed for Linux/Unix systems with C++17 support.
  • Security Features: Username authentication, input validation, and access control based on concurrency models.
  • File Operations: Full CRUD (Create, Read, Update, Delete) support for files and directories.
  • Automatic Directory Creation: Server auto-creates the shared directory if it doesn't exist.

Concurrency Models

The system supports four classical concurrency control models, each balancing safety and performance differently:

  1. EREW (Exclusive Read Exclusive Write):

    • Only one user can access a file at a time (read or write).
    • Maximum safety, minimum concurrency.
    • Ideal for critical files requiring strict serialization.
  2. CREW (Concurrent Read Exclusive Write):

    • Multiple users can read simultaneously.
    • Only one user can write at a time, blocking all reads during writing.
    • Balances safety and performance for read-heavy workloads.
  3. ERCW (Exclusive Read Concurrent Write):

    • Only one user can read at a time.
    • Multiple users can write simultaneously.
    • Suitable for write-heavy scenarios with occasional reads.
  4. CRCW (Concurrent Read Concurrent Write):

    • Maximum concurrency: multiple users can read and write simultaneously.
    • Minimal restrictions, highest performance.
    • Best for collaborative environments with low conflict risk.

Available Commands

CommandSyntaxDescription
LIST / LSLISTDisplay all files in the shared directory with details (size, permissions, owner, modified time).
INFO / EXIFTOOLINFO <filename>Show detailed file metadata (size, permissions, owner, timestamps, lock status).
VIEW / CAT / SHOWVIEW <filename>Display the complete contents of a file.
EDIT / WRITE / NANOEDIT <filename>Open an external editor (nano/vim) to modify a file (client-side only).
UPLOAD / PUTUPLOAD <local_file> <remote_file>Upload a local file to the shared directory.
DOWNLOAD / GETDOWNLOAD <remote_file> <local_file>Download a file from the shared directory to local machine.
DELETE / RM / DELDELETE <filename>Remove a file from the shared directory.
MKDIR / MKDMKDIR <dirname>Create a new directory in the shared directory.
RMDIR / RMDRMDIR <dirname>Remove an empty directory from the shared directory.
TOUCH / CREATETOUCH <filename>Create a new empty file.
LOCKWRITE / LOCKLOCKWRITE <filename>Acquire an exclusive write lock on a file.
UNLOCKWRITE / UNLOCKUNLOCKWRITE <filename>Release the write lock on a file.
LOCKSTATUS / FILESTATUSLOCKSTATUS <filename>Check the current lock status and access permissions for a file.
HELPHELPDisplay the command reference menu.
EXIT / QUITEXITDisconnect from the server.

Prerequisites

  • Operating System: Linux/Unix-based system (tested on Kali Linux, Ubuntu, Debian).
  • Compiler: C++17 compatible compiler (e.g., g++ 7.0+).
  • Build Tools: make utility.
  • Libraries: POSIX threads (pthread), readline library (libreadline-dev).
  • Editors: nano or vim for file editing (auto-detected by client).
  • Network Tools: netcat (optional, for basic client connections).
  • Enhanced CLI: rlwrap (recommended for better interactive experience with netcat).

Install prerequisites on Debian/Ubuntu/Kali:

sudo apt-get update
sudo apt-get install -y build-essential libpthread-stubs0-dev libreadline-dev libc6-dev make nano vim rlwrap

Installation & Compilation

Build Instructions

  1. Clone or download the project:

    git clone https://github.com/TheLeopard65/Network-File-Editor.git
    cd Network-File-Editor
  2. Compile the project:

    make clean
    make
  3. Verify compilation:

    ls -la nfe_server nfe_client
    • nfe_server: The server executable.
    • nfe_client: The client executable with editor integration.

Build Options

  • Debug Build (default): make - Includes debug symbols and no optimizations.
  • Release Build: make release - Optimized for performance, no debug symbols.
  • Clean Build: make clean - Removes all compiled binaries and logs.

Running the System

Starting the Server

The server can be started with default settings or customized via command-line arguments.

# Basic usage (defaults: port 2121, EREW model, ./share/ directory)
./nfe_server
# Custom configuration
./nfe_server -p 3333 -d /path/to/shared/dir -m CREW
# Command-line options:# -p, --port PORT Server port (default: 2121)# -d, --directory DIR Shared directory path (default: ./share/)# -m, --model MODEL Concurrency model: EREW, CREW, ERCW, CRCW (default: EREW)# -h, --help Display help message

Server Logs: All server activity is logged to NFE-Server.log with timestamps and user tracking.

Connecting Clients

Method 1: Dedicated Client (Recommended)

./nfe_client [server_ip] [port]
# Examples:
./nfe_client # Connect to localhost:2121
./nfe_client 192.168.1.100 3333 # Connect to specific IP/port
./nfe_client -h # Show client help

Method 2: Using netcat (Basic)

nc localhost 2121

Method 3: Using netcat with rlwrap (Enhanced Interactive Experience)

rlwrap nc localhost 2121

Note: rlwrap provides arrow key support, command history, and tab completion for a much better CLI experience.

Authentication

Upon connection, clients must provide a username (alphanumeric, underscores, hyphens only, max 50 characters). The server then displays the active concurrency model and available commands.

Usage Examples

Basic File Operations

# List files
LIST
# View file contents
VIEW example.txt
# Get file information
INFO example.txt
# Create a new file
TOUCH newfile.txt
# Create a directory
MKDIR myfolder
# Upload a local file
UPLOAD /local/path/file.txt remote_file.txt
# Download a file
DOWNLOAD remote_file.txt /local/path/file.txt

Editing Files

# Edit with external editor
EDIT example.txt

Note: This acquires a write lock, downloads the file, opens it in nano/vim, uploads changes, and releases the lock automatically.

Lock Management

# Check lock status
LOCKSTATUS example.txt
# Manual lock acquisition (for advanced users)
LOCKWRITE example.txt
# Manual lock release
UNLOCKWRITE example.txt

Lock Status Examples

Unlocked File:

[+] Current File Lock Status:
>>> UNLOCKED
>>> You can acquire WRITE lock
>>> You can acquire READ lock

File Locked for Writing (EREW/CREW):

[+] Current File Lock Status:
>>> LOCKED for WRITE Access by User: leopard
>>> WRITE access is currently restricted
>>> READ access is currently restricted

File Locked for Reading (CREW):

[+] Current File Lock Status:
>>> LOCKED for READ Access by User: alex, kali
>>> WRITE access is currently restricted
>>> You can acquire READ lock

File Locked for Concurrent Writing (ERCW):

[+] Current File Lock Status:
>>> LOCKED for CONCURRENT WRITE Access by User: leopard, alex (Total: 2)
>>> You can acquire WRITE lock
>>> READ access is currently restricted

Multi-User Collaboration Example

  1. User 1 (leopard) starts editing:

    EDIT document.txt

    File is locked for writing.

  2. User 2 (alex) checks status:

    LOCKSTATUS document.txt

    Shows locked by leopard.

  3. User 2 tries to edit:

    EDIT document.txt

    Access denied based on concurrency model.

  4. User 1 finishes editing: Lock automatically released after upload.

  5. User 2 can now edit:

    EDIT document.txt

Troubleshooting

IssueSymptomSolution
Compilation failsg++: command not foundInstall build-essential: sudo apt-get install build-essential
Linker errorsundefined reference to readlineInstall libreadline-dev: sudo apt-get install libreadline-dev
Server won't startBind failedCheck if port is in use: netstat -tlnp | grep :2121
Client can't connectConnection refusedEnsure server is running and IP/port are correct
Arrow keys show ^[[DUsing nc without wrapperUse rlwrap nc localhost 2121
File permissionsPermission deniedCheck shared directory permissions on server
Editor not foundFailed to execute editorInstall nano or vim: sudo apt-get install nano vim
Stale locksFiles remain locked after crashServer auto-cleans locks after 5 minutes (configurable)
High CPU usageMany concurrent clientsReduce client connections or increase server resources
Log file grows largeNFE-Server.log too bigRotate logs manually or implement log rotation

Common Error Messages

  • [-] ERROR: Cannot read file - Access denied!: File is locked by another user in a restrictive model.
  • [-] ERROR: Cannot acquire write lock for <file>: Another user holds an incompatible lock.
  • [-] ERROR: Username contains invalid characters: Use only alphanumeric, underscore, hyphen.
  • [-] ERROR: Failed to execute editor: Install nano or vim.

Performance Tuning

  • Server: Increase ulimit -n for more open file descriptors if handling many clients.
  • Client: For large files, increase socket buffer sizes in client.cpp if needed.
  • Models: Use CRCW for maximum performance, EREW for maximum safety.

Project Structure

Network-File-Editor/
├── LICENSE # MIT License file
├── Makefile # Build configuration and rules
├── README.md # This documentation
├── requirements.txt # List of system dependencies
├── share/ # Default shared directory (auto-created)
│ ├── code.cpp # Sample C++ file
│ ├── info.txt # Sample text file
│ └── script.sh # Sample shell script
├── source/ # Source code directory
│ ├── client/ # Client-side code
│ │ ├── client.cpp # Main client implementation
│ │ ├── client.hpp # Client header
│ │ └── main_client.cpp # Client entry point
│ ├── common/ # Shared utilities
│ │ ├── concurrency.cpp # Concurrency model implementations
│ │ ├── concurrency.hpp # Concurrency headers
│ │ ├── config.hpp # Configuration management
│ │ ├── logger.cpp # Logging system
│ │ └── logger.hpp # Logger headers
│ └── server/ # Server-side code
│ ├── client_handler.cpp # Client connection handling
│ ├── client_handler.hpp # Handler headers
│ ├── file_manager.cpp # File operations
│ ├── file_manager.hpp # File manager headers
│ ├── main_server.cpp # Server entry point
│ ├── server.cpp # Main server logic
│ └── server.hpp # Server headers
└── NFE-Server.log # Server activity log (auto-generated)

Contributing

  1. Fork the repository.
  2. Create a feature branch: git checkout -b feature-name
  3. Make changes and test thoroughly.
  4. Ensure code follows C++17 standards and includes proper error handling.
  5. Update documentation if adding new features.
  6. Submit a pull request with a clear description of changes.

Development Guidelines

  • Use C++17 features consistently.
  • Follow RAII principles for resource management.
  • Add comprehensive logging for new features.
  • Test with all four concurrency models.
  • Handle edge cases (file not found, permission denied, network errors).
  • Update the README for any new commands or features.

License

This project is licensed under the MIT License. See the LICENSE file for complete details.

About

A Network File Editor (NFE) supporting four concurrency models for multi-user file management with external editor integration.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages