Skip to content

Commit ace90d9

Browse files
committed
Config: Refactor filesystem usage detection and add tests photoprism#4266
Signed-off-by: Michael Mayer <michael@photoprism.app>
1 parent fbdd621 commit ace90d9

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

compose.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ services:
128128
working_dir: "/go/src/github.com/photoprism/photoprism"
129129
volumes:
130130
- ".:/go/src/github.com/photoprism/photoprism"
131+
- "./storage:/photoprism"
131132
- "go-mod:/go/pkg/mod"
132133

133134
## MariaDB (Database Server)

internal/config/config_usage.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,26 @@ func (c *Config) Usage() Usage {
4646
Select("SUM(file_size) AS used").
4747
Where("deleted_at IS NULL").
4848
Take(&info).Error; err != nil {
49-
log.Warnf("config: %s", err.Error())
49+
log.Warnf("config: failed to calculate indexed file usage (%s)", err.Error())
5050
}
5151

5252
quotaTotal := c.QuotaBytes()
5353

5454
if m, err := duf.PathInfo(originalsPath); err == nil {
5555
info.Free = m.Free
5656
info.Total = info.Used + m.Free
57+
} else {
58+
log.Debugf("config: failed to detect filesystem usage (%s)", err.Error())
59+
}
5760

58-
if quotaTotal > 0 && quotaTotal < info.Total {
59-
info.Total = quotaTotal
60-
}
61-
62-
if info.Total > 0 {
63-
info.UsedPct = int(math.RoundToEven(float64(info.Used) / float64(info.Total) * 100))
64-
}
65-
} else if quotaTotal > 0 {
61+
if quotaTotal > 0 && quotaTotal < info.Total {
6662
info.Total = quotaTotal
6763
}
6864

65+
if info.Total > 0 {
66+
info.UsedPct = int(math.RoundToEven(float64(info.Used) / float64(info.Total) * 100))
67+
}
68+
6969
if info.Used > 0 && info.UsedPct <= 0 {
7070
info.UsedPct = 1
7171
}

pkg/fs/duf/duf.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func FindByPath(dir string) (m []Mount, warnings []string, err error) {
7171
if len(folders) <= 1 {
7272
filter.OnlyMountPoints = NewFilterValues("/")
7373
} else if parent := strings.TrimSpace(folders[1]); parent != "" {
74-
filter.OnlyMountPoints = NewFilterValues("/", "/"+parent+"/*")
74+
filter.OnlyMountPoints = NewFilterValues("/", "/"+parent+"*")
7575
} else if len(folders) > 2 {
7676
filter.OnlyMountPoints = NewFilterValues("/")
7777
}

pkg/fs/duf/duf_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,62 @@ func TestMounts(t *testing.T) {
3838
}
3939
})
4040
}
41+
42+
func TestPathInfo(t *testing.T) {
43+
t.Run("Success", func(t *testing.T) {
44+
// Get slice of mounted file systems.
45+
result, err := PathInfo("/photoprism/originals")
46+
47+
// No warnings or errors are expected.
48+
assert.NoError(t, err)
49+
50+
// Check result for plausibility.
51+
assert.NotEmpty(t, result.Device)
52+
assert.Equal(t, "local", result.DeviceType)
53+
assert.Equal(t, "/photoprism", result.Mountpoint)
54+
assert.NotEmpty(t, result.Fstype)
55+
assert.NotEmpty(t, result.Opts)
56+
assert.NotEmpty(t, result.Total)
57+
assert.NotEmpty(t, result.Used)
58+
assert.NotEmpty(t, result.Free)
59+
assert.NotEmpty(t, result.Inodes)
60+
assert.NotEmpty(t, result.InodesFree)
61+
assert.NotEmpty(t, result.InodesUsed)
62+
assert.NotEmpty(t, result.Blocks)
63+
assert.NotEmpty(t, result.BlockSize)
64+
assert.NotEmpty(t, result.Metadata)
65+
})
66+
}
67+
68+
func TestFindByPath(t *testing.T) {
69+
t.Run("Success", func(t *testing.T) {
70+
// Get slice of mounted file systems.
71+
results, warnings, err := FindByPath("/photoprism/originals")
72+
73+
// No warnings or errors are expected.
74+
assert.NoError(t, err)
75+
assert.Empty(t, warnings)
76+
77+
// At least one mount returned?
78+
if len(results) < 1 {
79+
t.Error("at least one result expected")
80+
} else {
81+
// If so, check the first mount for plausibility.
82+
result := results[0]
83+
assert.NotEmpty(t, result.Device)
84+
assert.Equal(t, "local", result.DeviceType)
85+
assert.Equal(t, "/photoprism", result.Mountpoint)
86+
assert.NotEmpty(t, result.Fstype)
87+
assert.NotEmpty(t, result.Opts)
88+
assert.NotEmpty(t, result.Total)
89+
assert.NotEmpty(t, result.Used)
90+
assert.NotEmpty(t, result.Free)
91+
assert.NotEmpty(t, result.Inodes)
92+
assert.NotEmpty(t, result.InodesFree)
93+
assert.NotEmpty(t, result.InodesUsed)
94+
assert.NotEmpty(t, result.Blocks)
95+
assert.NotEmpty(t, result.BlockSize)
96+
assert.NotEmpty(t, result.Metadata)
97+
}
98+
})
99+
}

0 commit comments

Comments
 (0)