|
1 |
| -# rwfs - WIP |
| 1 | +# RWFS - In-Memory Read-Write File System |
2 | 2 |
|
3 |
| -`rwfs` is a Go library for an in-memory file system with optional persistence to disk and compression. |
| 3 | +RWFS is a Go-based in-memory read-write file system that supports various features like directory support, file permissions, memory caching, compression, security, and more. It is designed to provide a simple and efficient way to simulate file system operations in memory. |
4 | 4 |
|
5 | 5 | ## Features
|
6 | 6 |
|
7 |
| - In-memory file system |
8 |
| - Persistent storage to disk |
9 |
| - Optional gzip compression |
10 |
| - |
11 |
| -# **NOTE:** This library is still in the process of being developed and many features are still being added. |
| 7 | +- **Directory Support**: Create, delete, and navigate directories. |
| 8 | +- **File and Directory Search**: Search files and directories by name or patterns. |
| 9 | +- **Permissions and Access Control**: Manage read, write, and execute permissions for files and directories. |
| 10 | +- **Caching**: Memory caching of files for improved read performance. |
| 11 | +- **Compression**: Support for file compression to save space. |
| 12 | +- **File Metadata**: Manage extended metadata for files. |
| 13 | +- **Concurrency Support**: Concurrent access to files and directories with support for read-write locks. |
| 14 | +- **Error Handling**: Custom error handling for file system operations. |
| 15 | +- **Security Features**: Implement security measures like encryption and access control lists. |
| 16 | +- **Local File System Integration**: Integration with the local file system for seamless data storage. |
12 | 17 |
|
13 | 18 | ## Installation
|
14 | 19 |
|
15 |
| -```bash |
| 20 | +To install RWFS, use the following command: |
| 21 | + |
| 22 | +```sh |
16 | 23 | go get github.com/DanielcoderX/rwfs
|
17 | 24 | ```
|
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +### Creating and Initializing the File System |
| 29 | + |
| 30 | +```go |
| 31 | +package main |
| 32 | + |
| 33 | +import ( |
| 34 | + "fmt" |
| 35 | + "github.com/DanielcoderX/rwfs" |
| 36 | +) |
| 37 | + |
| 38 | +func main() { |
| 39 | + // Initialize the in-memory file system |
| 40 | + fs := rwfs.NewMemFileSystem() |
| 41 | + |
| 42 | + // Create a new directory |
| 43 | + err := fs.CreateDir("example_dir") |
| 44 | + if err != nil { |
| 45 | + fmt.Println("Error creating directory:", err) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + // Change to the new directory |
| 50 | + err = fs.ChangeDir("example_dir") |
| 51 | + if err != nil { |
| 52 | + fmt.Println("Error changing directory:", err) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + // Create a new file in the current directory |
| 57 | + _, err = fs.CreateFile("example_file.txt", "owner1", rwfs.FilePermission{Read: true, Write: true}) |
| 58 | + if err != nil { |
| 59 | + fmt.Println("Error creating file:", err) |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + // List the contents of the current directory |
| 64 | + contents := fs.CWD.GetDirectoryContents() |
| 65 | + fmt.Println("Current directory contents:", contents) |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Core Functions |
| 70 | + |
| 71 | +#### CreateDir |
| 72 | + |
| 73 | +Creates a new directory in the current working directory. |
| 74 | + |
| 75 | +```go |
| 76 | +func (fs *MemFileSystem) CreateDir(name string) error |
| 77 | +``` |
| 78 | + |
| 79 | +#### RemoveDir |
| 80 | + |
| 81 | +Removes a directory from the current working directory. |
| 82 | + |
| 83 | +```go |
| 84 | +func (fs *MemFileSystem) RemoveDir(name string) error |
| 85 | +``` |
| 86 | + |
| 87 | +#### ChangeDir |
| 88 | + |
| 89 | +Changes the current working directory. |
| 90 | + |
| 91 | +```go |
| 92 | +func (fs *MemFileSystem) ChangeDir(name string) error |
| 93 | +``` |
| 94 | + |
| 95 | +#### CreateFile |
| 96 | + |
| 97 | +Creates a new file in the current working directory. |
| 98 | + |
| 99 | +```go |
| 100 | +func (fs *MemFileSystem) CreateFile(name, owner string, permissions FilePermission) (File, error) |
| 101 | +``` |
| 102 | + |
| 103 | +#### OpenFile |
| 104 | + |
| 105 | +Opens a file in the current working directory. |
| 106 | + |
| 107 | +```go |
| 108 | +func (fs *MemFileSystem) OpenFile(name string) (File, error) |
| 109 | +``` |
| 110 | + |
| 111 | +#### RemoveFile |
| 112 | + |
| 113 | +Removes a file from the current working directory. |
| 114 | + |
| 115 | +```go |
| 116 | +func (fs *MemFileSystem) RemoveFile(name string) error |
| 117 | +``` |
| 118 | + |
| 119 | +#### ListFiles |
| 120 | + |
| 121 | +Lists all files in the current working directory. |
| 122 | + |
| 123 | +```go |
| 124 | +func (fs *MemFileSystem) ListFiles() ([]string, error) |
| 125 | +``` |
| 126 | + |
| 127 | +#### ListDirContents |
| 128 | + |
| 129 | +Lists the contents of the current working directory (both files and directories). |
| 130 | + |
| 131 | +```go |
| 132 | +func (fs *MemFileSystem) ListDirContents() ([]DirEntry, error) |
| 133 | +``` |
| 134 | + |
| 135 | +### Additional Utilities |
| 136 | + |
| 137 | +#### GetDirectoryContents |
| 138 | + |
| 139 | +Returns the contents of a directory in a structured format. |
| 140 | + |
| 141 | +```go |
| 142 | +// DirectoryContents represents the contents of a directory |
| 143 | +type DirectoryContents struct { |
| 144 | + DirectoryName string |
| 145 | + Files []string |
| 146 | + Subdirectories []string |
| 147 | +} |
| 148 | + |
| 149 | +// GetDirectoryContents returns the contents of the directory in a structured format |
| 150 | +func (dir *MemDirectory) GetDirectoryContents() DirectoryContents |
| 151 | +``` |
| 152 | + |
| 153 | +### Example |
| 154 | + |
| 155 | +Here is an example to demonstrate basic operations like creating directories, changing directories, and listing directory contents: |
| 156 | + |
| 157 | +```go |
| 158 | +package main |
| 159 | + |
| 160 | +import ( |
| 161 | + "fmt" |
| 162 | + "github.com/DanielcoderX/rwfs" |
| 163 | +) |
| 164 | + |
| 165 | +func main() { |
| 166 | + // Define the file system configuration |
| 167 | + config := rwfs.FileSystemConfig{ |
| 168 | + Filepath: "data.rwfs", |
| 169 | + Compression: true, |
| 170 | + CompressLevel: 9, |
| 171 | + Encryption: false, |
| 172 | + } |
| 173 | + // Initialize a new local file system |
| 174 | + fs, err := rwfs.NewLocalFileSystem(config) |
| 175 | + if err != nil { |
| 176 | + fmt.Println("Error initializing file system:", err) |
| 177 | + return |
| 178 | + } |
| 179 | + |
| 180 | +// Create and navigate directories |
| 181 | + err := fs.CreateDir("dir1") |
| 182 | + if err != nil { |
| 183 | + fmt.Println("Error creating directory:", err) |
| 184 | + return |
| 185 | + } |
| 186 | + err = fs.ChangeDir("dir1") |
| 187 | + if err != nil { |
| 188 | + fmt.Println("Error changing directory:", err) |
| 189 | + return |
| 190 | + } |
| 191 | + err = fs.CreateDir("subdir1") |
| 192 | + if err != nil { |
| 193 | + fmt.Println("Error creating subdirectory:", err) |
| 194 | + return |
| 195 | + } |
| 196 | + |
| 197 | + // Create a file |
| 198 | + _, err = fs.CreateFile("file1.txt", "owner1", rwfs.FilePermission{Read: true, Write: true}) |
| 199 | + if err != nil { |
| 200 | + fmt.Println("Error creating file:", err) |
| 201 | + return |
| 202 | + } |
| 203 | + |
| 204 | + // Get contents of current directory |
| 205 | + contents := fs.CWD.GetDirectoryContents() |
| 206 | + fmt.Println("Current directory contents:", contents) |
| 207 | + |
| 208 | + // Remove a directory |
| 209 | + err = fs.ChangeDir("/") |
| 210 | + if err != nil { |
| 211 | + fmt.Println("Error changing directory:", err) |
| 212 | + return |
| 213 | + } |
| 214 | + err = fs.RemoveDir("dir1") |
| 215 | + if err != nil { |
| 216 | + fmt.Println("Error removing directory:", err) |
| 217 | + return |
| 218 | + } |
| 219 | +} |
| 220 | +``` |
| 221 | + |
| 222 | +## License |
| 223 | + |
| 224 | +This project is licensed under the GPL License. See the [LICENSE](LICENSE) file for details. |
| 225 | + |
| 226 | +## Contributing |
| 227 | + |
| 228 | +Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes. |
| 229 | + |
| 230 | +## Contact |
| 231 | + |
| 232 | +Just open an issue :) |
0 commit comments