Skip to content

Commit

Permalink
[Go] close file after write (#67)
Browse files Browse the repository at this point in the history
Signed-off-by: sunby <[email protected]>
  • Loading branch information
sunby committed Sep 28, 2023
1 parent 0b0ff47 commit c58990f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go/io/fs/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (f *Factory) Create(fsType options.FsType, uri *url.URL) (Fs, error) {
case options.InMemory:
return NewMemoryFs(), nil
case options.LocalFS:
return NewLocalFs(), nil
return NewLocalFs(uri), nil
case options.S3:
return NewMinioFs(uri)
default:
Expand Down
11 changes: 7 additions & 4 deletions go/io/fs/local_fs.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package fs

import (
"net/url"
"os"
"path/filepath"

"github.com/milvus-io/milvus-storage/go/common/log"
"github.com/milvus-io/milvus-storage/go/io/fs/file"
)

type LocalFS struct{}
type LocalFS struct {
path string
}

func (l *LocalFS) OpenFile(path string) (file.File, error) {
// Extract the directory from the path
Expand Down Expand Up @@ -66,9 +69,9 @@ func (l *LocalFS) Exist(path string) (bool, error) {
}

func (l *LocalFS) Path() string {
return ""
return l.path
}

func NewLocalFs() *LocalFS {
return &LocalFS{}
func NewLocalFs(uri *url.URL) *LocalFS {
return &LocalFS{uri.Path}
}
4 changes: 3 additions & 1 deletion go/storage/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ func WriteManifestFile(manifest *Manifest, output file.File) error {
if write != len(bytes) {
return fmt.Errorf("failed to write whole file, expect: %v, actual: %v", len(bytes), write)
}

if err = output.Close(); err != nil {
return err
}
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions go/storage/manifest/reader_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ func (rw ManifestReaderWriter) Write(m *Manifest) error {
log.Debug("path", log.String("tmpManifestFilePath", tmpManifestFilePath), log.String("manifestFilePath", manifestFilePath))
output, err := rw.fs.OpenFile(tmpManifestFilePath)
if err != nil {
return fmt.Errorf("save manfiest: %w", err)
return fmt.Errorf("open file error: %w", err)
}
if err = WriteManifestFile(m, output); err != nil {
return err
}
err = rw.fs.Rename(tmpManifestFilePath, manifestFilePath)
if err != nil {
return fmt.Errorf("save manfiest: %w", err)
return fmt.Errorf("rename file error: %w", err)
}
log.Debug("save manifest file success", log.String("path", manifestFilePath))
return nil
Expand Down

0 comments on commit c58990f

Please sign in to comment.