Example #1
0
func mkdirNormalMasterRun(master *Master, arg string, rep *WorkResponse) {
	rootless := strings.TrimLeft(arg, "/")
	dir, _ := SplitPath(rootless)
	dirAttr := master.fileServer.attributes.Get(dir)
	if dirAttr.Deletion() {
		rep.Stderr = fmt.Sprintf("File not found: /%s", dir)
		rep.Exit = syscall.WaitStatus(1 << 8)
		return
	}

	if !dirAttr.IsDir() {
		rep.Stderr = fmt.Sprintf("Is not a directory: /%s", dir)
		rep.Exit = syscall.WaitStatus(1 << 8)
		return
	}

	chAttr := master.fileServer.attributes.Get(rootless)
	if !chAttr.Deletion() {
		rep.Stderr = fmt.Sprintf("File exists: /%s", rootless)
		rep.Exit = syscall.WaitStatus(1 << 8)
		return
	}
	chAttr = mkdirEntry(rootless)

	fs := attr.FileSet{}

	ct := chAttr.ChangeTime()
	mt := chAttr.ModTime()
	dirAttr.SetTimes(nil, &mt, &ct)
	fs.Files = append(fs.Files, dirAttr, chAttr)
	master.replay(fs)
}
Example #2
0
func rmMaybeMasterRun(master *Master, req *WorkRequest, rep *WorkResponse) bool {
	g := Getopt(req.Argv[1:], nil, nil, true)

	force := g.HasLong("force") || g.HasShort('f')
	delete(g.Long, "force")
	delete(g.Short, 'f')

	recursive := g.HasLong("recursive") || g.HasShort('r') || g.HasShort('R')
	delete(g.Long, "recursive")
	delete(g.Short, 'R')
	delete(g.Short, 'r')

	if g.HasOptions() {
		return false
	}

	log.Println("Running in master:", req.Summary())
	todo := []string{}
	for _, a := range g.Args {
		if a[0] != '/' {
			a = filepath.Join(req.Dir, a)
		}
		a = strings.TrimLeft(filepath.Clean(a), "/")
		todo = append(todo, a)
	}

	fs := attr.FileSet{}
	msgs := []string{}
	status := 0
	now := time.Now()
	if recursive {
		for _, t := range todo {
			parentDir, _ := SplitPath(t)
			parent := master.attributes.Get(parentDir)
			if parent.Deletion() {
				continue
			}

			parent.SetTimes(nil, &now, &now)
			fs.Files = append(fs.Files, parent)
			for _, n := range recurseNames(master, t) {
				fs.Files = append(fs.Files, &attr.FileAttr{
					Path: n,
				})
			}
		}
	} else {
		for _, p := range todo {
			a := master.fileServer.attributes.GetDir(p)
			switch {
			case a.Deletion():
				if !force {
					msgs = append(msgs, fmt.Sprintf("rm: no such file or directory: %s", p))
					status = 1
				}
			case a.IsDir() && !recursive:
				msgs = append(msgs, fmt.Sprintf("rm: is a directory: %s", p))
				status = 1
			default:
				parentDir, _ := SplitPath(p)
				parentAttr := master.attributes.Get(parentDir)
				parentAttr.SetTimes(nil, &now, &now)
				fs.Files = append(fs.Files, parentAttr, &attr.FileAttr{Path: p})
			}
		}
	}
	master.replay(fs)

	rep.Stderr = strings.Join(msgs, "\n")
	rep.Exit = syscall.WaitStatus(status << 8)
	return true
}