Exemple #1
0
// Create a single directory on remote server
func mkone(c *g9pc.Client, s string) {
	fname := normpath(s)
	file, err := c.FCreate(fname, 0777|g9p.DMDIR, g9p.OWRITE)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error creating directory %s: %s\n", fname, err)
		return
	}
	file.Close()
}
Exemple #2
0
// Copy a local file to remote server
func cmdput(c *g9pc.Client, s []string) {
	var from, to string
	switch len(s) {
	case 1:
		_, to = path.Split(s[0])
		to = normpath(to)
		from = s[0]
	case 2:
		from, to = s[0], normpath(s[1])
	default:
		fmt.Fprintf(os.Stderr, "incorrect arguments; usage: put local [remote]\n")
	}

	fromfile, err := os.Open(from)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error opening %s for reading: %s\n", from, err)
		return
	}
	defer fromfile.Close()

	file, ferr := c.FOpen(to, g9p.OWRITE|g9p.OTRUNC)
	if ferr != nil {
		file, ferr = c.FCreate(to, 0666, g9p.OWRITE)
		if ferr != nil {
			fmt.Fprintf(os.Stderr, "error opening %s for writing: %s\n", to, err)
			return
		}
	}
	defer file.Close()

	buf := make([]byte, 8192)
	for {
		n, oserr := fromfile.Read(buf)
		if oserr != nil && oserr != io.EOF {
			fmt.Fprintf(os.Stderr, "error reading %s: %s\n", from, oserr)
			return
		}

		if n == 0 {
			break
		}

		m, err := file.Write(buf[0:n])
		if err != nil {
			fmt.Fprintf(os.Stderr, "error writing %s: %s\n", to, err)
			return
		}

		if m != n {
			fmt.Fprintf(os.Stderr, "short write %s\n", to)
			return
		}
	}
}
Exemple #3
0
// Write the string s to remote file f. Create f if it doesn't exist
func writeone(c *g9pc.Client, f, s string) {
	fname := normpath(f)
	file, err := c.FOpen(fname, g9p.OWRITE|g9p.OTRUNC)
	if err != nil {
		file, err = c.FCreate(fname, 0666, g9p.OWRITE)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error opening %s: %v\n", fname, err)
			return
		}
	}
	defer file.Close()

	m, err := file.Write([]byte(s))
	if err != nil {
		fmt.Fprintf(os.Stderr, "error writing to %s: %s\n", fname, err)
		return
	}

	if m != len(s) {
		fmt.Fprintf(os.Stderr, "short write %s\n", fname)
		return
	}
}