Skip to content

Commit ad661b1

Browse files
committed
feat(copy_out_truncate): add support to copy out truncate
1 parent b613b1f commit ad661b1

File tree

7 files changed

+27
-12
lines changed

7 files changed

+27
-12
lines changed

cmd/go-judge/grpc_executor/grpc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ func convertPBCmd(c *pb.Request_CmdType, srcPrefix []string) (cm worker.Cmd, err
211211
CopyOutCached: convertCopyOut(c.GetCopyOutCached()),
212212
CopyOutMax: c.GetCopyOutMax(),
213213
CopyOutDir: c.GetCopyOutDir(),
214+
CopyOutTruncate: c.GetCopyOutTruncate(),
214215
Symlinks: c.GetSymlinks(),
215216
}
216217
for _, f := range c.GetFiles() {

cmd/go-judge/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ func generateHandleVersion(_ *config.Config, _ map[string]any) func(*gin.Context
571571
"addressSpaceLimit": true,
572572
"stream": true,
573573
"procPeak": true,
574+
"copyOutTruncate": true,
574575
})
575576
}
576577
}
@@ -584,6 +585,7 @@ func generateHandleConfig(conf *config.Config, builderParam map[string]any) func
584585
"addressSpaceLimit": true,
585586
"stream": true,
586587
"procPeak": true,
588+
"copyOutTruncate": true,
587589
"fileStorePath": conf.Dir,
588590
"runnerConfig": builderParam,
589591
})

cmd/go-judge/model/model.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ type Cmd struct {
4848

4949
CopyIn map[string]CmdFile `json:"copyIn"`
5050

51-
CopyOut []string `json:"copyOut"`
52-
CopyOutCached []string `json:"copyOutCached"`
53-
CopyOutMax uint64 `json:"copyOutMax"`
54-
CopyOutDir string `json:"copyOutDir"`
51+
CopyOut []string `json:"copyOut"`
52+
CopyOutCached []string `json:"copyOutCached"`
53+
CopyOutMax uint64 `json:"copyOutMax"`
54+
CopyOutDir string `json:"copyOutDir"`
55+
CopyOutTruncate bool `json:"copyOutTruncate"`
5556

5657
TTY bool `json:"tty,omitempty"`
5758
StrictMemoryLimit bool `json:"strictMemoryLimit"`
@@ -317,6 +318,7 @@ func convertCmd(c Cmd, srcPrefix []string) (worker.Cmd, error) {
317318
CopyOutCached: convertCopyOut(c.CopyOutCached),
318319
CopyOutMax: c.CopyOutMax,
319320
CopyOutDir: c.CopyOutDir,
321+
CopyOutTruncate: c.CopyOutTruncate,
320322
}
321323
for _, f := range c.Files {
322324
cf, err := convertCmdFile(f, srcPrefix)

envexec/cmd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ type Cmd struct {
5050
Waiter func(context.Context, Process) bool
5151

5252
// file names to copyout after exec
53-
CopyOut []CmdCopyOutFile
54-
CopyOutMax Size // file size limit
53+
CopyOut []CmdCopyOutFile
54+
CopyOutMax Size // file size limit
55+
CopyOutTruncate bool
5556

5657
// CopyOutDir specifies a dir to dump all /w content
5758
CopyOutDir string

envexec/file_collect.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ func copyOutFile(
9898
s := stat.Size()
9999
if c.CopyOutMax > 0 && s > int64(c.CopyOutMax) {
100100
t = ErrCopyOutSizeExceeded
101-
return fmt.Errorf("copyout: %q size (%d) exceeds limit (%d)", n.Name, s, c.CopyOutMax)
101+
if !c.CopyOutTruncate {
102+
return fmt.Errorf("copyout: %q size (%d) exceeds limit (%d)", n.Name, s, c.CopyOutMax)
103+
}
104+
s = int64(c.CopyOutMax) + 1
102105
}
103106
// create store file
104107
buf, err := newStoreFile()
@@ -108,13 +111,17 @@ func copyOutFile(
108111
}
109112

110113
// Ensure not copy over file size
111-
_, err = buf.ReadFrom(io.LimitReader(cf, s))
114+
s, err = buf.ReadFrom(io.LimitReader(cf, s))
112115
if err != nil {
113116
t = ErrCopyOutCopyContent
114117
buf.Close()
115118
return fmt.Errorf("copyout: failed to copy content for %q: %w", n.Name, err)
116119
}
117120
put(buf, n.Name)
121+
if s > int64(c.CopyOutMax) {
122+
t = ErrCopyOutSizeExceeded
123+
return fmt.Errorf("copyout: %q size (%d) exceeds limit (%d)", n.Name, s, c.CopyOutMax)
124+
}
118125
return nil
119126
}
120127

worker/model.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ type Cmd struct {
3434
CopyIn map[string]CmdFile
3535
Symlinks map[string]string
3636

37-
CopyOut []CmdCopyOutFile
38-
CopyOutCached []CmdCopyOutFile
39-
CopyOutMax uint64
40-
CopyOutDir string
37+
CopyOut []CmdCopyOutFile
38+
CopyOutCached []CmdCopyOutFile
39+
CopyOutMax uint64
40+
CopyOutDir string
41+
CopyOutTruncate bool
4142

4243
TTY bool
4344
DataSegmentLimit bool

worker/worker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ func (w *worker) prepareCmd(rc Cmd, pipeFileName map[string]bool) (*envexec.Cmd,
391391
CopyOut: copyOut,
392392
CopyOutDir: copyOutDir,
393393
CopyOutMax: copyOutMax,
394+
CopyOutTruncate: rc.CopyOutTruncate,
394395
Waiter: wait.Wait,
395396
}, nil
396397
}

0 commit comments

Comments
 (0)