From a33974f5d0d11258d18a3dfe9450e8798e699472 Mon Sep 17 00:00:00 2001
From: RandomChars <random@chars.jp>
Date: Fri, 29 Oct 2021 15:24:49 +0900
Subject: [PATCH] move some stuff around, disassociate child/parent on image
 destruction

---
 config.go      | 34 ++++------------------------------
 main.go        | 35 ++++++++++++++++++++++++++++++-----
 store/image.go | 10 ++++++++++
 3 files changed, 44 insertions(+), 35 deletions(-)

diff --git a/config.go b/config.go
index 58867fa..b064e14 100644
--- a/config.go
+++ b/config.go
@@ -4,11 +4,13 @@ import (
 	"github.com/fsnotify/fsnotify"
 	log "github.com/sirupsen/logrus"
 	"github.com/spf13/viper"
-	"random.chars.jp/git/image-board/store"
 	"strconv"
 )
 
-var instance *store.Store
+var (
+	serverConfig map[string]interface{}
+	systemConfig map[string]interface{}
+)
 
 func configSetup() {
 	// Configure configuration file parameters
@@ -78,34 +80,6 @@ func configSetup() {
 	systemConfig = viper.GetStringMap("system")
 }
 
-func openStore() {
-	path := systemConfig["store"].(string)
-	single := parseBool(systemConfig["single-user"])
-	private := parseBool(systemConfig["private"])
-
-	instance = store.New(path, single, private)
-	if instance == nil {
-		log.Fatalf("Error initializing store.")
-	}
-	log.Infof("Store opened on %s revision %v compat %v.", path, instance.Revision, instance.Compat)
-	info := instance.User(instance.InitialUser)
-	if info.Snowflake == instance.InitialUser {
-		log.Infof("Initial user ID %s secret %s.", info.Snowflake, info.Secret)
-		if instance.UserPasswordValidate(info.Snowflake, "initial") {
-			log.Warnf("Initial user still has the initial password.")
-		}
-	} else {
-		if single {
-			log.Fatal("Instance has no initial user, single user mode unavailable.")
-		}
-	}
-	if single {
-		log.Info("Server running in single user mode, all operations are performed as the initial user.")
-	} else if private {
-		log.Info("Server running in private mode, all operations will require authentication.")
-	}
-}
-
 func parseBool(v interface{}) bool {
 	if s, ok := v.(bool); !ok {
 		var sS string
diff --git a/main.go b/main.go
index acf82b6..febca53 100644
--- a/main.go
+++ b/main.go
@@ -5,15 +5,12 @@ import (
 	"net/http"
 	"os"
 	"os/signal"
+	"random.chars.jp/git/image-board/store"
 	"syscall"
 )
 
 var (
-	serverConfig map[string]interface{}
-	systemConfig map[string]interface{}
-)
-
-var (
+	instance   *store.Store
 	server     = http.Server{}
 	executable string
 )
@@ -81,3 +78,31 @@ func main() {
 		restart()
 	}
 }
+
+func openStore() {
+	path := systemConfig["store"].(string)
+	single := parseBool(systemConfig["single-user"])
+	private := parseBool(systemConfig["private"])
+
+	instance = store.New(path, single, private)
+	if instance == nil {
+		log.Fatalf("Error initializing store.")
+	}
+	log.Infof("Store opened on %s revision %v compat %v.", path, instance.Revision, instance.Compat)
+	info := instance.User(instance.InitialUser)
+	if info.Snowflake == instance.InitialUser {
+		log.Infof("Initial user ID %s secret %s.", info.Snowflake, info.Secret)
+		if instance.UserPasswordValidate(info.Snowflake, "initial") {
+			log.Warnf("Initial user still has the initial password.")
+		}
+	} else {
+		if single {
+			log.Fatal("Instance has no initial user, single user mode unavailable.")
+		}
+	}
+	if single {
+		log.Info("Server running in single user mode, all operations are performed as the initial user.")
+	} else if private {
+		log.Info("Server running in private mode, all operations will require authentication.")
+	}
+}
diff --git a/store/image.go b/store/image.go
index 9df4293..4bd360f 100644
--- a/store/image.go
+++ b/store/image.go
@@ -428,11 +428,21 @@ func (s *Store) ImageDestroy(hash string) {
 		return
 	}
 
+	// Attempt to disassociate parent
+	s.ImageUpdate(hash, "\000", "", "\000", "\000")
+
 	s.getLock(hash).Lock()
 	defer s.getLock(hash).Unlock()
 
 	info := s.ImageMetadataRead(s.ImageMetadataPath(hash))
 
+	// Disassociate child if set
+	if info.Child != "" {
+		if child := s.ImageSnowflake(info.Child); child.Snowflake == info.Child {
+			s.ImageUpdate(child.Hash, "\000", "", "\000", "\000")
+		}
+	}
+
 	// Untag the image completely
 	tags := s.imageTags(info.Snowflake)
 	for _, tag := range tags {
-- 
GitLab