// SaveToFile writes the configuration into a file.
764425
func (g *Generator) SaveToFile(path string, exportOpts ExportOptions) error {
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()
	return g.Save(f, exportOpts)
}
520208
func (cert *Certificate) WriteToDERFile(filename string) (err error) {
	certOut, err := os.Create(filename)
	if err != nil {
		return fmt.Errorf("Failed to open %s for writing: %s", filename, err)
	}
	defer func() {
		if err := certOut.Close(); err != nil {
			log.Debugf("Unable to close file: %v", err)
		}
	}()
	_, err = certOut.Write(cert.derBytes)
	return err
}
526695
func (r *Request) ToFile(filename string) error {
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer f.Close()

	resp, err := r.getResponse()
	if err != nil {
		return err
	}
	if resp.Body == nil {
		return nil
	}
	defer resp.Body.Close()
	_, err = io.Copy(f, resp.Body)
	return err
}
56204
func (c *Config) SaveProfile(profileAliasHost, profileDir string, profileOptions ...ProfileOptions) error {
	if c.WebProxyAddr == "" {
		return nil
	}

	// The profile is saved to a directory with the name of the proxy web endpoint.
	webProxyHost, _ := c.WebProxyHostPort()
	profileDir = FullProfilePath(profileDir)
	profilePath := path.Join(profileDir, webProxyHost) + ".yaml"

	profileAliasPath := ""
	if profileAliasHost != "" {
		profileAliasPath = path.Join(profileDir, profileAliasHost) + ".yaml"
	}

	var cp ClientProfile
	cp.Username = c.Username
	cp.WebProxyAddr = c.WebProxyAddr
	cp.SSHProxyAddr = c.SSHProxyAddr
	cp.KubeProxyAddr = c.KubeProxyAddr
	cp.ForwardedPorts = c.LocalForwardPorts.String()
	cp.SiteName = c.SiteName

	// create a profile file and set it current base on the option
	var opts ProfileOptions
	if len(profileOptions) == 0 {
		// default behavior is to override the profile
		opts = ProfileMakeCurrent
	} else {
		for _, flag := range profileOptions {
			opts |= flag
		}
	}
	if err := cp.SaveTo(ProfileLocation{
		AliasPath: profileAliasPath,
		Path:      profilePath,
		Options:   opts,
	}); err != nil {
		return trace.Wrap(err)
	}
	return nil
}
1233891
func (c Client) Save() error {
	settings := settingsFile{Username: c.Username, SslVerify: c.SSLVerify,
		Controller: c.ControllerURL.String(), Token: c.Token, Limit: c.ResponseLimit}

	if settings.Limit <= 0 {
		settings.Limit = DefaultResponseLimit
	}

	settingsContents, err := json.Marshal(settings)

	if err != nil {
		return err
	}

	if err = os.MkdirAll(path.Join(FindHome(), "/.deis/"), 0700); err != nil {
		return err
	}

	return ioutil.WriteFile(locateSettingsFile(), settingsContents, 0600)
}
1273939
func (b *Builder) WriteToFile(w io.Writer) error {
	if _, err := w.Write(header); err != nil {
		return err
	}

	if err := binary.Write(w, binary.LittleEndian, version); err != nil {
		return err
	}

	key, signature, err := b.signContent()
	if err != nil {
		return err
	}

	if err := binary.Write(w, binary.LittleEndian, uint32(len(key))); err != nil {
		return err
	}

	if err := binary.Write(w, binary.LittleEndian, uint32(len(signature))); err != nil {
		return err
	}

	if _, err := w.Write(key); err != nil {
		return err
	}

	if _, err := w.Write(signature); err != nil {
		return err
	}

	if _, err := io.Copy(w, b.Content); err != nil {
		return err
	}

	return nil
}
102994
func writeFile(filename string, data []byte) error {
	safeFile, err := safe.CreateFile(filename)
	if err != nil {
		return err
	}
	_, err = safeFile.Write(data)
	if err != nil {
		return err
	}
	return safeFile.Close()
}
1293032
func (c *Conf) Save(src interface{}) (err error) {
	nb, err := json.Marshal(src)
	if err != nil {
		return
	}

	err = ioutil.WriteFile(c.Path(), nb, 0666)
	if err != nil {
		return
	}

	return
}
1181205
func (configFile *ConfigFile) Save() error {
	if configFile.Filename == "" {
		return errors.Errorf("Can't save config with empty filename")
	}

	dir := filepath.Dir(configFile.Filename)
	if err := os.MkdirAll(dir, 0700); err != nil {
		return err
	}
	temp, err := ioutil.TempFile(dir, filepath.Base(configFile.Filename))
	if err != nil {
		return err
	}
	err = configFile.SaveToWriter(temp)
	temp.Close()
	if err != nil {
		os.Remove(temp.Name())
		return err
	}
	return os.Rename(temp.Name(), configFile.Filename)
}
846730
func (kp *KeyPair) WriteToFile(privateKeyPath string, publicKeyPath string) error {
	files := []struct {
		File  string
		Type  string
		Value []byte
	}{
		{
			File:  privateKeyPath,
			Value: kp.PrivateKey,
		},
		{
			File:  publicKeyPath,
			Value: kp.PublicKey,
		},
	}

	for _, v := range files {
		f, err := os.Create(v.File)
		if err != nil {
			return ErrUnableToWriteFile
		}

		if _, err := f.Write(v.Value); err != nil {
			return ErrUnableToWriteFile
		}

		// windows does not support chmod
		switch runtime.GOOS {
		case "darwin", "linux":
			if err := f.Chmod(0600); err != nil {
				return err
			}
		}
	}

	return nil
}
489656
func logToFile(writer io.Writer, prefix string, m params.LogRecord) error {
	_, err := writer.Write([]byte(strings.Join([]string{
		prefix,
		m.Entity,
		m.Time.In(time.UTC).Format("2006-01-02 15:04:05"),
		m.Level,
		m.Module,
		m.Location,
		m.Message,
	}, " ") + "\n"))
	return err
}
352319
func (r *Response) Save(fileName string) error {
	return ioutil.WriteFile(fileName, r.Body, 0644)
}
849277
func SaveConfig(conf *Config) error {
	fullConfigFile, err := GetConfigFilePath(configFile)
	if err != nil {
		return err
	}

	out, err := yaml.Marshal(*conf)
	if err != nil {
		return err
	}

	f, err := os.Create(fullConfigFile)
	if err != nil {
		return err
	}
	defer f.Close()

	_, err = f.Write(out)
	return err
}
89163
func (fs *statFS) WriteFile(
	ctx context.Context,
	op *fuseops.WriteFileOp) (err error) {
	fs.mu.Lock()
	defer fs.mu.Unlock()

	fs.mostRecentWriteSize = len(op.Data)
	return
}
206993
func SaveFile(filePath, contents string, takeBackup bool) error {
	backupFile := ""
	if takeBackup {
		tmpDir := os.TempDir()
		fileName := fmt.Sprintf("%s_%v", filepath.Base(filePath), GetUniqueID())
		backupFile = filepath.Join(tmpDir, fileName)
		err := CopyFile(filePath, backupFile)
		if err != nil {
			return fmt.Errorf("Failed to make backup for '%s': %s", filePath, err.Error())
		}
	}
	err := ioutil.WriteFile(filePath, []byte(contents), NewFilePermissions)
	if err != nil {
		return fmt.Errorf("Failed to write to '%s': %s", filePath, err.Error())
	}

	return nil
}
1169024
func (i *IniParser) WriteFile(filename string, options IniOptions) error {
	return writeIniToFile(i, filename, options)
}
292266
func (c *Cert) writeKeyToFile(path string) error {
	pemfile, err := os.Create(path)
	if err != nil {
		return fmt.Errorf("failed to create pem key file for writting: %v", err)
	}

	if err := pem.Encode(pemfile, c.Data()); err != nil {
		return fmt.Errorf("failed to encode key to pem file at'%s': %v", path, err)
	}

	if err := pemfile.Close(); err != nil {
		return fmt.Errorf("error closing pem file at '%s': %v", path, err)
	}

	return nil
}