示例#1
0
文件: path.go 项目: jempe/hugo
func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
	dir, _ := filepath.Split(inpath)
	ospath := filepath.FromSlash(dir)

	if ospath != "" {
		err = fs.MkdirAll(ospath, 0777) // rwx, rw, r
		if err != nil {
			return
		}
	}

	exists, err := Exists(inpath, fs)
	if err != nil {
		return
	}
	if exists {
		return fmt.Errorf("%v already exists", inpath)
	}

	file, err := fs.Create(inpath)
	if err != nil {
		return
	}
	defer file.Close()

	_, err = io.Copy(file, r)
	return
}
示例#2
0
// getSettingsFolder returns the path of the settings folder
// and ensures that the folder exists.
func getSettingsFolder(fs afero.Fs, baseFolder string) string {
	settingsFolder := filepath.Join(baseFolder, ".dee")
	createFolderError := fs.MkdirAll(settingsFolder, 0700)
	if createFolderError != nil {
		panic(createFolderError)
	}

	return settingsFolder
}
示例#3
0
func newFile(testName string, fs afero.Fs, t *testing.T) (f afero.File) {
	fs.MkdirAll(testDir, 0777)
	f, err := fs.Create(path.Join(testDir, testName))
	if err != nil {
		t.Fatalf("%v: create %s: %s", fs.Name(), testName, err)
	}
	_, err = f.WriteString("")
	if err != nil {
		t.Fatalf("%v: writestring %s: %s", fs.Name(), testName, err)
	}
	return f
}
示例#4
0
文件: path.go 项目: jempe/hugo
func WriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
	dir, _ := filepath.Split(inpath)
	ospath := filepath.FromSlash(dir)

	if ospath != "" {
		err = fs.MkdirAll(ospath, 0777) // rwx, rw, r
		if err != nil {
			if err != os.ErrExist {
				panic(err)
			}
		}
	}

	file, err := fs.Create(inpath)
	if err != nil {
		return
	}
	defer file.Close()

	_, err = io.Copy(file, r)
	return
}