From c5291cb6ad496ea72b10d522f9b9df75e5614768 Mon Sep 17 00:00:00 2001 From: Arman Bhalla Date: Thu, 19 Oct 2017 22:29:37 +0100 Subject: [PATCH] Quits program if a file already exists (issue 22). --- connect.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/connect.go b/connect.go index 7e28fe8..cfafbc5 100644 --- a/connect.go +++ b/connect.go @@ -207,9 +207,16 @@ func (c *Connection) runClient() { // have the main thread ask for the okay if id == 0 { fmt.Printf("Receiving file (%d bytes) into: %s\n", c.File.Size, c.File.Name) + var sentFileNames []string + + if fileAlreadyExists(sentFileNames, c.File.Name) { + fmt.Printf("Will not overwrite file!") + os.Exit(1) + } getOK := getInput("ok? (y/n): ") if getOK == "y" { gotOK = true + sentFileNames = append(sentFileNames, c.File.Name) } gotResponse = true } @@ -273,6 +280,15 @@ func (c *Connection) runClient() { } } +func fileAlreadyExists(s []string, f string) bool { + for _, a := range s { + if a == f { + return true + } + } + return false +} + func (c *Connection) catFile(fname string) { // cat the file os.Remove(fname)