@@ -24,6 +24,7 @@ import (
24
24
"io"
25
25
"os"
26
26
"path"
27
+ "strconv"
27
28
"time"
28
29
29
30
"github.com/pkg/errors"
@@ -37,8 +38,11 @@ const MemorySource = "memory"
37
38
// CopyableFile is something that can be copied
38
39
type CopyableFile interface {
39
40
io.Reader
41
+ io.Writer
40
42
GetLength () int
43
+ SetLength (int )
41
44
GetSourcePath () string
45
+ GetTargetPath () string
42
46
43
47
GetTargetDir () string
44
48
GetTargetName () string
@@ -62,6 +66,11 @@ func (b *BaseAsset) GetSourcePath() string {
62
66
return b .SourcePath
63
67
}
64
68
69
+ // GetTargetPath returns target path
70
+ func (b * BaseAsset ) GetTargetPath () string {
71
+ return path .Join (b .GetTargetDir (), b .GetTargetName ())
72
+ }
73
+
65
74
// GetTargetDir returns target dir
66
75
func (b * BaseAsset ) GetTargetDir () string {
67
76
return b .TargetDir
@@ -86,6 +95,7 @@ func (b *BaseAsset) GetModTime() (time.Time, error) {
86
95
type FileAsset struct {
87
96
BaseAsset
88
97
reader io.ReadSeeker
98
+ writer io.Writer
89
99
file * os.File // Optional pointer to close file through FileAsset.Close()
90
100
}
91
101
@@ -134,6 +144,14 @@ func (f *FileAsset) GetLength() (flen int) {
134
144
return int (fi .Size ())
135
145
}
136
146
147
+ // SetLength sets the file length
148
+ func (f * FileAsset ) SetLength (flen int ) {
149
+ err := os .Truncate (f .SourcePath , int64 (flen ))
150
+ if err != nil {
151
+ klog .Errorf ("truncate(%q) failed: %v" , f .SourcePath , err )
152
+ }
153
+ }
154
+
137
155
// GetModTime returns modification time of the file
138
156
func (f * FileAsset ) GetModTime () (time.Time , error ) {
139
157
fi , err := os .Stat (f .SourcePath )
@@ -152,6 +170,23 @@ func (f *FileAsset) Read(p []byte) (int, error) {
152
170
return f .reader .Read (p )
153
171
}
154
172
173
+ // Write writes the asset
174
+ func (f * FileAsset ) Write (p []byte ) (int , error ) {
175
+ if f .writer == nil {
176
+ f .file .Close ()
177
+ perms , err := strconv .ParseUint (f .Permissions , 8 , 32 )
178
+ if err != nil || perms > 07777 {
179
+ return 0 , err
180
+ }
181
+ f .file , err = os .OpenFile (f .SourcePath , os .O_RDWR | os .O_CREATE , os .FileMode (perms ))
182
+ if err != nil {
183
+ return 0 , err
184
+ }
185
+ f .writer = io .Writer (f .file )
186
+ }
187
+ return f .writer .Write (p )
188
+ }
189
+
155
190
// Seek resets the reader to offset
156
191
func (f * FileAsset ) Seek (offset int64 , whence int ) (int64 , error ) {
157
192
return f .reader .Seek (offset , whence )
@@ -177,11 +212,23 @@ func (m *MemoryAsset) GetLength() int {
177
212
return m .length
178
213
}
179
214
215
+ // SetLength returns length
216
+ func (m * MemoryAsset ) SetLength (len int ) {
217
+ m .length = len
218
+ }
219
+
180
220
// Read reads the asset
181
221
func (m * MemoryAsset ) Read (p []byte ) (int , error ) {
182
222
return m .reader .Read (p )
183
223
}
184
224
225
+ // Writer writes the asset
226
+ func (m * MemoryAsset ) Write (p []byte ) (int , error ) {
227
+ m .length = len (p )
228
+ m .reader = bytes .NewReader (p )
229
+ return len (p ), nil
230
+ }
231
+
185
232
// Seek resets the reader to offset
186
233
func (m * MemoryAsset ) Seek (offset int64 , whence int ) (int64 , error ) {
187
234
return m .reader .Seek (offset , whence )
@@ -298,6 +345,11 @@ func (m *BinAsset) GetLength() int {
298
345
return m .length
299
346
}
300
347
348
+ // SetLength sets length
349
+ func (m * BinAsset ) SetLength (len int ) {
350
+ m .length = len
351
+ }
352
+
301
353
// Read reads the asset
302
354
func (m * BinAsset ) Read (p []byte ) (int , error ) {
303
355
if m .GetLength () == 0 {
@@ -306,6 +358,13 @@ func (m *BinAsset) Read(p []byte) (int, error) {
306
358
return m .reader .Read (p )
307
359
}
308
360
361
+ // Write writes the asset
362
+ func (m * BinAsset ) Write (p []byte ) (int , error ) {
363
+ m .length = len (p )
364
+ m .reader = bytes .NewReader (p )
365
+ return len (p ), nil
366
+ }
367
+
309
368
// Seek resets the reader to offset
310
369
func (m * BinAsset ) Seek (offset int64 , whence int ) (int64 , error ) {
311
370
return m .reader .Seek (offset , whence )
0 commit comments