Exemplo n.º 1
0
// touch a file.  Same as system touch.
func (f *File) touch(fs testfs.FileSystem, r core.ResponseWriter) {
	file, err := fs.OpenFile(f.path, os.O_CREATE|os.O_WRONLY, f.mode)
	if err != nil {
		r.Message("error", err.Error())
		r.Success(false)
		return
	}
	err = file.Close()
	if err != nil {
		r.Message("error", err.Error())
		r.Success(false)
		return
	}
	r.Message("info", "touched ", f.path)
	r.Success(true)
	r.Changed(true)
	return
}
Exemplo n.º 2
0
// set sets the contents of a file.
func (f *File) set(fs testfs.FileSystem, r core.ResponseWriter) {
	var (
		file testfs.File
		err  error
	)

	// If we have a valid checksum for the input data and the file exists, read it
	// and avoid modifying the file if possible.
	if f.matching(fs) {
		r.Success(true)
		r.Changed(false)
		r.Message("info", f.path, " unchanged")
		return
	}

	file, err = fs.OpenFile(f.path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, f.mode)
	if err != nil {
		r.Message("error", err.Error())
		r.Success(false)
		return
	}

	err = bufferedWrite(f.data, file)
	if err != nil {
		r.Message("error", err.Error())
		r.Success(false)
		return
	}

	err = file.Close()
	if err != nil {
		r.Message("error", err.Error())
		r.Success(false)
		return
	}

	r.Message("info", "set content of ", f.path)
	r.Success(true)
	r.Changed(true)
	return
}
Exemplo n.º 3
0
// matching returns true if a file matches the checksum, false otherwise
func (f *File) matching(fs testfs.FileSystem) bool {
	if f.sum == nil || len(f.sum) == 0 {
		return false
	}
	_, err := fs.Stat(f.path)
	if err != nil {
		return false
	}
	file, err := fs.OpenFile(f.path, os.O_RDONLY, 0)
	if err != nil {
		return false
	}
	data, err := ioutil.ReadAll(file)
	if err != nil {
		return false
	}

	if bytes.Compare(f.sum, data) == 0 {
		return true
	}

	return false
}
Exemplo n.º 4
0
func walk(fs testfs.FileSystem, t *tar.Writer) (err error) {

	cwd, err := fs.Getwd()
	if err != nil {
		return
	}

	f, err := fs.OpenFile(cwd, os.O_RDONLY, 0)
	if err != nil {
		return
	}

	fi, err := f.Readdir(-1)
	if err != nil {
		return
	}

	for _, file := range fi {
		// Add a new file to the archive
		var hdr *tar.Header
		hdr, err = tarHdr(file)
		if err != nil {
			return
		}

		// Make header name fully qualified
		if cwd[len(cwd)-1] == '/' {
			hdr.Name = cwd + hdr.Name
		} else {
			hdr.Name = cwd + "/" + hdr.Name
		}

		// Strip leading slashes
		if hdr.Name[0] == '/' {
			hdr.Name = hdr.Name[1:]
		}

		err = t.WriteHeader(hdr)
		if err != nil {
			return
		}

		// Write the file data to the archive
		if !file.IsDir() && file.Size() > 0 {
			var target testfs.File

			target, err = fs.Open(file.Name())
			if err != nil {
				return
			}

			_, err = io.Copy(t, target)
			if err != nil {
				return
			}
		}

		// Close the file in the archive
		err = t.Flush()
		if err != nil {
			return
		}

		// If a directory, recurse into it
		if file.IsDir() {
			err = fs.Chdir(file.Name())
			if err != nil {
				return
			}

			return walk(fs, t)
		}

	}

	return
}