// SendFile calls the specified method, and passes through the IO stream
344442
func (c *Client) SendFile(serviceMethod string, data io.Reader, ret interface{}) error {
	body, err := c.callWithRetry(serviceMethod, data, true)
	if err != nil {
		return err
	}
	defer body.Close()
	if err := json.NewDecoder(body).Decode(&ret); err != nil {
		logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
		return err
	}
	return nil
}
564567
func (h *fs) WriteFile(filename string, data []byte) error {
	return ioutil.WriteFile(filename, data, 0700)
}
1072361
func (dir *Dir) WriteFile(fileName string, reader io.Reader) (file *File, err error) {
	if err = dir.createIfNoExist(); err == nil {
		relativeDir := strings.TrimPrefix(dir.DirPath, dir.Filebox.BaseDir)
		file = dir.Filebox.AccessFile(filepath.Join(relativeDir, fileName), dir.Roles...)
		err = file.Write(reader)
	}
	return
}
27388
func sendFile(content_type string, hr HandlerReq) error {
	w, r := hr.w, hr.r
	req_file := path.Join(hr.Dir, hr.File)

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

	w.Header().Set("Content-Type", content_type)
	w.Header().Set("Content-Length", fmt.Sprintf("%d", f.Size()))
	w.Header().Set("Last-Modified", f.ModTime().Format(http.TimeFormat))
	http.ServeFile(w, r, req_file)

	return nil
}
1286870
func (c *Client) SendFileToRemote(fp string) error {
	logrus.Debugln("Verifying client")
	err := c.VerifyClient()
	if err != nil {
		return err
	}
	logrus.Debugln("Client passed verification")

	logrus.Debugln("Creating session")
	// Each ClientConn can support multiple interactive sessions,
	// represented by a Session.
	s, err := c.client.NewSession()
	if err != nil {
		return err
	}
	defer s.Close()

	logrus.Debugln("Creating session client")
	sc, err := NewSessionClient(s)
	if err != nil {
		return err
	}
	defer sc.writer.Close()

	sc.wg.Add(1)
	go sc.FileSource(fp)
	logrus.Infoln("Beginning transfer")
	go s.Run("/usr/bin/scp -t ./")
	logrus.Debugln("Waiting for transfer to complete...")
	sc.wg.Wait()
	for err := range sc.errors {
		logrus.WithError(err).Errorln("Error sending the file")
	}
	return nil
}
887035
func transferFile(c *fi.Context, source string, target string, sha string) error {

	// TODO drop file to disk, as vfs reads file into memory.  We load kubelet into memory for instance.
	// TODO in s3 can we do a copy file ... would need to test

	data, err := vfs.Context.ReadFile(source)
	if err != nil {
		if os.IsNotExist(err) {
			return fmt.Errorf("file not found %q: %v", source, err)
		}

		return fmt.Errorf("error downloading file %q: %v", source, err)
	}

	objectStore, err := buildVFSPath(target)
	if err != nil {
		return err
	}

	uploadVFS, err := vfs.Context.BuildVfsPath(objectStore)
	if err != nil {
		return fmt.Errorf("error building path %q: %v", objectStore, err)
	}

	shaTarget := objectStore + ".sha1"
	shaVFS, err := vfs.Context.BuildVfsPath(shaTarget)
	if err != nil {
		return fmt.Errorf("error building path %q: %v", shaTarget, err)
	}

	in := bytes.NewReader(data)
	dataHash, err := hashing.HashAlgorithmSHA1.Hash(in)
	if err != nil {
		return fmt.Errorf("unable to parse sha from file %q downloaded: %v", sha, err)
	}

	shaHash, err := hashing.FromString(strings.TrimSpace(sha))
	if err != nil {
		return fmt.Errorf("unable to hash sha: %q, %v", sha, err)
	}

	if !shaHash.Equal(dataHash) {
		return fmt.Errorf("the sha value in %q does not match %q calculated value %q", shaTarget, source, dataHash.String())
	}

	glog.Infof("uploading %q to %q", source, objectStore)
	if err := writeFile(c, uploadVFS, data); err != nil {
		return err
	}

	b := []byte(shaHash.Hex())
	if err := writeFile(c, shaVFS, b); err != nil {
		return err
	}

	return nil
}
1311630
func (bot *BotAPI) SendFile(u User, path string) (APIResponse, error) {
	file, err := os.Open(path)
	if err != nil {
		return APIResponse{}, err
	}
	defer file.Close()

	body := &bytes.Buffer{}
	writer := multipart.NewWriter(body)
	part, err := writer.CreateFormFile("filedata", filepath.Base(path))
	if err != nil {
		return APIResponse{}, err
	}
	_, err = io.Copy(part, file)

	usr, _ := json.Marshal(u)
	_ = writer.WriteField("recipient", string(usr))
	img := NewImageFromURL("")
	im, _ := json.Marshal(img)
	_ = writer.WriteField("message", string(im))

	err = writer.Close()
	if err != nil {
		return APIResponse{}, err
	}

	return bot.MakeRequest(body)
}
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
}
304265
func (self *HttpClient) UploadFile(method, path, fileParam, filePath string, payload []byte, params map[string]string, headers map[string]string) (*HttpResponse, error) {
	var reader io.Reader

	if payload == nil {
		file, err := os.Open(filePath)
		if err != nil {
			return nil, err
		}
		defer file.Close()
		reader = file
	} else {
		reader = bytes.NewReader(payload)
	}

	body := &bytes.Buffer{}
	writer := multipart.NewWriter(body)
	part, err := writer.CreateFormFile(fileParam, filepath.Base(filePath))
	if err == nil {
		_, err = io.Copy(part, reader)
	}
	if err == nil {
		for key, val := range params {
			writer.WriteField(key, val)
		}
		err = writer.Close()
	}
	if err != nil {
		return nil, err
	}

	if headers == nil {
		headers = map[string]string{}
	}

	headers["Content-Type"] = writer.FormDataContentType()
	headers["Content-Length"] = strconv.Itoa(body.Len())
	req := self.Request(method, path, body, headers)

	return self.Do(req)
}
581970
func (s *AccessLog) SetFile(v *FileAccessLog) *AccessLog {
	s.File = v
	return s
}
1067615
func CopyFile(srcFile, destFile string) error {
	file, err := os.Open(srcFile)
	if err != nil {
		return err
	}
	defer file.Close()
	dest, err := os.Create(destFile)
	if err != nil {
		return err
	}
	defer dest.Close()
	_, err = io.Copy(dest, file)
	return err
}
248862
func (this *FCGIClient) PostFile(p map[string]string, data url.Values, file map[string]string) (resp *http.Response, err error) {
	buf := &bytes.Buffer{}
	writer := multipart.NewWriter(buf)
	bodyType := writer.FormDataContentType()

	for key, val := range data {
		for _, v0 := range val {
			err = writer.WriteField(key, v0)
			if err != nil {
				return
			}
		}
	}

	for key, val := range file {
		fd, e := os.Open(val)
		if e != nil {
			return nil, e
		}
		defer fd.Close()

		part, e := writer.CreateFormFile(key, filepath.Base(val))
		if e != nil {
			return nil, e
		}
		_, err = io.Copy(part, fd)
	}

	err = writer.Close()
	if err != nil {
		return
	}

	return this.Post(p, bodyType, buf, buf.Len())
}
596379
func (s *Streamer) SendString(id, event, data string) {
	dataLen := len(data)
	lfCount := 0

	// We must sent a "data:{data}\n" for each line
	if dataLen > 0 {
		lfCount = strings.Count(data, "\n")
		if lfCount > 0 {
			dataLen += (5 * lfCount) // data:
		}
	}

	p := format(id, event, dataLen)

	// fill in data lines
	start := 0
	ins := len(p) - (2 + dataLen)
	for i := 0; lfCount > 0; i++ {
		if data[i] == '\n' {
			copy(p[ins:], data[start:i])
			ins += i - start
			copy(p[ins:], "\ndata:")
			ins += 6

			start = i + 1
			lfCount--
		}
	}
	copy(p[ins:], data[start:])

	s.event <- p
}
836862
func (r *Response) serveFile(file string) error {
	log.Debugf("serving file %s", file)

	http.ServeFile(r.writer, r.request, file)
	return nil
}
620386
func WriteFile(filename string, data []byte) error {
	os.MkdirAll(path.Dir(filename), os.ModePerm)
	return ioutil.WriteFile(filename, data, 0655)
}
1364418
func writeFile(path string, contents []byte) {
	handleError(ioutil.WriteFile(path, contents, 0640))
}
451563
func CloseFile(f *os.File) error {
	atomic.AddUint64(&fileCount, ^uint64(0)) // decrement
	return f.Close()
}