Skip to content

Commit 15af376

Browse files
authored
bug: must reset file pointer (#59)
Signed-off-by: Gaukas Wang <i@gaukas.wang>
1 parent 941d88b commit 15af376

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

transport/v0/transport_module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ func (tm *TransportModule) pushConfig() int32 {
692692
}
693693

694694
configFile, err := tm.Core().Config().TransportModuleConfig.AsFile()
695-
if err == nil {
695+
if err != nil {
696696
log.LErrorf(tm.Core().Logger(), "water: getting config file failed: %v", err)
697697
return wasip1.EncodeWATERError(syscall.EBADF) // Cannot read a provided config file
698698
}

transport_module_config.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ func (c transportModuleConfigBytes) AsFile() (*os.File, error) {
7272
if err != nil {
7373
return nil, fmt.Errorf("failed to create temp file for transport module config: %w", err)
7474
}
75+
76+
if _, err := f.Write(c); err != nil {
77+
return nil, fmt.Errorf("failed to write transport module config to temp file: %w", err)
78+
}
79+
// reset the file pointer to the beginning of the file
80+
if _, err := f.Seek(0, 0); err != nil {
81+
return nil, fmt.Errorf("failed to seek to the beginning of the temp file: %w", err)
82+
}
83+
7584
runtime.SetFinalizer(f, func(tmpFile *os.File) {
7685
tmpFile.Close()
7786
// Remove the temp file from local file system when collected.
@@ -82,9 +91,5 @@ func (c transportModuleConfigBytes) AsFile() (*os.File, error) {
8291
os.Remove(tmpFile.Name())
8392
})
8493

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

0 commit comments

Comments
 (0)