Skip to content

Commit fbf96d3

Browse files
committed
Fix path in LogFile creator
Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent 5e0d793 commit fbf96d3

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

cio/io.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,11 @@ func BinaryIO(binary string, args map[string]string) Creator {
258258
q.Set(k, v)
259259
}
260260
uri.RawQuery = q.Encode()
261+
res := uri.String()
261262
return &logURI{
262263
config: Config{
263-
Stdout: uri.String(),
264-
Stderr: uri.String(),
264+
Stdout: res,
265+
Stderr: res,
265266
},
266267
}, nil
267268
}
@@ -271,14 +272,19 @@ func BinaryIO(binary string, args map[string]string) Creator {
271272
// If the log file already exists, the logs will be appended to the file.
272273
func LogFile(path string) Creator {
273274
return func(_ string) (IO, error) {
275+
path = filepath.Clean(path)
276+
if !strings.HasPrefix(path, "/") {
277+
return nil, errors.New("absolute path needed")
278+
}
274279
uri := &url.URL{
275280
Scheme: "file",
276-
Host: path,
281+
Path: path,
277282
}
283+
res := uri.String()
278284
return &logURI{
279285
config: Config{
280-
Stdout: uri.String(),
281-
Stderr: uri.String(),
286+
Stdout: res,
287+
Stderr: res,
282288
},
283289
}, nil
284290
}

cio/io_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,21 @@ func TestBinaryIOFailOnRelativePath(t *testing.T) {
177177
_, err := BinaryIO("./bin", nil)("!")
178178
assert.Error(t, err, "absolute path needed")
179179
}
180+
181+
func TestLogFileAbsolutePath(t *testing.T) {
182+
res, err := LogFile("/full/path/file.txt")("!")
183+
assert.NilError(t, err)
184+
assert.Equal(t, "file:///full/path/file.txt", res.Config().Stdout)
185+
assert.Equal(t, "file:///full/path/file.txt", res.Config().Stderr)
186+
187+
// Test parse back
188+
parsed, err := url.Parse(res.Config().Stdout)
189+
assert.NilError(t, err)
190+
assert.Equal(t, "file", parsed.Scheme)
191+
assert.Equal(t, "/full/path/file.txt", parsed.Path)
192+
}
193+
194+
func TestLogFileFailOnRelativePath(t *testing.T) {
195+
_, err := LogFile("./file.txt")("!")
196+
assert.Error(t, err, "absolute path needed")
197+
}

runtime/v1/linux/proc/io.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ func createIO(ctx context.Context, id string, ioUID, ioGID int, stdio proc.Stdio
103103
case "binary":
104104
pio.io, err = NewBinaryIO(ctx, id, u)
105105
case "file":
106-
if err := os.MkdirAll(filepath.Dir(u.Host), 0755); err != nil {
106+
if err := os.MkdirAll(filepath.Dir(u.Path), 0755); err != nil {
107107
return nil, err
108108
}
109109
var f *os.File
110-
f, err = os.OpenFile(u.Host, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
110+
f, err = os.OpenFile(u.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
111111
if err != nil {
112112
return nil, err
113113
}

0 commit comments

Comments
 (0)