diff --git a/store/image.go b/store/image.go index 2b5fdfd26c2825c8344b202089d920e02424bc4a..c136916f478e675df930dbe52833909ceec61a8f 100644 --- a/store/image.go +++ b/store/image.go @@ -110,7 +110,7 @@ func (s *Store) ImageData(hash string, preview bool) (Image, []byte) { // ImageTags returns tags of an image with specific hash. func (s *Store) ImageTags(flake string) []string { - if !s.flake(flake) || !s.dir(s.ImageTagsPath(flake)) { + if !numerical(flake) || !s.dir(s.ImageTagsPath(flake)) { return nil } @@ -133,7 +133,7 @@ func (s *Store) imageTags(flake string) []string { // ImageHasTag figures out if an image has a tag. func (s *Store) ImageHasTag(flake, tag string) bool { - if !s.flake(flake) || !nameRegex.MatchString(tag) { + if !numerical(flake) || !nameRegex.MatchString(tag) { return false } return s.file(s.ImageTagsPath(flake) + "/" + tag) @@ -211,7 +211,7 @@ func (s *Store) ImageSearch(tags []string) []string { // ImageAdd adds an image to the store. func (s *Store) ImageAdd(data []byte, flake string) Image { - if !s.flake(flake) || !s.dir(s.UserPath(flake)) { + if !numerical(flake) || !s.dir(s.UserPath(flake)) { return Image{} } @@ -375,7 +375,7 @@ func (s *Store) ImageSnowflakes() []string { // ImageSnowflakeHash returns image hash from snowflake. func (s *Store) ImageSnowflakeHash(flake string) string { - if !s.flake(flake) { + if !numerical(flake) { return "" } @@ -439,7 +439,7 @@ func (s *Store) ImageDestroy(hash string) { // ImageTagAdd adds a tag to an image with specific snowflake. func (s *Store) ImageTagAdd(flake, tag string) { - if !nameRegex.MatchString(tag) || !s.flake(flake) || !s.dir(s.ImageTagsPath(flake)) || !s.dir(s.TagPath(tag)) || + if !nameRegex.MatchString(tag) || !numerical(flake) || !s.dir(s.ImageTagsPath(flake)) || !s.dir(s.TagPath(tag)) || s.file(s.TagPath(tag)+"/"+flake) { return } @@ -455,7 +455,7 @@ func (s *Store) ImageTagAdd(flake, tag string) { // ImageTagRemove removes a tag from an image with specific snowflake. func (s *Store) ImageTagRemove(flake, tag string) { - if !nameRegex.MatchString(tag) || !s.flake(flake) || !s.dir(s.ImageTagsPath(flake)) || !s.dir(s.TagPath(tag)) { + if !nameRegex.MatchString(tag) || !numerical(flake) || !s.dir(s.ImageTagsPath(flake)) || !s.dir(s.TagPath(tag)) { return } diff --git a/store/misc.go b/store/misc.go index dd6ee40d32b0e4b3765cbc8584f1255ceac7b25a..38a1c47ece99a0d9c03a83576291f7702b07ba07 100644 --- a/store/misc.go +++ b/store/misc.go @@ -4,6 +4,7 @@ import ( "errors" "net/url" "regexp" + "strconv" ) const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -19,6 +20,15 @@ var ( AlreadyExists = errors.New("store path already exists") ) +// numerical validates a numerical string. +func numerical(flake string) bool { + if flake == "" { + return false + } + _, err := strconv.Atoi(flake) + return err == nil +} + // MatchName determines if str is a valid name. func MatchName(str string) bool { return nameRegex.MatchString(str) @@ -29,15 +39,3 @@ func MatchURL(str string) bool { u, err := url.Parse(str) return err == nil && u.Scheme != "" && u.Host != "" } - -// These two really shouldn't be methods... Maybe change that for v2. - -// MatchName determines if str is a valid name. As of v1, this just calls MatchName. -func (s *Store) MatchName(str string) bool { - return MatchName(str) -} - -// MatchURL determines if str is a valid URL. As of v1, this just calls MatchURL. -func (s *Store) MatchURL(str string) bool { - return MatchURL(str) -} diff --git a/store/page.go b/store/page.go index f5c555f63af8bf066fabb2e22d0cfbdec74f8f94..f5971ccd483e02a078956dcb688751617bc7c4ae 100644 --- a/store/page.go +++ b/store/page.go @@ -57,8 +57,12 @@ func (s *Store) pageGetTotalCount(variant string) uint64 { return 0 } -// pageSetTotalCount sets total count of a page variant. +// pageSetTotalCount sets total count of a page variant and destroys it if zero. func (s *Store) pageSetTotalCount(variant string, value uint64) { + if value == 0 { + s.pageDBDestroy(variant) + return + } db := s.pageDB(variant) payload := make([]byte, 8) diff --git a/store/store.go b/store/store.go index b96489bc388b31b941df8a16445842a15ba5110f..3d3ab5f05104e72ac1e1abf569e9e3c3b131b567 100644 --- a/store/store.go +++ b/store/store.go @@ -257,15 +257,6 @@ func (s *Store) dir(path string) bool { return true } -// flake validates a snowflake string. -func (s *Store) flake(flake string) bool { - if flake == "" { - return false - } - _, err := strconv.Atoi(flake) - return err == nil -} - // link provides symlink-like usage while considering compat mode. func (s *Store) link(old, new string) { if !s.Compat { @@ -305,3 +296,15 @@ func (s *Store) readlink(path string) string { } return "" } + +// TODO: These two really shouldn't be methods... Maybe change that for v2. + +// MatchName determines if str is a valid name. As of v1, this just calls MatchName. +func (s *Store) MatchName(str string) bool { + return MatchName(str) +} + +// MatchURL determines if str is a valid URL. As of v1, this just calls MatchURL. +func (s *Store) MatchURL(str string) bool { + return MatchURL(str) +} diff --git a/store/user.go b/store/user.go index 354739e80137b1fa9aba38b8e2dcf3bcb94bf14d..f8795a2bd0f13d7c8a1b081f3c8d9b23539ea566 100644 --- a/store/user.go +++ b/store/user.go @@ -32,7 +32,7 @@ func (s *Store) user(path string) User { // User returns user information with specific snowflake. func (s *Store) User(flake string) User { - if !s.flake(flake) || !s.file(s.UserPath(flake)) { + if !numerical(flake) || !s.file(s.UserPath(flake)) { return User{} } @@ -100,7 +100,7 @@ func (s *Store) UserAdd(username, password string, privileged bool) User { // UserPrivileged sets privileged status of user with specific snowflake. func (s *Store) UserPrivileged(flake string, privileged bool) { - if !s.flake(flake) { + if !numerical(flake) { return } @@ -118,7 +118,7 @@ func (s *Store) UserPrivileged(flake string, privileged bool) { // UserUsernameUpdate updates username of user with specific snowflake. func (s *Store) UserUsernameUpdate(flake, username string) bool { - if !s.flake(flake) || !nameRegex.MatchString(username) || s.file(s.UsernamePath(username)) { + if !numerical(flake) || !nameRegex.MatchString(username) || s.file(s.UsernamePath(username)) { return false } @@ -147,7 +147,7 @@ func (s *Store) UserUsernameUpdate(flake, username string) bool { // UserSecretRegen regenerates secret of user with specific snowflake. func (s *Store) UserSecretRegen(flake string) string { - if !s.flake(flake) { + if !numerical(flake) { return "" } @@ -223,7 +223,7 @@ func (s *Store) userPasswordUpdate(path, password string) bool { // UserPasswordValidate validates password of specified user. func (s *Store) UserPasswordValidate(flake, password string) bool { - if !s.flake(flake) || !s.file(s.UserPath(flake)) { + if !numerical(flake) || !s.file(s.UserPath(flake)) { return false } @@ -256,7 +256,7 @@ func (s *Store) UserUsernamePasswordValidate(username, password string) bool { // UserPasswordUpdate updates password of specified user. func (s *Store) UserPasswordUpdate(flake, password string) bool { - if !s.flake(flake) { + if !numerical(flake) { return false } @@ -268,7 +268,7 @@ func (s *Store) UserPasswordUpdate(flake, password string) bool { // UserDestroy destroys a user with specific snowflake. func (s *Store) UserDestroy(flake string) { - if !s.flake(flake) { + if !numerical(flake) { return } if !s.dir(s.UserPath(flake)) { @@ -294,7 +294,7 @@ func (s *Store) UserDestroy(flake string) { // UserImages returns slice of a user's images. func (s *Store) UserImages(flake string) []string { - if !s.flake(flake) { + if !numerical(flake) { return nil } if !s.dir(s.UserImagesPath(flake)) {