How to use CopyFiles method of osutil Package

Best Syzkaller code snippet using osutil.CopyFiles

run.go

Source:run.go Github

copy

Full Screen

1package commands2import (3 "fmt"4 "os"5 fp "path/filepath"6 "strings"7 "golang.org/x/sys/unix"8 "github.com/pkg/errors"9 "github.com/urfave/cli"10 "github.com/yuuki/droot/environ"11 "github.com/yuuki/droot/log"12 "github.com/yuuki/droot/mounter"13 "github.com/yuuki/droot/osutil"14)15var CommandArgRun = "--root ROOT_DIR [--user USER] [--group GROUP] [--bind SRC-PATH[:DEST-PATH]] [--robind SRC-PATH[:DEST-PATH]] [--no-dropcaps] -- COMMAND"16var CommandRun = cli.Command{17 Name: "run",18 Usage: "Run command in container",19 Action: fatalOnError(doRun),20 Flags: []cli.Flag{21 cli.StringFlag{Name: "root, r", Usage: "Root directory path for chrooting"},22 cli.StringFlag{Name: "user, u", Usage: "User (ID or name) to switch before running the program"},23 cli.StringFlag{Name: "group, g", Usage: "Group (ID or name) to switch to"},24 cli.StringSliceFlag{25 Name: "bind, b",26 Value: &cli.StringSlice{},27 Usage: "Bind mount directory (can be specifies multiple times)",28 },29 cli.StringSliceFlag{30 Name: "robind",31 Value: &cli.StringSlice{},32 Usage: "Readonly bind mount directory (can be specifies multiple times)",33 },34 cli.BoolFlag{35 Name: "copy-files, cp",36 Usage: "Copy host files to container such as /etc/group, /etc/passwd, /etc/resolv.conf, /etc/hosts",37 },38 cli.BoolFlag{Name: "no-dropcaps", Usage: "Provide COMMAND's process in chroot with root permission (dangerous)"},39 cli.StringSliceFlag{40 Name: "env, e",41 Value: &cli.StringSlice{},42 Usage: "Set environment variables",43 },44 },45}46var copyFiles = []string{47 "etc/group",48 "etc/passwd",49 "etc/resolv.conf",50 "etc/hosts",51}52var keepCaps = map[uint]bool{53 0: true, // CAP_CHOWN54 1: true, // CAP_DAC_OVERRIDE55 2: true, // CAP_DAC_READ_SEARCH56 3: true, // CAP_FOWNER57 6: true, // CAP_SETGID58 7: true, // CAP_SETUID59 10: true, // CAP_NET_BIND_SERVICE60}61func doRun(c *cli.Context) error {62 command := c.Args()63 if len(command) < 1 {64 cli.ShowCommandHelp(c, "run")65 return errors.New("command required")66 }67 optRootDir := c.String("root")68 if optRootDir == "" {69 cli.ShowCommandHelp(c, "run")70 return errors.New("--root option required")71 }72 rootDir, err := mounter.ResolveRootDir(optRootDir)73 if err != nil {74 return err75 }76 // Check env format KEY=VALUE77 env := c.StringSlice("env")78 if len(env) > 0 {79 for _, e := range env {80 if len(strings.SplitN(e, "=", 2)) != 2 {81 return errors.Errorf("Invalid env format: %s", e)82 }83 }84 }85 uid, gid := os.Getuid(), os.Getgid()86 if group := c.String("group"); group != "" {87 if gid, err = osutil.LookupGroup(group); err != nil {88 return err89 }90 }91 if user := c.String("user"); user != "" {92 if uid, err = osutil.LookupUser(user); err != nil {93 return err94 }95 }96 // copy files97 if c.Bool("copy-files") {98 for _, f := range copyFiles {99 srcFile, destFile := fp.Join("/", f), fp.Join(rootDir, f)100 if err := osutil.Cp(srcFile, destFile); err != nil {101 return errors.Wrapf(err, "Failed to copy %s", f)102 }103 if err := os.Lchown(destFile, uid, gid); err != nil {104 return errors.Wrapf(err, "Failed to lchown %s", f)105 }106 }107 }108 mnt := mounter.NewMounter(rootDir)109 if err := mnt.MountSysProc(); err != nil {110 return err111 }112 for _, val := range c.StringSlice("bind") {113 hostDir, containerDir, err := parseBindOption(val)114 if err != nil {115 return err116 }117 if err := mnt.BindMount(hostDir, containerDir); err != nil {118 return err119 }120 }121 for _, val := range c.StringSlice("robind") {122 hostDir, containerDir, err := parseBindOption(val)123 if err != nil {124 return err125 }126 if err := mnt.RoBindMount(hostDir, containerDir); err != nil {127 return errors.Wrapf(err, "Failed to robind mount %s", val)128 }129 }130 // create symlinks131 if err := osutil.Symlink("../run/lock", fp.Join(rootDir, "/var/lock")); err != nil {132 return err133 }134 if err := createDevices(rootDir, uid, gid); err != nil {135 return err136 }137 if err := osutil.Chroot(rootDir); err != nil {138 return fmt.Errorf("Failed to chroot: %s", err)139 }140 if !c.Bool("no-dropcaps") {141 if err := osutil.DropCapabilities(keepCaps); err != nil {142 return fmt.Errorf("Failed to drop capabilities: %s", err)143 }144 }145 if err := osutil.Setgid(gid); err != nil {146 return fmt.Errorf("Failed to set group %d: %s", gid, err)147 }148 if err := osutil.Setuid(uid); err != nil {149 return fmt.Errorf("Failed to set user %d: %s", uid, err)150 }151 if osutil.ExistsFile(environ.DROOT_ENV_FILE_PATH) {152 envFromFile, err := environ.GetEnvironFromEnvFile(environ.DROOT_ENV_FILE_PATH)153 if err != nil {154 return fmt.Errorf("Failed to read environ from '%s'", environ.DROOT_ENV_FILE_PATH)155 }156 env, err = environ.MergeEnviron(envFromFile, env)157 if err != nil {158 return fmt.Errorf("Failed to merge environ: %s", err)159 }160 }161 return osutil.Execv(command[0], command[0:], env)162}163func parseBindOption(bindOption string) (string, string, error) {164 var hostDir, containerDir string165 d := strings.SplitN(bindOption, ":", 2)166 if len(d) < 2 {167 hostDir = d[0]168 } else {169 hostDir, containerDir = d[0], d[1]170 }171 if containerDir == "" {172 containerDir = hostDir173 }174 if !fp.IsAbs(hostDir) {175 return hostDir, containerDir, fmt.Errorf("%s is not an absolute path", hostDir)176 }177 if !fp.IsAbs(containerDir) {178 return hostDir, containerDir, fmt.Errorf("%s is not an absolute path", containerDir)179 }180 return fp.Clean(hostDir), fp.Clean(containerDir), nil181}182func createDevices(rootDir string, uid, gid int) error {183 nullDir := fp.Join(rootDir, os.DevNull)184 if err := osutil.Mknod(nullDir, unix.S_IFCHR|uint32(os.FileMode(0666)), 1*256+3); err != nil {185 return err186 }187 if err := os.Lchown(nullDir, uid, gid); err != nil {188 log.Debugf("Failed to lchown %s: %s\n", nullDir, err)189 return err190 }191 zeroDir := fp.Join(rootDir, "/dev/zero")192 if err := osutil.Mknod(zeroDir, unix.S_IFCHR|uint32(os.FileMode(0666)), 1*256+3); err != nil {193 return err194 }195 if err := os.Lchown(zeroDir, uid, gid); err != nil {196 log.Debugf("Failed to lchown %s: %s\n", zeroDir, err)197 return err198 }199 for _, f := range []string{"/dev/random", "/dev/urandom"} {200 randomDir := fp.Join(rootDir, f)201 if err := osutil.Mknod(randomDir, unix.S_IFCHR|uint32(os.FileMode(0666)), 1*256+9); err != nil {202 return err203 }204 if err := os.Lchown(randomDir, uid, gid); err != nil {205 log.Debugf("Failed to lchown %s: %s\n", randomDir, err)206 return err207 }208 }209 return nil210}...

Full Screen

Full Screen

osutil_test.go

Source:osutil_test.go Github

copy

Full Screen

...16 if f := os.Args[0] + "-foo-bar-buz"; IsExist(f) {17 t.Fatalf("file %v exists", f)18 }19}20func TestCopyFiles(t *testing.T) {21 type Test struct {22 files []string23 patterns map[string]bool24 err string25 }26 tests := []Test{27 {28 files: []string{29 "foo",30 "bar",31 "baz/foo",32 "baz/bar",33 },34 patterns: map[string]bool{35 "foo": true,36 "bar": false,37 "qux": false,38 "baz/foo": true,39 "baz/bar": false,40 },41 },42 {43 files: []string{44 "foo",45 },46 patterns: map[string]bool{47 "bar": true,48 },49 err: "file bar does not exist",50 },51 {52 files: []string{53 "baz/foo",54 "baz/bar",55 },56 patterns: map[string]bool{57 "baz/*": true,58 },59 },60 {61 files: []string{62 "qux/foo/foo",63 "qux/foo/bar",64 "qux/bar/foo",65 "qux/bar/bar",66 },67 patterns: map[string]bool{68 "qux/*/*": false,69 },70 },71 }72 for _, link := range []bool{false, true} {73 fn, fnName := CopyFiles, "CopyFiles"74 if link {75 fn, fnName = LinkFiles, "LinkFiles"76 }77 t.Run(fnName, func(t *testing.T) {78 for i, test := range tests {79 t.Run(fmt.Sprint(i), func(t *testing.T) {80 dir, err := ioutil.TempDir("", "syz-osutil-test")81 if err != nil {82 t.Fatal(err)83 }84 defer os.RemoveAll(dir)85 src := filepath.Join(dir, "src")86 dst := filepath.Join(dir, "dst")87 for _, file := range test.files {...

Full Screen

Full Screen

CopyFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err := utils.CopyFile("test.txt", "test1.txt")4 if err != nil {5 fmt.Println(err)6 }7 err = utils.CopyFiles("test.txt", "test1.txt")8 if err != nil {9 fmt.Println(err)10 }11}

Full Screen

Full Screen

CopyFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if len(os.Args) < 3 {4 fmt.Println("Usage: go run 2.go <src> <dest>")5 os.Exit(1)6 }7 err := osutil.CopyFiles(src, dest)8 if err != nil {9 fmt.Println(err)10 os.Exit(1)11 }12 fmt.Println("Files copied successfully")13}

Full Screen

Full Screen

CopyFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err := osutil.CopyFiles("/home/golang/1.go", "/home/golang/2.go")4 if err != nil {5 fmt.Println(err)6 }7}8import (9func main() {10 err := osutil.CopyFiles("/home/golang/1.go", "/home/golang/2.go")11 if err != nil {12 fmt.Println(err)13 }14}

Full Screen

Full Screen

CopyFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 beego.Debug("hello world")4 files = append(files, "1.xlsx")5 files = append(files, "2.xlsx")6 files = append(files, "3.xlsx")7 err := utils.CopyFiles("./", "./new", files)8 if err != nil {9 fmt.Println(err)10 os.Exit(1)11 }12 fmt.Println("done")13}14import (15func main() {16 beego.Debug("hello world")17 files = append(files, "1.xlsx")18 files = append(files, "2.xlsx")19 files = append(files, "3.xlsx")20 err := utils.CopyFiles("./", "./new", files)21 if err != nil {22 fmt.Println(err)23 os.Exit(1)24 }25 fmt.Println("done")26}27import (28func main() {29 beego.Debug("hello world")30 files = append(files, "1.xlsx")31 files = append(files, "2.xlsx")32 files = append(files, "3.xlsx")33 err := utils.CopyFiles("./", "./new", files)34 if err != nil {35 fmt.Println(err)36 os.Exit(1)37 }38 fmt.Println("done")39}40import (41func main() {42 beego.Debug("hello world")43 files = append(files, "1.xlsx")44 files = append(files, "2

Full Screen

Full Screen

CopyFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4}5import (6func main() {7 fmt.Println("Hello, playground")8}9import (10func main() {11 fmt.Println("Hello, playground")12}13import (14func main() {15 fmt.Println("Hello, playground")16}

Full Screen

Full Screen

CopyFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err := osutil.CopyFiles("C:\\Users\\username\\Desktop\\test.txt", "C:\\Users\\username\\Desktop\\test1.txt")4 if err != nil {5 fmt.Println(err)6 }7}8 2 File(s) 0 bytes9 2 Dir(s) 40,960,712,704 bytes free

Full Screen

Full Screen

CopyFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err := utils.CopyFile(src, dst)4 if err != nil {5 fmt.Println(err)6 } else {7 fmt.Println("File copied successfully")8 }9}10func CopyDir(src string, dst string) error11import (12func main() {13 err := utils.CopyDir(src, dst)14 if err != nil {15 fmt.Println(err)16 } else {17 fmt.Println("Directory copied successfully")18 }19}20func CopyDirEx(src string, dst string, filters ...string) error21import (22func main() {23 filters := []string{"*.txt"}24 err := utils.CopyDirEx(src, dst, filters...)25 if err != nil {26 fmt.Println(err)27 } else {28 fmt.Println("Directory copied successfully")29 }30}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful