Skip to content

Commit ae13b12

Browse files
committed
rofl/build: Remove FixPermissions and fix concat
1 parent 1323206 commit ae13b12

File tree

3 files changed

+21
-50
lines changed

3 files changed

+21
-50
lines changed

build/env/env.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"path/filepath"
88
"strings"
99
"sync"
10-
11-
"github.com/oasisprotocol/cli/cmd/common"
1210
)
1311

1412
// ExecEnv is an execution environment.
@@ -24,10 +22,6 @@ type ExecEnv interface {
2422
// environment.
2523
PathToEnv(path string) (string, error)
2624

27-
// FixPermissions ensures that the user executing this process owns the file at the given path
28-
// outside the environment.
29-
FixPermissions(path string) error
30-
3125
// HasBinary returns true iff the given binary name is available in this environment.
3226
HasBinary(name string) bool
3327

@@ -58,11 +52,6 @@ func (ne *NativeEnv) PathToEnv(path string) (string, error) {
5852
return path, nil
5953
}
6054

61-
// FixPermissions implements ExecEnv.
62-
func (ne *NativeEnv) FixPermissions(string) error {
63-
return nil
64-
}
65-
6655
// HasBinary implements ExecEnv.
6756
func (ne *NativeEnv) HasBinary(name string) bool {
6857
path, err := exec.LookPath(name)
@@ -194,23 +183,6 @@ func (de *ContainerEnv) PathToEnv(path string) (string, error) {
194183
return "", fmt.Errorf("bad path '%s'", path)
195184
}
196185

197-
// FixPermissions implements ExecEnv.
198-
func (de *ContainerEnv) FixPermissions(path string) error {
199-
path, err := de.PathToEnv(path)
200-
if err != nil {
201-
return err
202-
}
203-
204-
cmd := exec.Command("chown", fmt.Sprintf("%d:%d", os.Getuid(), os.Getgid()), path) //nolint: gosec
205-
if err = de.WrapCommand(cmd); err != nil {
206-
return err
207-
}
208-
if common.IsVerbose() {
209-
fmt.Println(cmd)
210-
}
211-
return cmd.Run()
212-
}
213-
214186
// HasBinary implements ExecEnv.
215187
func (de *ContainerEnv) HasBinary(string) bool {
216188
return true

cmd/rofl/build/artifacts.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -459,39 +459,38 @@ func createVerityHashTree(buildEnv env.ExecEnv, fsFn, hashFn string) (string, er
459459
return "", fmt.Errorf("%w\n%s", err, out.String())
460460
}
461461

462-
if err = buildEnv.FixPermissions(fsFn); err != nil {
463-
return "", err
464-
}
465-
if err = buildEnv.FixPermissions(hashFn); err != nil {
466-
return "", err
467-
}
468-
if err = buildEnv.FixPermissions(rootHashFn); err != nil {
469-
return "", err
470-
}
471-
472462
data, err := os.ReadFile(rootHashFn)
473463
if err != nil {
474464
return "", fmt.Errorf("failed to read dm-verity root hash: %w", err)
475465
}
476466
return string(data), nil
477467
}
478468

479-
// concatFiles appends the contents of file b to a.
480-
func concatFiles(a, b string) error {
481-
df, err := os.OpenFile(a, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
469+
// concatFiles appends the contents of file b to a using the given build environment.
470+
// This ensures the operation works correctly with containerized builds where the host
471+
// may not have write permissions to container-created files.
472+
func concatFiles(buildEnv env.ExecEnv, a, b string) error {
473+
aEnv, err := buildEnv.PathToEnv(a)
482474
if err != nil {
483-
return err
475+
return fmt.Errorf("failed to translate path: %w", err)
484476
}
485-
defer df.Close()
486-
487-
sf, err := os.Open(b)
477+
bEnv, err := buildEnv.PathToEnv(b)
488478
if err != nil {
489-
return err
479+
return fmt.Errorf("failed to translate path: %w", err)
490480
}
491-
defer sf.Close()
492481

493-
_, err = io.Copy(df, sf)
494-
return err
482+
// Use shell to append file b to file a.
483+
cmd := exec.Command("sh", "-c", fmt.Sprintf("cat %q >> %q", bEnv, aEnv)) //nolint:gosec
484+
var out strings.Builder
485+
cmd.Stderr = &out
486+
cmd.Stdout = &out
487+
if err = buildEnv.WrapCommand(cmd); err != nil {
488+
return err
489+
}
490+
if err = cmd.Run(); err != nil {
491+
return fmt.Errorf("%w\n%s", err, out.String())
492+
}
493+
return nil
495494
}
496495

497496
// padWithEmptySpace pads the given file with empty space to make it the given size. See

cmd/rofl/build/tdx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func tdxPrepareStage2(
166166
}
167167

168168
// Concatenate filesystem and hash tree into one image.
169-
if err = concatFiles(rootfsImage, hashFile); err != nil {
169+
if err = concatFiles(buildEnv, rootfsImage, hashFile); err != nil {
170170
return nil, fmt.Errorf("failed to concatenate rootfs and hash tree files: %w", err)
171171
}
172172

0 commit comments

Comments
 (0)