@cryptotaxi247 / kubo / commits / 01df5bd21

Export IpfsDir() and CheckIpfsDir()

- IpfsDir gets the location of the ipfs directory, whether it exists or not - CheckIpfsDir get the location and checks whether it exists.

gammazero committed Jan 14, 2021 at 15:43 UTC 01df5bd2118d351a9d18428d9b1b44f3eb0c7950
4 files changed +61 -104
repo/fsrepo/migrations/ipfsdir.go
+54 -94
@@ -23,17 +23,15 @@ const (
23 shellUpTimeout = 2 * time.Second
24 )
25
26 -var (
27 - disableDirCache bool
28 - ipfsDirCache string
29 - ipfsDirCacheKey string
30 -)
26 +func init() {
27 + homedir.DisableCache = true
28 +}
29
30 // ApiEndpoint reads the api file from the local ipfs install directory and
31 // returns the address:port read from the file. If the ipfs directory is not
32 // specified then the default location is used.
33 func ApiEndpoint(ipfsDir string) (string, error) {
36 - ipfsDir, err := checkIpfsDir(ipfsDir)
34 + ipfsDir, err := CheckIpfsDir(ipfsDir)
35 if err != nil {
36 return "", err
37 }
@@ -70,15 +68,60 @@ func ApiShell(ipfsDir string) (*api.Shell, string, error) {
68 return sh, ver, nil
69 }
70
73 -// Returns the path of the default ipfs directory.
74 -func IpfsDir() (string, error) {
75 - return checkIpfsDir("")
71 +// IpfsDir returns the path of the ipfs directory. If dir specified, then
72 +// returns the expanded version dir. If dir is "", then return the directory
73 +// set by IPFS_PATH, or if IPFS_PATH is not set, then return the default
74 +// location in the home directory.
75 +func IpfsDir(dir string) (string, error) {
76 + var err error
77 + if dir != "" {
78 + dir, err = homedir.Expand(dir)
79 + if err != nil {
80 + return "", err
81 + }
82 + return dir, nil
83 + }
84 +
85 + ipfspath := os.Getenv(envIpfsPath)
86 + if ipfspath != "" {
87 + dir, err := homedir.Expand(ipfspath)
88 + if err != nil {
89 + return "", err
90 + }
91 + return dir, nil
92 + }
93 +
94 + home, err := homedir.Dir()
95 + if err != nil {
96 + return "", err
97 + }
98 + if home == "" {
99 + return "", errors.New("could not determine IPFS_PATH, home dir not set")
100 + }
101 +
102 + return path.Join(home, ".ipfs"), nil
103 +}
104 +
105 +// CheckIpfsDir gets the ipfs directory and checks that the directory exists.
106 +func CheckIpfsDir(dir string) (string, error) {
107 + var err error
108 + dir, err = IpfsDir(dir)
109 + if err != nil {
110 + return "", err
111 + }
112 +
113 + _, err = os.Stat(dir)
114 + if err != nil {
115 + return "", err
116 + }
117 +
118 + return dir, nil
119 }
120
121 // RepoVersion returns the version of the repo in the ipfs directory. If the
122 // ipfs directory is not specified then the default location is used.
123 func RepoVersion(ipfsDir string) (int, error) {
81 - ipfsDir, err := checkIpfsDir(ipfsDir)
124 + ipfsDir, err := CheckIpfsDir(ipfsDir)
125 if err != nil {
126 return 0, err
127 }
@@ -88,7 +131,7 @@ func RepoVersion(ipfsDir string) (int, error) {
131 // WriteRepoVersion writes the specified repo version to the repo located in
132 // ipfsDir. If ipfsDir is not specified, then the default location is used.
133 func WriteRepoVersion(ipfsDir string, version int) error {
91 - ipfsDir, err := checkIpfsDir(ipfsDir)
134 + ipfsDir, err := IpfsDir(ipfsDir)
135 if err != nil {
136 return err
137 }
@@ -97,23 +140,6 @@ func WriteRepoVersion(ipfsDir string, version int) error {
140 return ioutil.WriteFile(vFilePath, []byte(fmt.Sprintf("%d\n", version)), 0644)
141 }
142
100 -// CacheIpfsDir enables or disables caching the location of the ipfs directory.
101 -// Enabled by default, this avoids subsequent search for and check of the same
102 -// ipfs directory. Disabling the cache may be useful if the location of the
103 -// ipfs directory is expected to change.
104 -func CacheIpfsDir(enable bool) {
105 - if !enable {
106 - disableDirCache = true
107 - ipfsDirCache = ""
108 - ipfsDirCacheKey = ""
109 - homedir.DisableCache = true
110 - homedir.Reset()
111 - } else {
112 - homedir.DisableCache = false
113 - disableDirCache = false
114 - }
115 -}
116 -
143 func repoVersion(ipfsDir string) (int, error) {
144 c, err := ioutil.ReadFile(path.Join(ipfsDir, versionFile))
145 if err != nil {
@@ -130,69 +156,3 @@ func repoVersion(ipfsDir string) (int, error) {
156 }
157 return ver, nil
158 }
133 -
134 -func checkIpfsDir(dir string) (string, error) {
135 - if dir == ipfsDirCacheKey && ipfsDirCache != "" {
136 - return ipfsDirCache, nil
137 - }
138 -
139 - var (
140 - err error
141 - found string
142 - )
143 - if dir == "" {
144 - found, err = findIpfsDir()
145 - if err != nil {
146 - return "", fmt.Errorf("could not find ipfs directory: %s", err)
147 - }
148 - } else {
149 - found, err = homedir.Expand(dir)
150 - if err != nil {
151 - return "", err
152 - }
153 -
154 - _, err = os.Stat(found)
155 - if err != nil {
156 - return "", err
157 - }
158 - }
159 -
160 - if !disableDirCache {
161 - ipfsDirCacheKey = dir
162 - ipfsDirCache = found
163 - }
164 -
165 - return found, nil
166 -}
167 -
168 -func findIpfsDir() (string, error) {
169 - ipfspath := os.Getenv(envIpfsPath)
170 - if ipfspath != "" {
171 - expandedPath, err := homedir.Expand(ipfspath)
172 - if err != nil {
173 - return "", err
174 - }
175 - return expandedPath, nil
176 - }
177 -
178 - home, err := homedir.Dir()
179 - if err != nil {
180 - return "", err
181 - }
182 - if home == "" {
183 - return "", errors.New("could not determine IPFS_PATH, home dir not set")
184 - }
185 -
186 - for _, dir := range []string{".go-ipfs", ".ipfs"} {
187 - defaultDir := path.Join(home, dir)
188 - _, err = os.Stat(defaultDir)
189 - if err == nil {
190 - return defaultDir, nil
191 - }
192 - if !os.IsNotExist(err) {
193 - return "", err
194 - }
195 - }
196 -
197 - return "", err
198 -}
repo/fsrepo/migrations/ipfsdir_test.go
+5 -9
@@ -12,10 +12,6 @@ var (
12 fakeIpfs string
13 )
14
15 -func init() {
16 - CacheIpfsDir(false)
17 -}
18 -
15 func TestRepoDir(t *testing.T) {
16 var err error
17 fakeHome, err = ioutil.TempDir("", "testhome")
@@ -33,7 +29,7 @@ func TestRepoDir(t *testing.T) {
29 }
30
31 func testFindIpfsDir(t *testing.T) {
36 - _, err := findIpfsDir()
32 + _, err := CheckIpfsDir("")
33 if err == nil {
34 t.Fatal("expected error when no .ipfs directory to find")
35 }
@@ -43,7 +39,7 @@ func testFindIpfsDir(t *testing.T) {
39 panic(err)
40 }
41
46 - dir, err := findIpfsDir()
42 + dir, err := IpfsDir("")
43 if err != nil {
44 t.Fatal(err)
45 }
@@ -52,7 +48,7 @@ func testFindIpfsDir(t *testing.T) {
48 }
49
50 os.Setenv("IPFS_PATH", "~/.ipfs")
55 - dir, err = findIpfsDir()
51 + dir, err = IpfsDir("")
52 if err != nil {
53 t.Fatal(err)
54 }
@@ -62,12 +58,12 @@ func testFindIpfsDir(t *testing.T) {
58 }
59
60 func testCheckIpfsDir(t *testing.T) {
65 - _, err := checkIpfsDir("~/no_such_dir")
61 + _, err := CheckIpfsDir("~/no_such_dir")
62 if err == nil {
63 t.Fatal("expected error from nonexistent directory")
64 }
65
70 - dir, err := checkIpfsDir("~/.ipfs")
66 + dir, err := CheckIpfsDir("~/.ipfs")
67 if err != nil {
68 t.Fatal(err)
69 }
repo/fsrepo/migrations/migrations.go
+1 -1
@@ -22,7 +22,7 @@ const (
22 // RunMigration finds, downloads, and runs the individual migrations needed to
23 // migrate the repo from its current version to the target version.
24 func RunMigration(ctx context.Context, targetVer int, ipfsDir string) error {
25 - ipfsDir, err := checkIpfsDir(ipfsDir)
25 + ipfsDir, err := CheckIpfsDir(ipfsDir)
26 if err != nil {
27 return err
28 }
repo/fsrepo/migrations/migrations_test.go
+1
@@ -65,6 +65,7 @@ func TestFetchMigrations(t *testing.T) {
65 ctx, cancel := context.WithCancel(context.Background())
66 defer cancel()
67
68 + SetIpfsDistPath("/ipfs/QmdFVsmD668ijuBFJwjyXdP5Sq44a5bPNAq3nnQF77kpyJ")
69 _, err := LatestDistVersion(ctx, "ipfs-1-to-2")
70 if err != nil {
71 if strings.Contains(err.Error(), http.StatusText(http.StatusNotFound)) {