当前位置: 首页>>代码示例>>Golang>>正文


Golang File.Seek方法代码示例

本文整理汇总了Golang中os.File.Seek方法的典型用法代码示例。如果您正苦于以下问题:Golang File.Seek方法的具体用法?Golang File.Seek怎么用?Golang File.Seek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在os.File的用法示例。


在下文中一共展示了File.Seek方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: getTarFileBytes

func getTarFileBytes(file *os.File, path string) ([]byte, error) {
	_, err := file.Seek(0, 0)
	if err != nil {
		fmt.Errorf("error seeking file: %v", err)
	}

	var fileBytes []byte
	fileWalker := func(t *tarball.TarFile) error {
		if filepath.Clean(t.Name()) == path {
			fileBytes, err = ioutil.ReadAll(t.TarStream)
			if err != nil {
				return err
			}
		}

		return nil
	}

	tr := tar.NewReader(file)
	if err := tarball.Walk(*tr, fileWalker); err != nil {
		return nil, err
	}

	if fileBytes == nil {
		return nil, fmt.Errorf("file %q not found", path)
	}

	return fileBytes, nil
}
开发者ID:rtrentin73,项目名称:docker2aci,代码行数:29,代码来源:file.go

示例2: Zip

func (zipper ApplicationZipper) Zip(dirOrZipFilePath string, targetFile *os.File) error {
	if zipper.IsZipFile(dirOrZipFilePath) {
		zipFile, err := os.Open(dirOrZipFilePath)
		if err != nil {
			return err
		}
		defer zipFile.Close()

		_, err = io.Copy(targetFile, zipFile)
		if err != nil {
			return err
		}
	} else {
		err := writeZipFile(dirOrZipFilePath, targetFile)
		if err != nil {
			return err
		}
	}

	_, err := targetFile.Seek(0, os.SEEK_SET)
	if err != nil {
		return err
	}

	return nil
}
开发者ID:jasonkeene,项目名称:cli,代码行数:26,代码来源:zipper.go

示例3: newFileEncoder

// newFileEncoder creates a new encoder with current file offset for the page writer.
func newFileEncoder(f *os.File, prevCrc uint32) (*encoder, error) {
	offset, err := f.Seek(0, os.SEEK_CUR)
	if err != nil {
		return nil, err
	}
	return newEncoder(f, prevCrc, int(offset)), nil
}
开发者ID:kubernetes,项目名称:kubernetes,代码行数:8,代码来源:encoder.go

示例4: initFileOffset

func (h *Harvester) initFileOffset(file *os.File) error {
	offset, err := file.Seek(0, os.SEEK_CUR)

	if h.getOffset() > 0 {
		// continue from last known offset

		logp.Debug("harvester",
			"harvest: %q position:%d (offset snapshot:%d)", h.Path, h.getOffset(), offset)
		_, err = file.Seek(h.getOffset(), os.SEEK_SET)
	} else if h.Config.TailFiles {
		// tail file if file is new and tail_files config is set

		logp.Debug("harvester",
			"harvest: (tailing) %q (offset snapshot:%d)", h.Path, offset)
		offset, err = file.Seek(0, os.SEEK_END)
		h.SetOffset(offset)

	} else {
		// get offset from file in case of encoding factory was
		// required to read some data.
		logp.Debug("harvester", "harvest: %q (offset snapshot:%d)", h.Path, offset)
		h.SetOffset(offset)
	}

	return err
}
开发者ID:yhyang1,项目名称:beats,代码行数:26,代码来源:log.go

示例5: dataLookup

func (wndb *WordNetDb) dataLookup(fh *os.File, offset int64) ([]byte, error) {
	_, err := fh.Seek(offset, os.SEEK_SET)
	if err != nil {
		return nil, err
	}

	buffer := make([]byte, BUFFSIZE) // initial size of the buffer is 3kb
	line := make([]byte, 0, BUFFSIZE)
	prevLen := 0
	for {
		prevLen = len(line)
		n, err := fh.Read(buffer) // we read the next 3kb (or less)
		if err != nil && err != io.EOF {
			return nil, err
		}
		line = append(line, buffer[:]...)
		until := bytes.IndexByte(buffer, '\n')
		if until > 0 { // We have a full line
			return line[:prevLen+until], nil
		}
		if err == io.EOF || n < BUFFSIZE {
			return line, nil
		}
	}
	return nil, ERR_MSG(UNREACHABLE_CODE)
}
开发者ID:emepyc,项目名称:gown,代码行数:26,代码来源:search.go

示例6: RewindFile

func RewindFile(fileHandle *os.File) {
	_, rewindErr := fileHandle.Seek(0, 0)

	if rewindErr != nil {
		log.Fatalln("Unable to rewind file")
	}
}
开发者ID:thacker99,项目名称:textql,代码行数:7,代码来源:file_helpers.go

示例7: NewDraw

func NewDraw(fp *os.File, labels []string) *Draw {
	w, h := termbox.Size()
	tab := INPROCESS

	inproc, arch, err := ReadCSV(fp)
	if err != nil {
		panic(err)
	}
	fp.Seek(0, 0)

	data := &Data{Labels: &LabelList{}}
	data.InProcess = inproc
	data.Archive = arch
	*data.Labels = labels

	view := mode.NewView(w, h)
	view.Reset()
	drawer := &mode.NormalDraw{
		View: *view,
		Tab:  tab,
	}
	drawer.SetLister(data.InProcess)

	return &Draw{
		Drawer: drawer,
		data:   data,
		tab:    tab,
		file:   fp,
	}
}
开发者ID:mizkei,项目名称:yalzo,代码行数:30,代码来源:draw.go

示例8: uploadToS3

func (r *Runner) uploadToS3(file *os.File, b *Build, boundary string) string {
	name := fmt.Sprintf("%s-build-%s-%s.txt", b.ID, b.Commit, time.Now().Format("2006-01-02-15-04-05"))
	url := fmt.Sprintf("https://s3.amazonaws.com/%s/%s", logBucket, name)

	if _, err := file.Seek(0, os.SEEK_SET); err != nil {
		log.Printf("failed to seek log file: %s\n", err)
		return ""
	}

	stat, err := file.Stat()
	if err != nil {
		log.Printf("failed to get log file size: %s\n", err)
		return ""
	}

	log.Printf("uploading build log to S3: %s\n", url)
	if err := s3attempts.Run(func() error {
		contentType := "multipart/mixed; boundary=" + boundary
		acl := "public-read"
		_, err := r.s3.PutObject(&s3.PutObjectRequest{
			Key:           &name,
			Body:          file,
			Bucket:        &logBucket,
			ACL:           &acl,
			ContentType:   &contentType,
			ContentLength: typeconv.Int64Ptr(stat.Size()),
		})
		return err
	}); err != nil {
		log.Printf("failed to upload build output to S3: %s\n", err)
	}
	return url
}
开发者ID:yanghongkjxy,项目名称:flynn,代码行数:33,代码来源:runner.go

示例9: dataLookup

func dataLookup(fh *os.File, offset int64) []byte {
	_, err := fh.Seek(offset, os.SEEK_SET)
	if err != nil {
		log.Fatal(err)
	}

	buffer := make([]byte, BUFFSIZE) // initial size of the buffer is 3kb
	line := make([]byte, 0, BUFFSIZE)
	prevLen := 0
	for {
		prevLen = len(line)
		n, err := fh.Read(buffer) // we read the next 3kb (or less)
		if err != nil && err != io.EOF {
			log.Fatal(err)
		}
		line = append(line, buffer[:]...)
		until := bytes.IndexByte(buffer, '\n')
		if until > 0 { // We have a full line
			return line[:prevLen+until]
		}
		if err == io.EOF || n < BUFFSIZE {
			return line
		}
	}
	log.Fatal("We can't be here")
	return nil
}
开发者ID:emepyc,项目名称:gown,代码行数:27,代码来源:readLine.go

示例10: MakePrimitiveBlockReader

func MakePrimitiveBlockReader(file *os.File) <-chan blockData {
	retval := make(chan blockData)

	go func() {
		file.Seek(0, os.SEEK_SET)
		for {
			filePosition, err := file.Seek(0, os.SEEK_CUR)

			blobHeader, err := ReadNextBlobHeader(file)
			if err == io.EOF {
				break
			} else if err != nil {
				println("Blob header read error:", err.Error())
				os.Exit(2)
			}

			blobBytes, err := readBlock(file, *blobHeader.Datasize)
			if err != nil {
				println("Blob read error:", err.Error())
				os.Exit(3)
			}

			retval <- blockData{blobHeader, blobBytes, filePosition}
		}
		close(retval)
	}()

	return retval
}
开发者ID:ctessum,项目名称:osmfilter,代码行数:29,代码来源:pbfFileFormat.go

示例11: NewSlowLogParser

func NewSlowLogParser(file *os.File, stopChan <-chan bool, opt Options) *SlowLogParser {
	// Seek to the offset, if any.
	// @todo error if start off > file size
	if opt.StartOffset > 0 {
		// @todo handle error
		file.Seek(int64(opt.StartOffset), os.SEEK_SET)
	}

	if opt.Debug {
		l.SetFlags(l.Ltime | l.Lmicroseconds)
		fmt.Println()
		l.Println("parsing " + file.Name())
	}

	p := &SlowLogParser{
		stopChan:    stopChan,
		opt:         opt,
		file:        file,
		EventChan:   make(chan *log.Event),
		inHeader:    false,
		inQuery:     false,
		headerLines: 0,
		queryLines:  0,
		bytesRead:   opt.StartOffset,
		lineOffset:  0,
		event:       log.NewEvent(),
	}
	return p
}
开发者ID:charity,项目名称:mysql-log-parser,代码行数:29,代码来源:slow.go

示例12: WriteLobbyInfo

func WriteLobbyInfo(file *os.File, lobby *models.Lobby) {
	if !config.Constants.ChatLogsEnabled {
		return
	}
	file.Seek(0, os.SEEK_SET)
	//TODO: write lobby info to file
}
开发者ID:mcapu,项目名称:Helen,代码行数:7,代码来源:chatLogger.go

示例13: GetProperty

// returns string property prop
func (proc *SProcess) GetProperty(prop string) string {
	var (
		file *os.File
		err  error
	)

	// file exists
	if proc.files[prop] != nil {
		file = proc.files[prop]
		file.Seek(0, 0)

		// doesn't exist; create
	} else {
		file, err = os.Create("/system/process/" + strconv.Itoa(proc.pid) + "/" + prop)
		file.Chmod(0755)
	}

	// read up to 1024 bytes
	b := make([]byte, 1024)
	_, err = file.Read(b)

	// an error occured, and it was not an EOF
	if err != nil && err != io.EOF {
		return "(undefined)"
	}

	// file was more than 1M
	if err != io.EOF {
		return "(maxed out)"
	}

	return string(b)
}
开发者ID:cooper,项目名称:processlib,代码行数:34,代码来源:server.go

示例14: FindAndReplaceFd

func FindAndReplaceFd(fd *os.File, oldPattern, newPattern string) error {
	fbuf, err := ioutil.ReadAll(fd)
	if err != nil {
		return err
	}
	fd.Seek(0, -1)
	fd.Truncate(0)
	expr, err := regexp.Compile(oldPattern)
	if err != nil {
		return err
	}
	buffer := bytes.NewBuffer(fbuf)
	for {
		line, err := buffer.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				break
			}
			return err
		}
		if expr.MatchString(line) {
			line = expr.ReplaceAllString(line, newPattern)
		}
		if _, err := fd.WriteString(line); err != nil {
			return err
		}
	}
	return nil
}
开发者ID:weldpua2008,项目名称:deployer,代码行数:29,代码来源:io.go

示例15: mergeCoverprofile

func mergeCoverprofile(file *os.File, out io.Writer) error {
	_, err := file.Seek(0, 0)
	if err != nil {
		return err
	}

	rd := bufio.NewReader(file)
	_, err = rd.ReadString('\n')
	if err == io.EOF {
		return nil
	}

	if err != nil {
		return err
	}

	_, err = io.Copy(out, rd)
	if err != nil {
		return err
	}

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

	return err
}
开发者ID:marete,项目名称:restic,代码行数:28,代码来源:run_tests.go


注:本文中的os.File.Seek方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。