Skip to content

Commit be46a66

Browse files
committed
new feature, v1
1 parent a2285c8 commit be46a66

14 files changed

+1623
-143
lines changed

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 223 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,232 @@
1-
# rwfs - WIP
1+
# RWFS - In-Memory Read-Write File System
22

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.
44

55
## Features
66

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.
1217

1318
## Installation
1419

15-
```bash
20+
To install RWFS, use the following command:
21+
22+
```sh
1623
go get github.com/DanielcoderX/rwfs
1724
```
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 :)

example/file_cache_example.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/DanielcoderX/rwfs/pkg/rwfs"
8+
)
9+
10+
func main() {
11+
// Create a new file cache
12+
cache := rwfs.NewFileCache()
13+
14+
// Create MemFile instances
15+
memFile1 := rwfs.NewMemFile("key1", "", rwfs.FilePermission{})
16+
memFile2 := rwfs.NewMemFile("key2", "", rwfs.FilePermission{})
17+
18+
// Put MemFile instances into the cache
19+
cache.Put("key1", memFile1, false) // Not dirty
20+
cache.Put("key2", memFile2, false) // Not dirty
21+
22+
// Retrieve data from the cache
23+
data1, exists1 := cache.Get("key1")
24+
data2, exists2 := cache.Get("key2")
25+
26+
if exists1 {
27+
fmt.Printf("Data for key1: %s\n", data1)
28+
} else {
29+
fmt.Println("Data for key1 not found in cache")
30+
}
31+
32+
if exists2 {
33+
fmt.Printf("Data for key2: %s\n", data2.Data.Bytes())
34+
} else {
35+
fmt.Println("Data for key2 not found in cache")
36+
}
37+
38+
// Wait for some time to demonstrate cache expiration
39+
time.Sleep(time.Second * 3)
40+
41+
// Check if the cached data expired
42+
data1, exists1 = cache.Get("key1")
43+
if exists1 {
44+
fmt.Printf("Data for key1: %s\n", data1.Data.Bytes())
45+
} else {
46+
fmt.Println("Data for key1 expired from cache")
47+
}
48+
49+
// Remove data from the cache
50+
cache.Remove("key2")
51+
52+
// Try to retrieve data that has been removed
53+
data2, exists2 = cache.Get("key2")
54+
if exists2 {
55+
fmt.Printf("Data for key2: %s\n", data2.Data.Bytes())
56+
} else {
57+
fmt.Println("Data for key2 not found in cache (removed)")
58+
}
59+
}

0 commit comments

Comments
 (0)