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)
| 34 | // mode is empty, then no file will be created. If mode is not a regular file |
| 35 | // mode, then contents is ignored. |
| 36 | func 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. |
| 75 | func TestGetExecUserHome(t *testing.T) { |
no test coverage detected
searching dependent graphs…