Exemple #1
0
// Save writes config data in JSON format to a file.
func (d config) Save(filename string) (err error) {
	d.lock.Lock()
	defer d.lock.Unlock()

	jsonData, err := json.MarshalIndent(d.data, "", "\t")
	if err != nil {
		return iodine.New(err, nil)
	}

	file, err := atomic.FileCreate(filename)
	if err != nil {
		return iodine.New(err, nil)
	}

	if runtime.GOOS == "windows" {
		jsonData = []byte(strings.Replace(string(jsonData), "\n", "\r\n", -1))
	}
	_, err = file.Write(jsonData)
	if err != nil {
		return iodine.New(err, nil)
	}

	if err := file.Close(); err != nil {
		return iodine.New(err, nil)
	}
	return nil
}
Exemple #2
0
// CreateFile - create a file inside disk root path, replies with custome disk.File which provides atomic writes
func (disk Disk) CreateFile(filename string) (*atomic.File, error) {
	disk.lock.Lock()
	defer disk.lock.Unlock()

	if filename == "" {
		return nil, iodine.New(InvalidArgument{}, nil)
	}

	f, err := atomic.FileCreate(filepath.Join(disk.path, filename))
	if err != nil {
		return nil, iodine.New(err, nil)
	}

	return f, nil
}