File tree 3 files changed +21
-2
lines changed
3 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ SQLiteFS is a Go package that implements the `fs.FS` interface using SQLite as a
14
14
15
15
To use SQLiteFS in your Go project, run the following command:
16
16
17
- ```
17
+ ``` sh
18
18
go get github.com/jilio/sqlitefs
19
19
```
20
20
@@ -51,7 +51,7 @@ func main() {
51
51
defer sqliteFS.Close ()
52
52
53
53
// Write a file
54
- writer := sqlitefs. NewSQLiteWriter (sqliteFS, " example.txt" )
54
+ writer := sqliteFS. NewWriter ( " example.txt" )
55
55
_, err = writer.Write ([]byte (" Hello, SQLiteFS!" ))
56
56
if err != nil {
57
57
log.Fatal (err)
Original file line number Diff line number Diff line change @@ -42,6 +42,11 @@ func NewSQLiteFS(db *sql.DB) (*SQLiteFS, error) {
42
42
return fs , nil
43
43
}
44
44
45
+ // NewWriter creates a new writer for the specified path.
46
+ func (fs * SQLiteFS ) NewWriter (path string ) * SQLiteWriter {
47
+ return NewSQLiteWriter (fs , path )
48
+ }
49
+
45
50
// Open открывает файл по указанному пути.
46
51
func (fs * SQLiteFS ) Open (name string ) (fs.File , error ) {
47
52
// Проверка существования файла в базе данных
Original file line number Diff line number Diff line change 1
1
package sqlitefs
2
2
3
3
import (
4
+ "errors"
4
5
"mime"
5
6
"path/filepath"
6
7
)
@@ -14,8 +15,11 @@ type SQLiteWriter struct {
14
15
fragmentSize int
15
16
fragmentIndex int
16
17
fileCreated bool
18
+ closed bool
17
19
}
18
20
21
+ // NewSQLiteWriter creates a new SQLiteWriter for the specified path.
22
+ // Deprecated: Use SQLiteFS.NewWriter instead.
19
23
func NewSQLiteWriter (fs * SQLiteFS , path string ) * SQLiteWriter {
20
24
return & SQLiteWriter {
21
25
fs : fs ,
@@ -26,6 +30,10 @@ func NewSQLiteWriter(fs *SQLiteFS, path string) *SQLiteWriter {
26
30
}
27
31
28
32
func (w * SQLiteWriter ) Write (p []byte ) (n int , err error ) {
33
+ if w .closed {
34
+ return 0 , errors .New ("sqlitefs: write to closed writer" )
35
+ }
36
+
29
37
n = len (p )
30
38
w .buffer = append (w .buffer , p ... )
31
39
@@ -84,11 +92,17 @@ func (w *SQLiteWriter) createFileRecord() error {
84
92
}
85
93
86
94
func (w * SQLiteWriter ) Close () error {
95
+ if w .closed {
96
+ return nil
97
+ }
98
+
87
99
for len (w .buffer ) > 0 || ! w .fileCreated {
88
100
err := w .writeFragment ()
89
101
if err != nil {
90
102
return err
91
103
}
92
104
}
105
+
106
+ w .closed = true
93
107
return nil
94
108
}
You can’t perform that action at this time.
0 commit comments