本文整理汇总了Golang中os.FileInfo.Size方法的典型用法代码示例。如果您正苦于以下问题:Golang FileInfo.Size方法的具体用法?Golang FileInfo.Size怎么用?Golang FileInfo.Size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os.FileInfo
的用法示例。
在下文中一共展示了FileInfo.Size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getReaderSize
// getReaderSize gets the size of the underlying reader, if possible.
func getReaderSize(reader io.Reader) (size int64, err error) {
size = -1
if reader != nil {
switch v := reader.(type) {
case *bytes.Buffer:
size = int64(v.Len())
case *bytes.Reader:
size = int64(v.Len())
case *strings.Reader:
size = int64(v.Len())
case *os.File:
var st os.FileInfo
st, err = v.Stat()
if err != nil {
return 0, err
}
size = st.Size()
case *Object:
var st ObjectInfo
st, err = v.Stat()
if err != nil {
return 0, err
}
size = st.Size
}
}
return size, nil
}
示例2: Stat
// Stat produces a styxproto.Stat from an open file. If the value
// provides a Stat method matching that of os.File, that is used.
// Otherwise, the styxfile package determines the file's attributes
// based on other characteristics.
func Stat(buf []byte, file Interface, name string, qid styxproto.Qid) (styxproto.Stat, error) {
var (
fi os.FileInfo
err error
)
type hasStat interface {
Stat() (os.FileInfo, error)
}
if v, ok := file.(hasStat); ok {
fi, err = v.Stat()
if err != nil {
return nil, err
}
} else {
fi = statGuess{file, name, qid.Type()}
}
uid, gid, muid := sys.FileOwner(fi)
stat, _, err := styxproto.NewStat(buf, fi.Name(), uid, gid, muid)
if err != nil {
return nil, err
}
stat.SetLength(fi.Size())
stat.SetMode(Mode9P(fi.Mode()))
stat.SetAtime(uint32(fi.ModTime().Unix()))
stat.SetMtime(uint32(fi.ModTime().Unix()))
stat.SetQid(qid)
return stat, nil
}
示例3: dir2Dir
func dir2Dir(path string, d os.FileInfo, dotu bool, upool p.Users) *p.Dir {
sysMode := d.Sys().(*syscall.Stat_t)
dir := new(Dir)
dir.Qid = *dir2Qid(d)
dir.Mode = dir2Npmode(d, dotu)
dir.Atime = uint32(atime(sysMode).Unix())
dir.Mtime = uint32(d.ModTime().Unix())
dir.Length = uint64(d.Size())
dir.Name = path[strings.LastIndex(path, "/")+1:]
if dotu {
dir.dotu(path, d, upool, sysMode)
return &dir.Dir
}
unixUid := int(sysMode.Uid)
unixGid := int(sysMode.Gid)
dir.Uid = strconv.Itoa(unixUid)
dir.Gid = strconv.Itoa(unixGid)
dir.Muid = "none"
// BUG(akumar): LookupId will never find names for
// groups, as it only operates on user ids.
u, err := user.LookupId(dir.Uid)
if err == nil {
dir.Uid = u.Username
}
g, err := user.LookupId(dir.Gid)
if err == nil {
dir.Gid = g.Username
}
return &dir.Dir
}
示例4: LoadConfig
func LoadConfig(fname string) (cfg Config, err error) {
var fd *os.File
var fs os.FileInfo
var data []byte
// Check if the config file exists, and create a buffer to hold it's content
if fs, err = os.Stat(fname); err != nil {
return
}
data = make([]byte, fs.Size())
// Open and read the file into the buffer
if fd, err = os.Open(fname); err != nil {
return
}
defer fd.Close()
if _, err = fd.Read(data); err != nil {
return
}
// Parse the yaml into a struct
cfg = Config{}
err = yaml.Unmarshal(data, &cfg)
if err != nil {
return
}
// Set the hostname
cfg.Hostname, _ = os.Hostname()
return
}
示例5: StitchWalk
func StitchWalk(path string, info os.FileInfo, err error) error {
fmt.Println(path, info, err)
if info.IsDir() {
fmt.Println("Is a dir")
} else {
/*
f0, err0 := os.Open(path)
if err0 != nil {
return err0
}
*/
fmt.Printf("writing %s. %v bytes", path, info.Size())
b0, err0 := ioutil.ReadFile(path)
if err0 != nil {
return err0
}
s0 := string(b0)
// s1 := fmt.Sprintf("%s,\n", s0)
s1 := info.Name()
s1 = s1[:len(s1)-5]
s2 := fmt.Sprintf("%s:%s,\n", s1, s0)
W.WriteString(s2)
}
return err
}
示例6: OnlyDir_TrailingSlash
func (t *GcsfuseTest) OnlyDir_TrailingSlash() {
var err error
var fi os.FileInfo
// Mount only a single directory from the bucket, including a trailing slash.
args := []string{
"--only-dir",
path.Dir(canned.ExplicitDirFile) + "/",
canned.FakeBucketName,
t.dir,
}
err = t.runGcsfuse(args)
AssertEq(nil, err)
defer unmount(t.dir)
// It should be as if t.dir points into the bucket's first-level directory.
entries, err := fusetesting.ReadDirPicky(t.dir)
AssertEq(nil, err)
AssertEq(1, len(entries))
fi = entries[0]
ExpectEq(path.Base(canned.ExplicitDirFile), fi.Name())
ExpectEq(len(canned.ExplicitDirFile_Contents), fi.Size())
}
示例7: acquireNewBlock
func (store *fileStore) acquireNewBlock() (Id, []byte, error) {
var (
err error
fileInfo os.FileInfo
id Id
memSlice []byte
)
if store.free.Len() > 0 {
store.freeMtx.Lock()
el := store.free.Front()
store.free.Remove(el)
store.freeMtx.Unlock()
id = el.Value.(Id)
} else {
store.fdMtx.Lock()
if fileInfo, err = store.fd.Stat(); err != nil {
return Id(0), nil, err
}
id = Id(fileInfo.Size() / blockSize)
store.fd.Truncate(fileInfo.Size() + blockSize)
store.fdMtx.Unlock()
}
memSlice, err = store.acquireBlock(id)
return id, memSlice, err
}
示例8: addFiles
func (m *MetaInfo) addFiles(fs MetaInfoFileSystem, file string) (err error) {
var fileInfo os.FileInfo
fileInfo, err = fs.Stat(file)
if err != nil {
return
}
if fileInfo.IsDir() {
var f MetaInfoFile
f, err = fs.Open(file)
if err != nil {
return
}
var fi []string
fi, err = f.Readdirnames(0)
if err != nil {
return
}
for _, name := range fi {
err = m.addFiles(fs, path.Join(file, name))
if err != nil {
return
}
}
} else {
fileDict := FileDict{Length: fileInfo.Size()}
cleanFile := path.Clean(file)
parts := strings.Split(cleanFile, string(os.PathSeparator))
fileDict.Path = parts
m.Info.Files = append(m.Info.Files, fileDict)
}
return
}
示例9: SymlinksWork
func (t *CachingWithImplicitDirsTest) SymlinksWork() {
var fi os.FileInfo
var err error
// Create a file.
fileName := path.Join(t.Dir, "foo")
const contents = "taco"
err = ioutil.WriteFile(fileName, []byte(contents), 0400)
AssertEq(nil, err)
// Create a symlink to it.
symlinkName := path.Join(t.Dir, "bar")
err = os.Symlink("foo", symlinkName)
AssertEq(nil, err)
// Stat the link.
fi, err = os.Lstat(symlinkName)
AssertEq(nil, err)
ExpectEq("bar", fi.Name())
ExpectEq(0, fi.Size())
ExpectEq(filePerms|os.ModeSymlink, fi.Mode())
// Stat the target via the link.
fi, err = os.Stat(symlinkName)
AssertEq(nil, err)
ExpectEq("bar", fi.Name())
ExpectEq(len(contents), fi.Size())
ExpectEq(filePerms, fi.Mode())
}
示例10: watch
// watch watchers the file for changes
func (d *FileQuery) watch(lastStat os.FileInfo) <-chan *watchResult {
ch := make(chan *watchResult, 1)
go func(lastStat os.FileInfo) {
for {
stat, err := os.Stat(d.path)
if err != nil {
select {
case <-d.stopCh:
return
case ch <- &watchResult{err: err}:
return
}
}
changed := lastStat == nil ||
lastStat.Size() != stat.Size() ||
lastStat.ModTime() != stat.ModTime()
if changed {
select {
case <-d.stopCh:
return
case ch <- &watchResult{stat: stat}:
return
}
}
time.Sleep(FileQuerySleepTime)
}
}(lastStat)
return ch
}
示例11: fillLocalfsImage
func (repo *LocalfsImagerep) fillLocalfsImage(image *models.Image, fileinfo os.FileInfo) bool {
// ubuntu-14.04_x86_64_raw.img -> name: ubuntu-14.04, arch: x86_64, type: raw.img
imginfo := strings.SplitN(fileinfo.Name(), "_", 3)
if len(imginfo) != 3 {
log.WithField("image", fileinfo.Name()).Info("skipping image with invalid name")
return false
}
image.Name = imginfo[0]
image.Size = fileinfo.Size()
image.Date = fileinfo.ModTime()
image.Filename = fileinfo.Name()
image.FullPath = filepath.Join(repo.Root, fileinfo.Name())
switch imginfo[1] {
default:
log.WithField("filename", fileinfo.Name()).WithField("parts", imginfo).Info("skipping unknown image architecture")
return false
case "amd64":
image.Arch = models.IMAGE_ARCH_X86_64
case "i386":
image.Arch = models.IMAGE_ARCH_X86
}
switch imginfo[2] {
default:
log.WithField("filename", fileinfo.Name()).WithField("parts", imginfo).Info("skipping unknown image type")
return false
case "raw.img":
image.Type = models.IMAGE_FMT_RAW
case "qcow2.img":
image.Type = models.IMAGE_FMT_QCOW2
}
return true
}
示例12: visit
func (gw *gameWalker) visit(path string, f os.FileInfo, err error) error {
if f == nil || f.Name() == ".DS_Store" {
return nil
}
if f.IsDir() {
return nil
}
hh, err := HashesForFile(path)
if err != nil {
return err
}
romName, err := filepath.Rel(gw.gamepath, path)
if err != nil {
return err
}
rom := new(types.Rom)
rom.Name = romName
rom.Size = f.Size()
rom.Crc = hh.Crc
rom.Md5 = hh.Md5
rom.Sha1 = hh.Sha1
gw.game.Roms = append(gw.game.Roms, rom)
return nil
}
示例13: Finfo2Theader
// create tar.Header from os.FileInfo
func Finfo2Theader(fi os.FileInfo) (hdr *tar.Header, err error) {
m := fi.Mode()
var (
ln string
tm byte
)
tm = tar.TypeReg
switch {
case m&os.ModeSymlink != 0:
tm = tar.TypeSymlink
/*if lfi, err := os.Lstat(fi.Name()); err == nil {
ln = lfi.Name()
}*/
case m&os.ModeDevice != 0 && m&os.ModeCharDevice != 0:
tm = tar.TypeChar
case m&os.ModeDevice != 0:
tm = tar.TypeBlock
case m&os.ModeNamedPipe != 0 || m&os.ModeSocket != 0:
tm = tar.TypeFifo
}
tim := fi.ModTime()
hdr = &tar.Header{Name: fi.Name(), Mode: int64(m.Perm()),
Size: fi.Size(), ModTime: tim,
Typeflag: tm, Linkname: ln}
FillHeader(hdr)
return
}
示例14: fileEndsWithString
func fileEndsWithString(path string, info os.FileInfo, str string, caseSensitive bool) bool {
f, err := os.Open(path)
if err != nil {
return false
}
defer f.Close()
bsize := len(str)
buff := make([]byte, bsize)
cbuff := make([]byte, 1)
j := bsize - 1
for i := info.Size() - 1; i >= 0 && j >= 0; i-- {
_, readErr := f.ReadAt(cbuff, i)
if readErr != nil {
return false
}
if cbuff[0] >= 32 || j < bsize-1 {
buff[j] = cbuff[0]
j--
}
}
endValue := string(buff)
if !caseSensitive {
endValue = strings.ToLower(endValue)
}
return str == endValue
}
示例15: NewIndexer
// NewIndexer new a indexer for async merge index data to disk.
func NewIndexer(file string, options Options) (i *Indexer, err error) {
var stat os.FileInfo
i = &Indexer{}
i.File = file
i.closed = false
i.syncOffset = 0
i.Options = options
i.ring = NewRing(options.RingBuffer)
i.bn = 0
i.buf = make([]byte, options.BufferSize)
if i.f, err = os.OpenFile(file, os.O_RDWR|os.O_CREATE|myos.O_NOATIME, 0664); err != nil {
log.Errorf("os.OpenFile(\"%s\") error(%v)", file, err)
return nil, err
}
if stat, err = i.f.Stat(); err != nil {
log.Errorf("index: %s Stat() error(%v)", i.File, err)
return nil, err
}
if stat.Size() == 0 {
if err = myos.Fallocate(i.f.Fd(), myos.FALLOC_FL_KEEP_SIZE, 0, fallocSize); err != nil {
log.Errorf("index: %s fallocate() error(err)", i.File, err)
i.Close()
return nil, err
}
}
i.wg.Add(1)
i.signal = make(chan int, 1)
go i.merge()
return
}