Esempio n. 1
0
func (e *exportedImage) writeVersion(fs testfs.FileSystem) (err error) {
	fp := e.VersionPath
	f, err := fs.Create(fp)
	if err != nil {
		return
	}
	defer f.Close()

	_, err = f.WriteString("1.0")
	if err != nil {
		return
	}

	return
}
Esempio n. 2
0
func (e *exportedImage) writeData(fs testfs.FileSystem, data []byte) (err error) {
	fp := e.LayerTarPath
	f, err := fs.Create(fp)
	if err != nil {
		return
	}
	defer f.Close()

	_, err = f.Write(data)
	if err != nil {
		return
	}

	return err
}
Esempio n. 3
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
}
Esempio n. 4
0
func (e *exportedImage) writeJSON(fs testfs.FileSystem) (err error) {
	fp := e.JSONPath
	f, err := fs.Create(fp)
	if err != nil {
		return
	}
	defer f.Close()

	jb, err := json.Marshal(e.LayerConfig)
	if err != nil {
		return
	}

	_, err = f.WriteString(string(jb))
	if err != nil {
		return
	}

	return err
}
Esempio n. 5
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
}
Esempio n. 6
0
func archive(fs testfs.FileSystem) (out []byte, err error) {
	buf := new(bytes.Buffer)
	t := tar.NewWriter(buf)

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

	// Start at the root of the fs
	fs.Chdir("/")

	err = walk(fs, t)
	if err != nil {
		return
	}

	err = fs.Chdir(origDir)
	if err != nil {
		return
	}

	err = t.Close()

	return buf.Bytes(), err
}
Esempio n. 7
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
}
Esempio n. 8
0
// remove removes a file.
func (f *File) remove(fs testfs.FileSystem, r core.ResponseWriter) {
	err := fs.Remove(f.path)
	switch {

	// Don't error if the file is already removed
	case os.IsNotExist(err):
		r.Success(true)
		r.Changed(false)
		r.Message("info", f.path, " already removed")

	case err == nil:
		r.Success(true)
		r.Changed(true)
		r.Message("info", f.path, " removed")

	default:
		r.Message("error", err.Error())
		r.Success(false)

	}

	return
}
Esempio n. 9
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
}