Skip to content

Commit

Permalink
bug: must reset file pointer (#59)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaukas Wang <[email protected]>
  • Loading branch information
gaukas authored Feb 23, 2024
1 parent 941d88b commit 15af376
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion transport/v0/transport_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ func (tm *TransportModule) pushConfig() int32 {
}

configFile, err := tm.Core().Config().TransportModuleConfig.AsFile()
if err == nil {
if err != nil {
log.LErrorf(tm.Core().Logger(), "water: getting config file failed: %v", err)
return wasip1.EncodeWATERError(syscall.EBADF) // Cannot read a provided config file
}
Expand Down
13 changes: 9 additions & 4 deletions transport_module_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func (c transportModuleConfigBytes) AsFile() (*os.File, error) {
if err != nil {
return nil, fmt.Errorf("failed to create temp file for transport module config: %w", err)
}

if _, err := f.Write(c); err != nil {
return nil, fmt.Errorf("failed to write transport module config to temp file: %w", err)
}
// reset the file pointer to the beginning of the file
if _, err := f.Seek(0, 0); err != nil {
return nil, fmt.Errorf("failed to seek to the beginning of the temp file: %w", err)
}

runtime.SetFinalizer(f, func(tmpFile *os.File) {
tmpFile.Close()
// Remove the temp file from local file system when collected.
Expand All @@ -82,9 +91,5 @@ func (c transportModuleConfigBytes) AsFile() (*os.File, error) {
os.Remove(tmpFile.Name())
})

if _, err := f.Write(c); err != nil {
return nil, fmt.Errorf("failed to write transport module config to temp file: %w", err)
}

return f, nil
}

0 comments on commit 15af376

Please sign in to comment.