0

I installed the ipfs version 0.8.0 on WSL Ubuntu 18.04. Started ipfs using sudo ipfs daemon. Added 2 directories using the command sudo ipfs add -r /home/user/ipfstest, it results like this:

added QmfYH2KVxANPA3um1W5MYWA6zR4Awv8VscaWyhhQBVj65L ipfstest/abc.sh added QmTXny9ZjuFPm4C4KbQSEYxvUp2MYbSCLppPQirW7ap4Go ipfstest

Likewise, I added one more directory having 2 files. Now, I need the total files and directories in my ipfs using go-ipfs-api. Following is my code:

package main

import (
        "fmt"
        "context"
        "os"
        "net/http"
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/promhttp"
        "github.com/ipfs/go-ipfs-api"
)

var sh *shell.Shell

func main() {
        sh := shell.NewShell("localhost:5001")
        dir,err:=sh.FilesLs(context.TODO(),"")
        if err != nil {
                fmt.Fprintf(os.Stderr, "error: %s", err)
                os.Exit(1)
        }
        fmt.Printf("Dir are: %d", dir)

        pins,err:=sh.Pins()
        if err != nil {
                fmt.Fprintf(os.Stderr, "error: %s", err)
                os.Exit(1)
        }
        fmt.Printf("Pins are: %d", len(pins))
        dqfs_pincount.Add(float64(len(pins)))
        prometheus.MustRegister(dqfs_pincount)
        http.Handle("/metrics", promhttp.Handler())
        http.ListenAndServe(":8090", nil)
}

If I run this code, I get the output as:

Dir are: [824634392256] Pins are: 18

Pinned files are incremented as I added files. But what is this output [824634392256]? And why only one?

I tried giving a path to the function dir,err:=sh.FilesLs(context.TODO(),"/.ipfs"). As I guess the files and dir's must be stored in ~/.ipfs. But this gives an error:

error: files/ls: file does not exist

How can I get all directories of ipfs? Where I am mistaken? what path should I prove as a parameter? Please help and guide.

Varsh
  • 413
  • 9
  • 26

1 Answers1

0

There's a bit to unpack here.

Why are you using sudo?

IPFS is meant to be run as a regular user. Generally you don't want to run it as root, but you'd instead run the same commands, just without sudo:

ipfs daemon
ipfs add -r /home/user/ipfstest
...

Code doesn't compile

Let's begin with the code, and make sure that's working as intended before moving forward, first off your import:

"github.com/ipfs/go-ipfs-api"

Should read:

shell "github.com/ipfs/go-ipfs-api"

As otherwise the code won't compile, because of your usage of shell later in the code.

Why does dir produce the output it does?

Next, let's look at your usage of dir. You're storing *[]MfsLsEntry (MfsLsEntry), which is a slice of pointers. You're outputting that with string formatting %d, which will be a base10 integer (docs), so the "824634392256" is just the memory address of the MfsLsEntry object in the first index of the slice.

Why does sh.FilesLs(context.TODO(),"/.ipfs") fail?

Well FilesLs isn't querying your own regular filesystem that your OS runs on, but actually MFS. MFS is stored locally though, but using the add API doesn't automatically add something to your MFS. You can use FilesCp to add a CID to your MFS after you add it though.

How do I list my directories on IPFS?

This is a bit of a tricky question. The only data really retain on IPFS is either data pinned, or data referenced in the MFS. So above we already learned the FilesLs command lists the files/directories on your MFS. To list your recursive pins (directories), it's quite simple using the command line:

ipfs pin ls -t recursive

For the API though, you'll first want to call something like Shell.Pins(), filter out for the pins you want (maybe a quick scan through, pull out anything recursive), then query the CIDs using Shell.ObjectStat or whatever you prefer.

If working with the pins though, do remember that it won't feel quite like a regular mutable filesystem, because it isn't. It's much easier to navigate through CIDs added to MFS. So that's how I'd recommend you list your directories on IPFS.

Discordian
  • 749
  • 3
  • 16