MCPcopy
hub / github.com/google/gvisor / createEtcFile

Function createEtcFile

pkg/sentry/fsimpl/user/user_test.go:36–72  ·  view source on GitHub ↗

createEtcFile creates /etc/ with the given contents and mode. If mode is empty, then no file will be created. If mode is not a regular file mode, then contents is ignored.

(ctx context.Context, vfsObj *vfs.VirtualFilesystem, creds *auth.Credentials, root vfs.VirtualDentry, contents string, mode linux.FileMode, filePath string)

Source from the content-addressed store, hash-verified

34// mode is empty, then no file will be created. If mode is not a regular file
35// mode, then contents is ignored.
36func createEtcFile(ctx context.Context, vfsObj *vfs.VirtualFilesystem, creds *auth.Credentials, root vfs.VirtualDentry, contents string, mode linux.FileMode, filePath string) error {
37 pop := vfs.PathOperation{
38 Root: root,
39 Start: root,
40 Path: fspath.Parse("etc"),
41 }
42 if err := vfsObj.MkdirAt(ctx, creds, &pop, &vfs.MkdirOptions{
43 Mode: 0755,
44 }); err != nil && err != linuxerr.EEXIST { // Ignore error if it already exists.
45 return fmt.Errorf("failed to create directory etc: %v", err)
46 }
47
48 pop = vfs.PathOperation{
49 Root: root,
50 Start: root,
51 Path: fspath.Parse(filePath),
52 }
53 switch mode.FileType() {
54 case 0:
55 // Don't create anything.
56 return nil
57 case linux.S_IFREG:
58 fd, err := vfsObj.OpenAt(ctx, creds, &pop, &vfs.OpenOptions{Flags: linux.O_CREAT | linux.O_WRONLY, Mode: mode})
59 if err != nil {
60 return err
61 }
62 defer fd.DecRef(ctx)
63 _, err = fd.Write(ctx, usermem.BytesIOSequence([]byte(contents)), vfs.WriteOptions{})
64 return err
65 case linux.S_IFDIR:
66 return vfsObj.MkdirAt(ctx, creds, &pop, &vfs.MkdirOptions{Mode: mode})
67 case linux.S_IFIFO:
68 return vfsObj.MknodAt(ctx, creds, &pop, &vfs.MknodOptions{Mode: mode})
69 default:
70 return fmt.Errorf("unknown file type %x", mode.FileType())
71 }
72}
73
74// TestGetExecUserHome tests the getExecUserHome function.
75func TestGetExecUserHome(t *testing.T) {

Callers 2

TestGetExecUserHomeFunction · 0.85

Calls 9

ParseFunction · 0.92
BytesIOSequenceFunction · 0.92
MkdirAtMethod · 0.65
ErrorfMethod · 0.65
OpenAtMethod · 0.65
DecRefMethod · 0.65
WriteMethod · 0.65
MknodAtMethod · 0.65
FileTypeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…