// packFile packs a file or directory to zip.Writer.
9826
func packFile(srcFile string, recPath string, zw *zip.Writer, fi os.FileInfo) error {
	if fi.IsDir() {
		fh, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}
		fh.Name = recPath + "/"
		if _, err = zw.CreateHeader(fh); err != nil {
			return err
		}
	} else {
		fh, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}
		fh.Name = recPath
		fh.Method = zip.Deflate

		fw, err := zw.CreateHeader(fh)
		if err != nil {
			return err
		}

		if fi.Mode()&os.ModeSymlink != 0 {
			target, err := os.Readlink(srcFile)
			if err != nil {
				return err
			}
			if _, err = fw.Write([]byte(target)); err != nil {
				return err
			}
		} else {
			f, err := os.Open(srcFile)
			if err != nil {
				return err
			}
			defer f.Close()
			
			if _, err = io.Copy(fw, f); err != nil {
				return err
			}
		}
	}
	return nil
}
393736
func writeZip(writer io.Writer, files []fileEntry) error {
	zipWriter := zip.NewWriter(writer)
	defer zipWriter.Close()

	for _, file := range files {
		zipFile, err := zipWriter.Create(file.name)
		if err != nil {
			return err
		}

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

	return zipWriter.Close()
}
1251906
func packToWriter(srcPath string, w io.Writer, fn func(fullName string, fi os.FileInfo) error, includeDir bool) error {
	zw := zip.NewWriter(w)
	defer zw.Close()

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

	fi, err := f.Stat()
	if err != nil {
		return err
	}

	basePath := path.Base(srcPath)
	if fi.IsDir() {
		if includeDir {
			if err = packFile(srcPath, basePath, zw, fi); err != nil {
				return err
			}
		} else {
			basePath = ""
		}
		return packDir(srcPath, basePath, zw, fn)
	}
	return packFile(srcPath, basePath, zw, fi)
}
1018065
func WriteFile(filename string, data []byte, perm os.FileMode) error {
	err := ioutil_base.WriteFile(filename, data, perm)
	if err != nil {
		return errors.Wrapf(err, "failed to write file %q", filename)
	}
	return nil
}
399546
func (f *ResetCachesFile) WriteFile(ctx context.Context, fi *dokan.FileInfo, bs []byte, offset int64) (n int, err error) {
	f.fs.logEnter(ctx, "ResetCachesFile Write")
	defer func() { f.fs.reportErr(ctx, libkbfs.WriteMode, err) }()
	if len(bs) == 0 {
		return 0, nil
	}
	f.fs.config.ResetCaches()
	return len(bs), nil
}
950912
func (s *StreamArchive) StreamFile(relPath string, fi os.FileInfo, data []byte) error {
	if fi.IsDir() {
		fh, err := tar.FileInfoHeader(fi, "")
		if err != nil {
			return err
		}
		fh.Name = relPath + "/"
		if err = s.Writer.WriteHeader(fh); err != nil {
			return err
		}
	} else {
		target := ""
		if fi.Mode()&os.ModeSymlink != 0 {
			target = string(data)
		}

		fh, err := tar.FileInfoHeader(fi, target)
		if err != nil {
			return err
		}
		fh.Name = filepath.Join(relPath, fi.Name())
		if err = s.Writer.WriteHeader(fh); err != nil {
			return err
		}

		if _, err = s.Writer.Write(data); err != nil {
			return err
		}
	}
	return nil
}
43337
func ToFile(w io.Writer, level int) Logger {
	return file{w, level}
}
75434
func PackMap(cmd BufferEx, theMap MapIter) (int, error) {
	return packMap(cmd, theMap)
}
144508
func (d *Dir) WriteFile(subPath string, data []byte, perm os.FileMode) error {
	dirPath := path.Dir(subPath)
	d.MkdirAll(dirPath, perm)
	node, err := d.GetByPath(subPath)
	if err != nil {
		//create new file if not exist
		node, err = d.GetByPath(dirPath)
		if err != nil {
			return err
		}
		if !node.IsDir() {
			return fmt.Errorf("There is a file on path " + dirPath)
		}
		var baseDir = node.(*Dir)
		baseDir.AddNode(&File{
			name:     path.Base(subPath),
			filemode: perm,
			time:     time.Now(),
			data:     data,
		})
		return nil
	}
	//overwrite file
	if node.IsDir() {
		return fmt.Errorf("Use WriteFile on directory")
	}
	var file = node.(*File)
	file.SetData(data)
	return nil
}
624189
func PackList(cmd BufferEx, list ListIter) (int, error) {
	return packList(cmd, list)
}
1162742
func (f *UpdatesFile) WriteFile(ctx context.Context, fi *dokan.FileInfo, bs []byte, offset int64) (n int, err error) {
	f.folder.fs.logEnter(ctx, "UpdatesFile WriteFile")
	defer func() { f.folder.fs.reportErr(ctx, libkbfs.WriteMode, err) }()
	f.folder.fs.log.CDebugf(ctx, "UpdatesFile (enable: %t) Write", f.enable)
	if len(bs) == 0 {
		return 0, nil
	}

	f.folder.updateMu.Lock()
	defer f.folder.updateMu.Unlock()
	if f.enable {
		if f.folder.updateChan == nil {
			return 0, errors.New("Updates are already enabled")
		}
		err = libkbfs.RestartCRForTesting(
			libcontext.BackgroundContextWithCancellationDelayer(),
			f.folder.fs.config, f.folder.getFolderBranch())
		if err != nil {
			return 0, err
		}
		f.folder.updateChan <- struct{}{}
		close(f.folder.updateChan)
		f.folder.updateChan = nil
	} else {
		if f.folder.updateChan != nil {
			return 0, errors.New("Updates are already disabled")
		}
		f.folder.updateChan, err =
			libkbfs.DisableUpdatesForTesting(f.folder.fs.config,
				f.folder.getFolderBranch())
		if err != nil {
			return 0, err
		}
		err = libkbfs.DisableCRForTesting(f.folder.fs.config,
			f.folder.getFolderBranch())
		if err != nil {
			return 0, err
		}
	}
	// Because we store state in the folder it must not be forgotten
	// even if it appears empty and unused.
	f.folder.noForget = true

	return len(bs), err
}
1176147
func LoadFile(config interface{}, file string, opts Options) error {
	return LoadFiles(config, []string{file}, opts)
}
27390
func openPackageFile(path string) (*PackageFile, error) {
	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	fi, err := f.Stat()
	if err != nil {
		return nil, err
	}

	p, err := ReadPackageFile(f)
	if err != nil {
		return nil, err
	}
	p.path = path
	p.fileSize = uint64(fi.Size())
	p.fileTime = fi.ModTime()
	return p, nil
}
80925
func (l *bunyanLog) writeFile(stream Stream, data []byte) error {
	if f, err := os.OpenFile(stream.Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err != nil {
		log.Printf("[bunyan] error: %v", err)
	} else if _, err := f.Write(data); err != nil {
		log.Printf("[bunyan] error: %v", err)
	} else if err := f.Close(); err != nil {
		log.Printf("[bunyan] error: %v", err)
	}
	return nil
}
512734
func (r *registry) loadFile(file *descriptor.FileDescriptorProto) {
	pkg := &GoPackage{
		Path: r.goPackagePath(file),
		Name: defaultGoPackageName(file),
	}
	if err := r.ReserveGoPackageAlias(pkg.Name, pkg.Path); err != nil {
		for i := 0; ; i++ {
			alias := fmt.Sprintf("%s_%d", pkg.Name, i)
			if err := r.ReserveGoPackageAlias(alias, pkg.Path); err == nil {
				pkg.Alias = alias
				break
			}
		}
	}
	f := &File{
		FileDescriptorProto: file,
		GoPackage:           pkg,
	}
	r.files[file.GetName()] = f
	r.registerMsg(f, nil, file.GetMessageType())
	r.registerEnum(f, nil, file.GetEnumType())
}
245020
func (a Afero) WriteFile(filename string, data []byte, perm os.FileMode) error {
	return WriteFile(a.Fs, filename, data, perm)
}
1070000
func (m *FileTransferRes) Pack() []byte {
	data := new(bytes.Buffer)
	binary.Write(data, binary.LittleEndian, m.TRANSFER_UID)
	binary.Write(data, binary.LittleEndian, m.RESULT)
	return data.Bytes()
}