當前位置: 首頁>>代碼示例>>Golang>>正文


Golang File.Stat方法代碼示例

本文整理匯總了Golang中os.File.Stat方法的典型用法代碼示例。如果您正苦於以下問題:Golang File.Stat方法的具體用法?Golang File.Stat怎麽用?Golang File.Stat使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在os.File的用法示例。


在下文中一共展示了File.Stat方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: replicationSlaveFullSync

// if full sync ok, return sync offset for later backlog syncing
func (h *Handler) replicationSlaveFullSync(c *conn) (syncOffset int64, resp redis.Resp, err error) {
	// after bgsave, we must send this RDB to slave,
	// so we don't allow others do bgsave before full sync done.
	if ok := h.bgSaveSem.AcquireTimeout(time.Minute); !ok {
		resp, err = toRespErrorf("wait others do bgsave timeout")
		return
	}
	defer h.bgSaveSem.Release()

	// now begin full sync
	h.counters.syncFull.Add(1)

	var rdb *os.File
	rdb, syncOffset, err = h.replicationBgSave()
	if err != nil {
		resp, err = toRespError(err)
		return
	}
	defer rdb.Close()

	// send rdb to slave
	st, _ := rdb.Stat()

	rdbSize := st.Size()

	if err = c.writeRDBFrom(rdbSize, rdb); err != nil {
		// close this connection here???
		log.Errorf("slave %s sync rdb err - %s", c, err)
		c.Close()
		return
	}

	return syncOffset, nil, nil
}
開發者ID:CowLeo,項目名稱:qdb,代碼行數:35,代碼來源:repl.go

示例2: OpenFileOrStdDev

func OpenFileOrStdDev(path string, write bool) *os.File {
	var fp *os.File
	var err error

	if path == "stdin" {
		fp = os.Stdin
		err = nil
	} else if path == "stdout" {
		fp = os.Stdout
		err = nil
	} else {
		if write {
			fp, err = os.Create(CleanPath(path))
		} else {
			fp, err = os.Open(CleanPath(path))
		}
	}

	if err != nil {
		log.Fatalln(err)
	}

	stat, statErr := fp.Stat()

	if statErr != nil {
		log.Fatalln(err)
	}

	if stat.IsDir() {
		log.Fatalf("%s: is a directory\n", path)
	}

	return fp
}
開發者ID:thacker99,項目名稱:textql,代碼行數:34,代碼來源:file_helpers.go

示例3: UploadReleaseAsset

// UploadReleaseAsset creates an asset by uploading a file into a release repository.
// To upload assets that cannot be represented by an os.File, call NewUploadRequest directly.
//
// GitHub API docs : http://developer.github.com/v3/repos/releases/#upload-a-release-asset
func (s *RepositoriesService) UploadReleaseAsset(owner, repo string, id int, opt *UploadOptions, file *os.File) (*ReleaseAsset, *Response, error) {
	u := fmt.Sprintf("repos/%s/%s/releases/%d/assets", owner, repo, id)
	u, err := addOptions(u, opt)
	if err != nil {
		return nil, nil, err
	}

	stat, err := file.Stat()
	if err != nil {
		return nil, nil, err
	}
	if stat.IsDir() {
		return nil, nil, errors.New("the asset to upload can't be a directory")
	}

	mediaType := mime.TypeByExtension(filepath.Ext(file.Name()))
	req, err := s.client.NewUploadRequest(u, file, stat.Size(), mediaType)
	if err != nil {
		return nil, nil, err
	}

	asset := new(ReleaseAsset)
	resp, err := s.client.Do(req, asset)
	if err != nil {
		return nil, resp, err
	}
	return asset, resp, err
}
開發者ID:quixoten,項目名稱:vault,代碼行數:32,代碼來源:repos_releases.go

示例4: main

func main() {
	var APIOptions = oss.GetDefaultAPIOptioins()
	APIOptions.AccessID = AccessKeyID
	APIOptions.SecretAccessKey = AccessKeySecret
	var OSSAPI, err = oss.NewAPI(APIOptions)

	if err != nil {
		log.Fatal(err)
	}

	var bucket = "ossgosdklargefile"
	var object = "largefile.bin"
	var bufSize = int64(1024 * 1024 * 2) // 2 M
	var file = "test.bin"
	var fp *os.File

	if err = OSSAPI.PutBucket(bucket, oss.ACLPublicReadWrite, "", nil); err != nil {
		log.Printf("%s\n", err)
	}

	var multi *oss.MultipartUpload
	if multi, err = OSSAPI.NewMultipartUpload(bucket, object, nil); err != nil {
		log.Printf("%s\n", err)
	}

	if fp, err = os.Open(file); err != nil {
		log.Fatal(err)
	}

	var stat, _ = fp.Stat()
	var fileLength = stat.Size()
	var filePart = int(fileLength / bufSize)
	if int64(filePart)*bufSize < fileLength {
		filePart = filePart + 1
	}

	var rd io.Reader
	var parts = make([]oss.Part, filePart)
	var etag string

	for i := 1; i <= filePart; i++ {
		rd = io.LimitReader(fp, bufSize)
		if etag, err = multi.UploadPart(i, rd); err != nil {
			log.Printf("%s\n", err)
		}
		log.Printf("PartNumber: %d, ETag: %s\n", i, etag)
		parts[i-1] = oss.Part{
			PartNumber: i,
			ETag:       etag,
		}
	}

	var result oss.CompleteMultipartUploadResult

	if err = multi.CompleteUpload(parts, &result); err != nil {
		log.Printf("%s\n", err)
	}

	log.Printf("Upload result: %s\n", result)
}
開發者ID:Lupino,項目名稱:oss-go-sdk,代碼行數:60,代碼來源:main.go

示例5: getSize

func getSize(f *os.File) (int64, error) {
	fi, err := f.Stat()
	if err != nil {
		return 0, err
	}
	return fi.Size(), nil
}
開發者ID:Ramzec,項目名稱:lxd,代碼行數:7,代碼來源:images.go

示例6: parse

//----------------------------------------------- parse & load a game data file into dictionary
func parse(file *os.File) {
	// csv 讀取器
	csv_reader := csv.NewReader(file)
	records, err := csv_reader.ReadAll()
	if err != nil {
		ERR("cannot parse csv file.", file.Name(), err)
		return
	}

	// 是否為空檔
	if len(records) == 0 {
		ERR("csv file is empty", file.Name())
		return
	}

	// 處理表名
	fi, err := file.Stat()
	if err != nil {
		ERR("cannot stat the file", file.Name())
		return
	}
	tblname := strings.TrimSuffix(fi.Name(), path.Ext(file.Name()))

	// 記錄數據, 第一行為表頭,因此從第二行開始
	for line := 1; line < len(records); line++ {
		for field := 1; field < len(records[line]); field++ { // 每條記錄的第一個字段作為行索引
			_set(tblname, records[line][0], records[0][field], records[line][field])
		}
	}
}
開發者ID:chenbk85,項目名稱:gonet,代碼行數:31,代碼來源:parser.go

示例7: GetFileSize

func GetFileSize(file *os.File) (size int64, err error) {
	var fi os.FileInfo
	if fi, err = file.Stat(); err == nil {
		size = fi.Size()
	}
	return
}
開發者ID:ham1,項目名稱:seaweedfs,代碼行數:7,代碼來源:file_util.go

示例8: NewBufferFile

// NewBufferFile maps a file to shared memory and returns a handle to the shared memory buffer
func NewBufferFile(file *os.File, size, prot int) (Buffer, error) {
	fi, err := file.Stat()
	if err != nil {
		return nil, err
	}
	sys := fi.Sys().(*syscall.Stat_t)
	if sys.Size != int64(size) {
		return nil, errWrongSize
	}

	// Dup to allow file parameter to be closed regardless
	fd, err := syscall.Dup(int(file.Fd()))
	if err != nil {
		return nil, err
	}
	const flags = syscall.MAP_SHARED
	b, err := syscall.Mmap(fd, 0, size, prot, flags)
	if err != nil {
		return nil, err
	}

	// localFile is nil because fd is from somewhere else
	buf := &sharedBuffer{os.NewFile(uintptr(fd), ""), nil, b, stackToKeep()}
	runtime.SetFinalizer(buf, (*sharedBuffer).finalize)

	return buf, nil
}
開發者ID:jsgilmore,項目名稱:shm,代碼行數:28,代碼來源:shm.go

示例9: tarCp

func (g *GraphTool) tarCp(srcName string, tw *tar.Writer) (int64, error) {
	var (
		src *os.File
		err error
	)

	if src, err = os.Open(srcName); err != nil {
		return 0, err
	}
	defer src.Close()

	srcStat, err := src.Stat()
	if err != nil {
		g.logger.Error(err.Error())
	} else if err := unix.Fadvise(int(src.Fd()), 0, srcStat.Size(), unix.MADV_SEQUENTIAL); err != nil {
		g.logger.Error(err.Error())
	}

	if n, err := io.Copy(tw, src); err != nil {
		g.logger.Error(err.Error())
	} else {
		return n, nil
	}

	return 0, nil
}
開發者ID:rsampaio,項目名稱:docker-graphtool,代碼行數:26,代碼來源:bundle.go

示例10: isTTY

func isTTY(file *os.File) bool {
	stat, err := file.Stat()
	if err != nil {
		log.Fatal(err)
	}
	return ((stat.Mode() & os.ModeCharDevice) != 0)
}
開發者ID:justincampbell,項目名稱:plop,代碼行數:7,代碼來源:main.go

示例11: customReader

func customReader() ([]byte, int) {
	var file *os.File
	var err error

	if section == nil {
		file, err = os.Open(inputSampleFilename)
		if err != nil {
			panic(err)
		}

		fi, err := file.Stat()
		if err != nil {
			panic(err)
		}

		section = io.NewSectionReader(file, 0, fi.Size())
	}

	b := make([]byte, IO_BUFFER_SIZE)

	n, err := section.Read(b)
	if err != nil {
		fmt.Println("section.Read():", err)
		file.Close()
	}

	return b, n
}
開發者ID:davidoram,項目名稱:gmf,代碼行數:28,代碼來源:format_test.go

示例12: ReadFile

func ReadFile(name string) (b []byte, e os.Error) {
	var (
		raw  []byte
		err  os.Error
		file *os.File
		dir  *os.Dir
	)

	file, err = os.Open(name, os.O_RDONLY, 0)
	if err != nil {
		return raw, err
	}

	dir, err = file.Stat()
	if err != nil {
		return raw, err
	}

	var size uint64 = dir.Size
	raw = make([]byte, size)
	_, err = file.Read(raw)
	if err != nil {
		return raw, err
	}

	return raw, err
}
開發者ID:michaelstorm,項目名稱:Gofunge-93,代碼行數:27,代碼來源:g93.go

示例13: StoreBlob

func (c *Conn) StoreBlob(file *os.File) (string, error) {
	reader := hashed.NewSha1FileReader(file)

	info, err := file.Stat()
	if err != nil {
		return "", err
	}
	size := info.Size()

	if err := c.WriteJSONFrame(request{"store", "", size, false}); err != nil {
		return "", err
	}

	if err := c.WriteBlobFrameFrom(reader, size); err != nil {
		return "", err
	}

	key := util.HexHash(reader)
	if err := c.WriteJSONFrame(request{Key: key}); err != nil {
		return "", err
	}

	var res response
	if err := c.ReadJSONFrame(&res); err != nil {
		return "", err
	}

	if res.Val != "ok" {
		return "", responseError(res)
	}

	return key, nil
}
開發者ID:jimenezrick,項目名稱:crap,代碼行數:33,代碼來源:conn.go

示例14: LoadNeedleMap

func LoadNeedleMap(file *os.File) *NeedleMap {
	nm := NewNeedleMap(file)
	bytes := make([]byte, 16*RowsToRead)
	count, e := nm.indexFile.Read(bytes)
	if count > 0 {
		fstat, _ := file.Stat()
		log.Println("Loading index file", fstat.Name(), "size", fstat.Size())
	}
	for count > 0 && e == nil {
		for i := 0; i < count; i += 16 {
			key := util.BytesToUint64(bytes[i : i+8])
			offset := util.BytesToUint32(bytes[i+8 : i+12])
			size := util.BytesToUint32(bytes[i+12 : i+16])
			if offset > 0 {
				nm.m.Set(Key(key), offset, size)
				//log.Println("reading key", key, "offset", offset, "size", size)
				nm.fileCounter++
			} else {
				nm.m.Delete(Key(key))
				//log.Println("removing key", key)
				nm.deletionCounter++
			}
		}

		count, e = nm.indexFile.Read(bytes)
	}
	return nm
}
開發者ID:wangjinlei,項目名稱:weedfs,代碼行數:28,代碼來源:needle_map.go

示例15: RawToFixed

func RawToFixed(f *os.File, options *VHDOptions) {
	info, err := f.Stat()
	check(err)
	size := uint64(info.Size())
	header := CreateFixedHeader(size, options)
	binary.Write(f, binary.BigEndian, header)
}
開發者ID:eljefedelrodeodeljefe,項目名稱:kubernetes,代碼行數:7,代碼來源:vhd.go


注:本文中的os.File.Stat方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。