Skip to content

Commit b9c138a

Browse files
committed
filesystem: add Seeker extension function
1 parent 15f262e commit b9c138a

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

internal/filesystem/filesystem.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,8 @@ func FSID(fsys fs.FS) (ID, error) {
124124
if fsys, ok := fsys.(IDFS); ok {
125125
return fsys.ID(), nil
126126
}
127-
return "", fmt.Errorf(
128-
"id %T: %w",
129-
fsys, errors.ErrUnsupported,
130-
)
127+
const op = "id"
128+
return "", unsupportedOpErrAnonymous(op, fsys)
131129
}
132130

133131
func OpenFile(fsys fs.FS, name string, flag int, perm fs.FileMode) (fs.File, error) {
@@ -261,6 +259,14 @@ func StreamDir(ctx context.Context, count int, directory fs.ReadDirFile) <-chan
261259
return stream
262260
}
263261

262+
func Seek(file fs.File, offset int64, whence int) (int64, error) {
263+
if seeker, ok := file.(io.Seeker); ok {
264+
return seeker.Seek(offset, whence)
265+
}
266+
const op = "seek"
267+
return -1, unsupportedOpErrAnonymous(op, file)
268+
}
269+
264270
func unsupportedOpErr(op, name string) error {
265271
return fmt.Errorf(
266272
op+` "%s": %w`,
@@ -274,3 +280,10 @@ func unsupportedOpErr2(op, name1, name2 string) error {
274280
name1, name2, errors.ErrUnsupported,
275281
)
276282
}
283+
284+
func unsupportedOpErrAnonymous(op string, subject any) error {
285+
return fmt.Errorf(
286+
op+` %T: %w`,
287+
subject, errors.ErrUnsupported,
288+
)
289+
}

0 commit comments

Comments
 (0)