package volume

import (
    "time"
    "encoding/binary"
    "errors"
    "os"
    "io"
)
//文件基本信息结构体
type FileInfo struct {
    Fid      uint64
    Offset   uint64
    Size     uint64
    Ctime    time.Time
    Mtime    time.Time
    Atime    time.Time
    FileName string
}
//文件信息编码到切片中
func (iv *FileInfo)MarshalBinary() []byte {
    data := make([]byte, 48 + len(iv.FileName))
    binary.BigEndian.PutUint64(data[0:8], iv.Fid)
    binary.BigEndian.PutUint64(data[8:16], iv.Offset)
    binary.BigEndian.PutUint64(data[16:24], iv.Size)
    binary.BigEndian.PutUint64(data[24:32], uint64(iv.Ctime.Unix()))
    binary.BigEndian.PutUint64(data[32:40], uint64(iv.Mtime.Unix()))
    binary.BigEndian.PutUint64(data[40:48], uint64(iv.Atime.Unix()))
    copy(data[48:], []byte(iv.FileName))
    return data
}
//文件信息解码到结构体中
func (iv *FileInfo)UnMarshalBinary(data []byte) (err error) {
    defer func() {
        if r := recover(); r != nil {
            err = r.(error)
        }
    }()

    iv.Fid = binary.BigEndian.Uint64(data[0:8])
    iv.Offset = binary.BigEndian.Uint64(data[8:16])
    iv.Size = binary.BigEndian.Uint64(data[16:24])
    iv.Ctime = time.Unix(int64(binary.BigEndian.Uint64(data[24:32])), 0)
    iv.Mtime = time.Unix(int64(binary.BigEndian.Uint64(data[32:40])), 0)
    iv.Atime = time.Unix(int64(binary.BigEndian.Uint64(data[40:48])), 0)
    iv.FileName = string(data[48:])
    return err
}
//文件结构体
type File struct {
    DataFile *os.File
    Info     *FileInfo
    offset   uint64  //偏移量
}
//读取文件
func (f *File)Read(b []byte) (n int, err error) {
       //计算文件开始读取位置和结束位置
    start := f.Info.Offset + f.offset
    end := f.Info.Offset + f.Info.Size
    length := end - start
    if len(b) > int(length) {
        b = b[:length]
    }

    n, err = f.DataFile.ReadAt(b, int64(start))  //读取文件  从start位置开始 读取b个数据
    f.offset += uint64(n)
    if f.offset >= f.Info.Size {
        err = io.EOF
    }
    return
}
//写文件
func (f *File)Write(b []byte) (n int, err error) {
    start := f.Info.Offset + f.offset
    end := f.Info.Offset + f.Info.Size
    length := end - start
    if len(b) > int(length) {
        //b = b[:length]
        return 0, errors.New("you should create a new File to write")
    }else {
        n, err = f.DataFile.WriteAt(b, int64(start))
        f.offset += uint64(n)
        return
    }
}
//读取文件偏移量
//0意味着相对于文件的原始位置,1意味着相对于当前偏移量,2意味着相对于文件结尾
func (f *File)Seek(offset int64, whence int) (int64, error) {
    switch whence {
    case 0: 
        f.offset = uint64(offset)
    case 1:
        f.offset = uint64(int64(f.offset) + offset)
    case 2:
        f.offset = uint64(int64(f.Info.Size) + offset)
    }
    return int64(f.offset), nil
    //if f.offset > f.Info.Size {
    //    f.offset = 0
    //    return int64(f.offset), errors.New("offset > file.size")
    //}else {
    //    return int64(f.offset), nil
    //}
}

file.go的更多相关文章

  1. 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file

    我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...

  2. HTML中上传与读取图片或文件(input file)----在路上(25)

    input file相关知识简例 在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传.批量上传.删除照片.增加照片.读取图片.对上传的图片或文件的判断,比如限制图片的张数.限 ...

  3. logstash file输入,无输出原因与解决办法

    1.现象 很多同学在用logstash input 为file的时候,经常会出现如下问题:配置文件无误,logstash有时一直停留在等待输入的界面 2.解释 logstash作为日志分析的管道,在实 ...

  4. input[tyle="file"]样式修改及上传文件名显示

    默认的上传样式我们总觉得不太好看,根据需求总想改成和上下结构统一的风格…… 实现方法和思路: 1.在input元素外加a超链接标签 2.给a标签设置按钮样式 3.设置input[type='file' ...

  5. .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍

    1年前,我在文章:这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)中(第9个项目),给大家推荐了一个开源免费的PDF读写组件 PDFSharp,PDFSharp我2年前就看过 ...

  6. [笔记]HAproxy reload config file with uninterrupt session

    HAProxy is a high performance load balancer. It is very light-weight, and free, making it a great op ...

  7. VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%

    1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时,出现以下问题: # odbcexec: "gcc": executabl ...

  8. input type='file'上传控件假样式

    采用bootstrap框架样式 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...

  9. FILE文件流的中fopen、fread、fseek、fclose的使用

    FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...

  10. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

随机推荐

  1. rails小重构:将图片加入产品Model

    原先的产品product模式中存放的是图片的url,必须手动将图片存入指定目录中.现在略作改动,在数据库中新建一个pictures表,其设定如下: class CreatePictures < ...

  2. ruby通过telnet读取互联网时间

    在前面的博文ntp服务器也谈逆向工程中,本猫曾经武断的认为telnet是无法连接到ntp服务器的.因为当时是这样连接的: telnet time.nist.gov 123,端口号123是在/etc/s ...

  3. C# 操作Word 文档——添加Word页眉、页脚和页码

    在Word文档中,我们可以通过添加页眉.页脚的方式来丰富文档内容.添加页眉.页脚时,可以添加时间.日期.文档标题,文档引用信息.页码.内容解释.图片/LOGO等多种图文信息.同时也可根据需要调整文字或 ...

  4. 继续死磕SDRAM控制器

    SDRAM控制器 博主上一篇介绍了一些SDRAM的基本原理是否有必要学习使用纯Verilog写一个SDRAM控制器,接下来记录SDRAM控制器的工作原理.首先是上电初始化. 上电初始化 时序图中,tR ...

  5. 如何卸载Centos自带jdk

    1.搜索安装的jdk: rpm -qa|grep jdk 结果如下: java-1.7.0-openjdk-1.7.0.45-2.4.3.3.el6.x86_64 java-1.6.0-openjdk ...

  6. Python高阶函数之 - 装饰器

    高阶函数:  1. 函数名可以作为参数传入     2. 函数名可以作为返回值. python装饰器是用于拓展原来函数功能的一种函数 , 这个函数的特殊之处在于它的返回值也是一个函数 , 使用pyth ...

  7. add-apt-repository出Exception问题

    参考:http://blog.sina.com.cn/s/blog_5388923c0100nu8h.html 症状: xxxx@xxxxx:~$ sudo add-apt-repository pp ...

  8. js对象属性值为对象形式取值方式

    console.log(rowData);//取带点的属性值 console.log(rowData['layoutPipegallery.pipegallerycode']);//取带点的属性值

  9. linux内核中断之看门狗

    一:内核中断 linux内核中的看门狗中断跟之前的裸板的中断差不多,在编写驱动之前,需要线把内核自带的watch dog模块裁剪掉,要不然会出现错误:在Device Drivers /Watchdog ...

  10. 了解mysqlpump工具

    Ⅰ.功能分析 1.1 多线程介绍 mysqlpump是MySQL5.7的官方工具,用于取代mysqldump,其参数与mysqldump基本一样 mysqlpump是多线程备份,但只能到表级别,单表备 ...