ipfs add 源码分析

add 指令 上传一个文件或目录

USAGE
  ipfs add <path>... - 添加目录或文件

可选参数
  ipfs add [--recursive | -r] [--quiet | -q] [--quieter | -Q] [--silent] [--progress | -p] [--trickle | -t] [--only-hash | -n] [--wrap-with-directory | -w] [--hidden | -H] [--chunker=<chunker> | -s] [--pin=false] [--raw-leaves] [--nocopy] [--fscache] [--] <path>...

ARGUMENTS

  <path>... - 文件路径

OPTION

  -r,         --recursive           bool   - 递归添加目录内容 Default: false.
  -q,         --quiet               bool   - 安静模式,执行过程中输出显示尽可能少的信息
  -Q,         --quieter             bool   - 更安静模式,仅输出最终的结果哈希值
  --silent                          bool   - 静默模式,不输出任何信息.
  -p,         --progress            bool   - 流式输出过程数据.
  -t,         --trickle             bool   - 使用trickle-dag格式进行有向图生成.
  -n,         --only-hash           bool   - 只计算hash,不写入内容到ipfs
  -w,         --wrap-with-directory bool   - 使用目录对象包装文件
  -H,         --hidden              bool   - 包含隐藏文件,仅在进行递归添加时有效
  -s,         --chunker             string - 使用的分块算法.
  --pin                             bool   - 添加时固定对象,默认值:true
  --raw-leaves                      bool   - 叶节点使用裸块. (experimental).
  --nocopy                          bool   - 使用filestore添加文件. (experimental).
  --fscache                         bool   - 为已有块检查filestore. (experimental).

DESCRIPTION

  Adds contents of <path> to ipfs. Use -r to add directories.
  Note that directories are added recursively, to form the ipfs
  MerkleDAG.

  The wrap option, '-w', wraps the file (or files, if using the
  recursive option) in a directory. This directory contains only
  the files which have been added, and means that the file retains
  its filename. For example:

    > ipfs add example.jpg
    added QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH example.jpg
    > ipfs add example.jpg -w
    added QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH example.jpg
    added QmaG4FuMqEBnQNn3C8XJ5bpW8kLs7zq2ZXgHptJHbKDDVx

  You can now refer to the added file in a gateway, like so:

 /ipfs/QmaG4FuMqEBnQNn3C8XJ5bpW8kLs7zq2ZXgHptJHbKDDVx/example.jpg

代码逻辑

 1. 获取节点、节目配置信息
 2.  初始化fileAdder
# 错误提示  超过ipfs的深度限制
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")

const (
    quietOptionName       = "quiet"
    quieterOptionName     = "quieter"
    silentOptionName      = "silent"
    progressOptionName    = "progress"
    trickleOptionName     = "trickle"
    wrapOptionName        = "wrap-with-directory"
    hiddenOptionName      = "hidden"
    onlyHashOptionName    = "only-hash"
    chunkerOptionName     = "chunker"
    pinOptionName         = "pin"
    rawLeavesOptionName   = "raw-leaves"
    noCopyOptionName      = "nocopy"
    fstoreCacheOptionName = "fscache"
    cidVersionOptionName  = "cid-version"
    hashOptionName        = "hash"
)

# 阻塞管道
const adderOutChanSize = 8

# 增加命令
var AddCmd = &cmds.Command{
        # 命令提示
    Helptext: cmdkit.HelpText{
        Tagline: "Add a file or directory to ipfs.",
        ShortDescription: `
Adds contents of <path> to ipfs. Use -r to add directories (recursively).
`,
        LongDescription: `
Adds contents of <path> to ipfs. Use -r to add directories.
Note that directories are added recursively, to form the ipfs
MerkleDAG.

The wrap option, '-w', wraps the file (or files, if using the
recursive option) in a directory. This directory contains only
the files which have been added, and means that the file retains
its filename. For example:

  > ipfs add example.jpg
  added QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH example.jpg
  > ipfs add example.jpg -w
  added QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH example.jpg
  added QmaG4FuMqEBnQNn3C8XJ5bpW8kLs7zq2ZXgHptJHbKDDVx

You can now refer to the added file in a gateway, like so:

  /ipfs/QmaG4FuMqEBnQNn3C8XJ5bpW8kLs7zq2ZXgHptJHbKDDVx/example.jpg

The chunker option, '-s', specifies the chunking strategy that dictates
how to break files into blocks. Blocks with same content can
be deduplicated. The default is a fixed block size of
256 * 1024 bytes, 'size-262144'. Alternatively, you can use the
rabin chunker for content defined chunking by specifying
rabin-[min]-[avg]-[max] (where min/avg/max refer to the resulting
chunk sizes). Using other chunking strategies will produce
different hashes for the same file.

  > ipfs add --chunker=size-2048 ipfs-logo.svg
  added QmafrLBfzRLV4XSH1XcaMMeaXEUhDJjmtDfsYU95TrWG87 ipfs-logo.svg
  > ipfs add --chunker=rabin-512-1024-2048 ipfs-logo.svg
  added Qmf1hDN65tR55Ubh2RN1FPxr69xq3giVBz1KApsresY8Gn ipfs-logo.svg

You can now check what blocks have been created by:

  > ipfs object links QmafrLBfzRLV4XSH1XcaMMeaXEUhDJjmtDfsYU95TrWG87
  QmY6yj1GsermExDXoosVE3aSPxdMNYr6aKuw3nA8LoWPRS 2059
  Qmf7ZQeSxq2fJVJbCmgTrLLVN9tDR9Wy5k75DxQKuz5Gyt 1195
  > ipfs object links Qmf1hDN65tR55Ubh2RN1FPxr69xq3giVBz1KApsresY8Gn
  QmY6yj1GsermExDXoosVE3aSPxdMNYr6aKuw3nA8LoWPRS 2059
  QmerURi9k4XzKCaaPbsK6BL5pMEjF7PGphjDvkkjDtsVf3 868
  QmQB28iwSriSUSMqG2nXDTLtdPHgWb4rebBrU7Q1j4vxPv 338
`,
    },
        # 命令参数
    Arguments: []cmdkit.Argument{
        cmdkit.FileArg("path", true, true, "The path to a file to be added to ipfs.").EnableRecursive().EnableStdin(),
    }, 
      # 可选参数
    Options: []cmdkit.Option{
        cmds.OptionRecursivePath, // a builtin option that allows recursive paths (-r, --recursive)
        cmdkit.BoolOption(quietOptionName, "q", "Write minimal output."),
        cmdkit.BoolOption(quieterOptionName, "Q", "Write only final hash."),
        cmdkit.BoolOption(silentOptionName, "Write no output."),
        cmdkit.BoolOption(progressOptionName, "p", "Stream progress data."),
        cmdkit.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation."),
        cmdkit.BoolOption(onlyHashOptionName, "n", "Only chunk and hash - do not write to disk."),
        cmdkit.BoolOption(wrapOptionName, "w", "Wrap files with a directory object."),
        cmdkit.BoolOption(hiddenOptionName, "H", "Include files that are hidden. Only takes effect on recursive add."),
        cmdkit.StringOption(chunkerOptionName, "s", "Chunking algorithm, size-[bytes] or rabin-[min]-[avg]-[max]").WithDefault("size-262144"),
        cmdkit.BoolOption(pinOptionName, "Pin this object when adding.").WithDefault(true),
        cmdkit.BoolOption(rawLeavesOptionName, "Use raw blocks for leaf nodes. (experimental)"),
        cmdkit.BoolOption(noCopyOptionName, "Add the file using filestore. Implies raw-leaves. (experimental)"),
        cmdkit.BoolOption(fstoreCacheOptionName, "Check the filestore for pre-existing blocks. (experimental)"),
        cmdkit.IntOption(cidVersionOptionName, "CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. (experimental)"),
        cmdkit.StringOption(hashOptionName, "Hash function to use. Implies CIDv1 if not sha2-256. (experimental)").WithDefault("sha2-256"),
    },
       # 执行前的参数设定
    PreRun: func(req *cmds.Request, env cmds.Environment) error {
        quiet, _ := req.Options[quietOptionName].(bool)
        quieter, _ := req.Options[quieterOptionName].(bool)
        quiet = quiet || quieter

        silent, _ := req.Options[silentOptionName].(bool)

        if quiet || silent {
            return nil
        }

        // ipfs cli progress bar defaults to true unless quiet or silent is used
        _, found := req.Options[progressOptionName].(bool)
        if !found {
            req.Options[progressOptionName] = true
        }

        return nil
    },
        # 执行命令
    Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
                # 获取节点信息 在request context信息里获取
        n, err := GetNode(env)
        if err != nil {
            res.SetError(err, cmdkit.ErrNormal)
            return
        }
              # 获取节点配置信息
        cfg, err := n.Repo.Config()
        if err != nil {
            res.SetError(err, cmdkit.ErrNormal)
            return
        }
        // check if repo will exceed storage limit if added
        // TODO: this doesn't handle the case if the hashed file is already in blocks (deduplicated)
        // TODO: conditional GC is disabled due to it is somehow not possible to pass the size to the daemon
        //if err := corerepo.ConditionalGC(req.Context(), n, uint64(size)); err != nil {
        //  res.SetError(err, cmdkit.ErrNormal)
        //  return
        //}

        progress, _ := req.Options[progressOptionName].(bool)
        trickle, _ := req.Options[trickleOptionName].(bool)
        wrap, _ := req.Options[wrapOptionName].(bool)
        hash, _ := req.Options[onlyHashOptionName].(bool)
        hidden, _ := req.Options[hiddenOptionName].(bool)
        silent, _ := req.Options[silentOptionName].(bool)
        chunker, _ := req.Options[chunkerOptionName].(string)
        dopin, _ := req.Options[pinOptionName].(bool)
        rawblks, rbset := req.Options[rawLeavesOptionName].(bool)
        nocopy, _ := req.Options[noCopyOptionName].(bool)
        fscache, _ := req.Options[fstoreCacheOptionName].(bool)
        cidVer, cidVerSet := req.Options[cidVersionOptionName].(int)
        hashFunStr, _ := req.Options[hashOptionName].(string)

        // The arguments are subject to the following constraints.
        //
        // nocopy -> filestoreEnabled
        // nocopy -> rawblocks
        // (hash != sha2-256) -> cidv1

        // NOTE: 'rawblocks -> cidv1' is missing. Legacy reasons.

                #  配置项  FilestoreEnabled 必须开启不重复存储文件
        // nocopy -> filestoreEnabled   
        if nocopy && !cfg.Experimental.FilestoreEnabled {
            res.SetError(errors.New("filestore is not enabled, see https://git.io/vNItf"),
                cmdkit.ErrClient)
            return
        }
                # 配置项 裸块
        // nocopy -> rawblocks
        if nocopy && !rawblks {
            // fixed?
            if rbset {
                res.SetError(
                    fmt.Errorf("nocopy option requires '--raw-leaves' to be enabled as well"),
                    cmdkit.ErrNormal,
                )
                return
            }
            // No, satisfy mandatory constraint.
            rawblks = true
        }
                # 配置项  加密算法
        // (hash != "sha2-256") -> CIDv1
        if hashFunStr != "sha2-256" && cidVer == 0 {
            if cidVerSet {
                res.SetError( 
                    errors.New("CIDv0 only supports sha2-256"),
                    cmdkit.ErrClient,
                )
                return
            }
            cidVer = 1
        }

        // cidV1 -> raw blocks (by default)
        if cidVer > 0 && !rbset {
            rawblks = true
        }
               # 有向无环图 获取CID信息 (CID 可以生成hash字符串)
        prefix, err := dag.PrefixForCidVersion(cidVer)
        if err != nil {
            res.SetError(err, cmdkit.ErrNormal)
            return
        }
                # hash加密方式
        hashFunCode, ok := mh.Names[strings.ToLower(hashFunStr)]
        if !ok {
            res.SetError(fmt.Errorf("unrecognized hash function: %s", strings.ToLower(hashFunStr)), cmdkit.ErrNormal)
            return
        }

        prefix.MhType = hashFunCode
        prefix.MhLength = -1

        if hash {
                       #  如果true 新生成一个节点来计算文件hash
            nilnode, err := core.NewNode(n.Context(), &core.BuildCfg{
                //TODO: need this to be true or all files
                // hashed will be stored in memory!
                NilRepo: true,
            })
            if err != nil {
                res.SetError(err, cmdkit.ErrNormal)
                return
            }
            n = nilnode
        }

        addblockstore := n.Blockstore
        if !(fscache || nocopy) {
                        # 使用filestore添加文件
            addblockstore = bstore.NewGCBlockstore(n.BaseBlocks, n.GCLocker)
        }
                
                # 数据库交换
        exch := n.Exchange
        local, _ := req.Options["local"].(bool)
        if local {
                        # 本机执行
            exch = offline.Exchange(addblockstore)
        }
                # 初始化块存储服务
        bserv := blockservice.New(addblockstore, exch) // hash security 001
               # 初始化dag服务
        dserv := dag.NewDAGService(bserv)

        outChan := make(chan interface{}, adderOutChanSize)
                # 初始化文件操作指针
        fileAdder, err := coreunix.NewAdder(req.Context, n.Pinning, n.Blockstore, dserv)
        if err != nil {
            res.SetError(err, cmdkit.ErrNormal)
            return
        }

        fileAdder.Out = outChan
        fileAdder.Chunker = chunker
        fileAdder.Progress = progress
        fileAdder.Hidden = hidden
        fileAdder.Trickle = trickle
        fileAdder.Wrap = wrap
        fileAdder.Pin = dopin
        fileAdder.Silent = silent
        fileAdder.RawLeaves = rawblks
        fileAdder.NoCopy = nocopy
        fileAdder.Prefix = &prefix

        if hash {
            md := dagtest.Mock()
            emptyDirNode := ft.EmptyDirNode()
            // Use the same prefix for the "empty" MFS root as for the file adder.
            emptyDirNode.Prefix = *fileAdder.Prefix
            mr, err := mfs.NewRoot(req.Context, md, emptyDirNode, nil)
            if err != nil {
                res.SetError(err, cmdkit.ErrNormal)
                return
            }

            fileAdder.SetMfsRoot(mr)
        }
                # 遍历文件列表
        addAllAndPin := func(f files.File) error {
            // Iterate over each top-level file and add individually. Otherwise the
            // single files.File f is treated as a directory, affecting hidden file
            // semantics.
            for {
                file, err := f.NextFile()
                if err == io.EOF {
                    // Finished the list of files.
                    break
                } else if err != nil {
                    return err
                }
                if err := fileAdder.AddFile(file); err != nil {
                    return err
                }
            }

            // copy intermediary nodes from editor to our actual dagservice
            _, err := fileAdder.Finalize()
            if err != nil {
                return err
            }

            if hash {
                return nil
            }

            return fileAdder.PinRoot()
        }

        errCh := make(chan error)
        go func() {
            var err error
            defer func() { errCh <- err }()
            defer close(outChan)
            err = addAllAndPin(req.Files)
        }()

        defer res.Close()
               
        err = res.Emit(outChan)
        if err != nil {
            log.Error(err)
            return
        }
        err = <-errCh
        if err != nil {
            res.SetError(err, cmdkit.ErrNormal)
        }
    },
        # 执行顺序 prerun postrun run ...
    PostRun: cmds.PostRunMap{
        cmds.CLI: func(req *cmds.Request, re cmds.ResponseEmitter) cmds.ResponseEmitter {
            reNext, res := cmds.NewChanResponsePair(req)
            outChan := make(chan interface{})

            sizeChan := make(chan int64, 1)

            sizeFile, ok := req.Files.(files.SizeFile)
            if ok {
                // Could be slow.
                go func() {
                    size, err := sizeFile.Size()
                    if err != nil {
                        log.Warningf("error getting files size: %s", err)
                        // see comment above
                        return
                    }

                    sizeChan <- size
                }()
            } else {
                // we don't need to error, the progress bar just
                // won't know how big the files are
                log.Warning("cannot determine size of input file")
            }

            progressBar := func(wait chan struct{}) {
                defer close(wait)

                quiet, _ := req.Options[quietOptionName].(bool)
                quieter, _ := req.Options[quieterOptionName].(bool)
                quiet = quiet || quieter

                progress, _ := req.Options[progressOptionName].(bool)

                var bar *pb.ProgressBar
                if progress {
                    bar = pb.New64(0).SetUnits(pb.U_BYTES)
                    bar.ManualUpdate = true
                    bar.ShowTimeLeft = false
                    bar.ShowPercent = false
                    bar.Output = os.Stderr
                    bar.Start()
                }

                lastFile := ""
                lastHash := ""
                var totalProgress, prevFiles, lastBytes int64

            LOOP:
                for {
                    select {
                    case out, ok := <-outChan:
                        if !ok {
                            if quieter {
                                fmt.Fprintln(os.Stdout, lastHash)
                            }

                            break LOOP
                        }
                        output := out.(*coreunix.AddedObject)
                        if len(output.Hash) > 0 {
                            lastHash = output.Hash
                            if quieter {
                                continue
                            }

                            if progress {
                                // clear progress bar line before we print "added x" output
                                fmt.Fprintf(os.Stderr, "\033[2K\r")
                            }
                            if quiet {
                                fmt.Fprintf(os.Stdout, "%s\n", output.Hash)
                            } else {
                                fmt.Fprintf(os.Stdout, "added %s %s\n", output.Hash, output.Name)
                            }

                        } else {
                            if !progress {
                                continue
                            }

                            if len(lastFile) == 0 {
                                lastFile = output.Name
                            }
                            if output.Name != lastFile || output.Bytes < lastBytes {
                                prevFiles += lastBytes
                                lastFile = output.Name
                            }
                            lastBytes = output.Bytes
                            delta := prevFiles + lastBytes - totalProgress
                            totalProgress = bar.Add64(delta)
                        }

                        if progress {
                            bar.Update()
                        }
                    case size := <-sizeChan:
                        if progress {
                            bar.Total = size
                            bar.ShowPercent = true
                            bar.ShowBar = true
                            bar.ShowTimeLeft = true
                        }
                    case <-req.Context.Done():
                        // don't set or print error here, that happens in the goroutine below
                        return
                    }
                }
            }

            go func() {
                // defer order important! First close outChan, then wait for output to finish, then close re
                defer re.Close()

                if e := res.Error(); e != nil {
                    defer close(outChan)
                    re.SetError(e.Message, e.Code)
                    return
                }

                wait := make(chan struct{})
                go progressBar(wait)

                defer func() { <-wait }()
                defer close(outChan)

                for {
                    v, err := res.Next()
                    if !cmds.HandleError(err, res, re) {
                        break
                    }

                    select {
                    case outChan <- v:
                    case <-req.Context.Done():
                        re.SetError(req.Context.Err(), cmdkit.ErrNormal)
                        return
                    }
                }
            }()

            return reNext
        },
    },
    Type: coreunix.AddedObject{},
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,245评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,749评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,960评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,575评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,668评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,670评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,664评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,422评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,864评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,178评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,340评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,015评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,646评论 3 323
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,265评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,494评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,261评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,206评论 2 352

推荐阅读更多精彩内容

  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,157评论 2 33
  • 如果我可以 我会让时间就此止步 我们是如此地深爱着对方 我一直温习着 满足的幸福 如果我可以 我会让所有的花重开数...
    阿桂爱原创阅读 612评论 25 72
  • 文: 黄飞蝗 01 我是一名中学的教师。一次,听到学生暗地里叫我“黑寡妇”,我气的差点吐血!凭良心,我对教学说不上...
    黄飞蝗阅读 866评论 15 12
  • 投资项目 经营生态农产品,产品涵盖土猪肉、腊肉制品、土鸡、粮油、企业福利和礼品定制。 项目分析 1、中等收入群体逐...
    快乐时光小牛哥阅读 674评论 0 0
  • 今天,又下了一场倾盆大雨,算起来这已经是第二天了。 刚开始的时候天气晴朗,不久就开始下起了倾盆大雨。雨滴落在草上花...
    21小石头邵雨乐阅读 179评论 0 0