From b9e5a077378a0c82f2acbd4ca360c5bafa47c3da Mon Sep 17 00:00:00 2001 From: Ammar Raza Date: Wed, 6 Dec 2023 00:08:24 -0500 Subject: [PATCH 1/4] fix: raise error if waved cannot find public/private dir #2130 --- server.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/server.go b/server.go index 5f9bed66e4..24dd439c8e 100644 --- a/server.go +++ b/server.go @@ -112,12 +112,21 @@ func Run(conf ServerConf) { fileDir := filepath.Join(conf.DataDir, "f") handle("_f/", newFileServer(fileDir, conf.Keychain, auth, conf.BaseURL+"_f")) for _, dir := range conf.PrivateDirs { + prefix, src := splitDirMapping(dir) + err := newDirServer(src, conf.Keychain, auth) + if err != nil { + log.Fatalf("Failed to start server due to directory issue: %v", err) + } echo(Log{"t": "private_dir", "source": src, "address": prefix}) handle(prefix, http.StripPrefix(conf.BaseURL+prefix, newDirServer(src, conf.Keychain, auth))) } for _, dir := range conf.PublicDirs { prefix, src := splitDirMapping(dir) + err := newDirServer(src, conf.Keychain, auth) + if err != nil { + log.Fatalf("Failed to start server due to directory issue: %v", err) + } echo(Log{"t": "public_dir", "source": src, "address": prefix}) handle(prefix, http.StripPrefix(conf.BaseURL+prefix, http.FileServer(http.Dir(src)))) } From bf61b1cd002918ddf7fb0c661767fff433129845 Mon Sep 17 00:00:00 2001 From: Ammar Raza Date: Wed, 6 Dec 2023 14:42:39 -0500 Subject: [PATCH 2/4] fix: added a fucntion to check if the dir is accessible, issue #2130 --- server.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/server.go b/server.go index 24dd439c8e..64705ff7f2 100644 --- a/server.go +++ b/server.go @@ -22,6 +22,7 @@ import ( "io/ioutil" "log" "net/http" + "os" "path" "path/filepath" "strings" @@ -70,6 +71,13 @@ func printLaunchBar(addr, baseURL string, isTLS bool) { log.Println("# └" + bar + "┘") } +// check if the directory is accessible +func checkDirectory(path string) { + if _, err := os.Stat(path); os.IsNotExist(err) { + log.Fatalf("Directory does not exist: %s", path) + } +} + // Run runs the HTTP server. func Run(conf ServerConf) { for _, line := range strings.Split(fmt.Sprintf(logo, conf.Version, conf.BuildDate), "\n") { @@ -113,20 +121,15 @@ func Run(conf ServerConf) { handle("_f/", newFileServer(fileDir, conf.Keychain, auth, conf.BaseURL+"_f")) for _, dir := range conf.PrivateDirs { + checkDirectory(dir) prefix, src := splitDirMapping(dir) - err := newDirServer(src, conf.Keychain, auth) - if err != nil { - log.Fatalf("Failed to start server due to directory issue: %v", err) - } echo(Log{"t": "private_dir", "source": src, "address": prefix}) handle(prefix, http.StripPrefix(conf.BaseURL+prefix, newDirServer(src, conf.Keychain, auth))) } for _, dir := range conf.PublicDirs { + + checkDirectory(dir) prefix, src := splitDirMapping(dir) - err := newDirServer(src, conf.Keychain, auth) - if err != nil { - log.Fatalf("Failed to start server due to directory issue: %v", err) - } echo(Log{"t": "public_dir", "source": src, "address": prefix}) handle(prefix, http.StripPrefix(conf.BaseURL+prefix, http.FileServer(http.Dir(src)))) } From 79d058b1abf7bfd73392c3e528aafcf4402e706d Mon Sep 17 00:00:00 2001 From: Ammar Raza Date: Fri, 8 Dec 2023 05:21:29 -0500 Subject: [PATCH 3/4] fix: added functionality to splitDirMapping --- server.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server.go b/server.go index 64705ff7f2..3fd425aa70 100644 --- a/server.go +++ b/server.go @@ -186,6 +186,11 @@ func splitDirMapping(m string) (string, string) { xs[0] = xs[0][2:] } + // Check if the directory is accessible + if _, err := os.Stat(xs[1]); os.IsNotExist(err) { + panic(fmt.Sprintf("Directory does not exist: %s", xs[1])) + } + return strings.TrimLeft(xs[0], "/"), xs[1] } From 7302053a6bcae89fcde49f49a086e5caf458e76c Mon Sep 17 00:00:00 2001 From: Bakhtiyor Mirzo <82373830+bakhtiyormirzo@users.noreply.github.com> Date: Fri, 8 Dec 2023 16:40:01 -0500 Subject: [PATCH 4/4] fix: added the path checking functionality inside splitDirMapping --- server.go | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/server.go b/server.go index 3fd425aa70..67da4742e5 100644 --- a/server.go +++ b/server.go @@ -71,13 +71,6 @@ func printLaunchBar(addr, baseURL string, isTLS bool) { log.Println("# └" + bar + "┘") } -// check if the directory is accessible -func checkDirectory(path string) { - if _, err := os.Stat(path); os.IsNotExist(err) { - log.Fatalf("Directory does not exist: %s", path) - } -} - // Run runs the HTTP server. func Run(conf ServerConf) { for _, line := range strings.Split(fmt.Sprintf(logo, conf.Version, conf.BuildDate), "\n") { @@ -120,15 +113,11 @@ func Run(conf ServerConf) { fileDir := filepath.Join(conf.DataDir, "f") handle("_f/", newFileServer(fileDir, conf.Keychain, auth, conf.BaseURL+"_f")) for _, dir := range conf.PrivateDirs { - - checkDirectory(dir) prefix, src := splitDirMapping(dir) echo(Log{"t": "private_dir", "source": src, "address": prefix}) handle(prefix, http.StripPrefix(conf.BaseURL+prefix, newDirServer(src, conf.Keychain, auth))) } for _, dir := range conf.PublicDirs { - - checkDirectory(dir) prefix, src := splitDirMapping(dir) echo(Log{"t": "public_dir", "source": src, "address": prefix}) handle(prefix, http.StripPrefix(conf.BaseURL+prefix, http.FileServer(http.Dir(src)))) @@ -176,6 +165,11 @@ func Run(conf ServerConf) { } func splitDirMapping(m string) (string, string) { + // Check if the directory is accessible + if _, err := os.Stat(m); os.IsNotExist(err) { + panic(fmt.Sprintf("Directory does not exist: %s", m)) + } + xs := strings.SplitN(m, "@", 2) if len(xs) < 2 { panic(fmt.Sprintf("invalid directory mapping: want \"remote@local\", got %s", m)) @@ -186,11 +180,6 @@ func splitDirMapping(m string) (string, string) { xs[0] = xs[0][2:] } - // Check if the directory is accessible - if _, err := os.Stat(xs[1]); os.IsNotExist(err) { - panic(fmt.Sprintf("Directory does not exist: %s", xs[1])) - } - return strings.TrimLeft(xs[0], "/"), xs[1] }