From ea9738950dd785959333f826a627d2a52a2e2b64 Mon Sep 17 00:00:00 2001 From: postmannen Date: Tue, 6 Jun 2023 12:23:26 +0200 Subject: [PATCH] initial implementation where toFile also writes to sockets --- requests_copy.go | 18 ++++++++++++++++-- requests_file_handling.go | 33 +++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/requests_copy.go b/requests_copy.go index 3334e74..8b6e1aa 100644 --- a/requests_copy.go +++ b/requests_copy.go @@ -442,6 +442,18 @@ type copySubData struct { func copySrcSubProcFunc(proc process, cia copyInitialData, cancel context.CancelFunc, initialMessage Message) func(context.Context, chan Message) error { pf := func(ctx context.Context, procFuncCh chan Message) error { + // Check if the realpath of the directory and filename specified in the + // message are of type unix socket, and if it is we do not add the extra + // suffix to the filename. + file := filepath.Join(initialMessage.Directory, initialMessage.FileName) + + // Check the file is a unix socket, and if it is we write the + // data to the socket instead of writing it to a normal file. + fi, err := os.Stat(file) + if err == nil { + fmt.Printf(" ** DEBUG: STAT ERROR: %v\n", err) + } + // We want to be able to send the reply message when the copying is done, // and also for any eventual errors within the subProcFunc. We want to // write these to the same place as the the reply message for the initial @@ -449,8 +461,10 @@ func copySrcSubProcFunc(proc process, cia copyInitialData, cancel context.Cancel // individual files. msgForSubReplies := initialMessage msgForSubErrors := initialMessage - msgForSubReplies.FileName = msgForSubReplies.FileName + ".copyreply" - msgForSubErrors.FileName = msgForSubErrors.FileName + ".copyerror" + if fi.Mode().Type() != fs.ModeSocket { + msgForSubReplies.FileName = msgForSubReplies.FileName + ".copyreply" + msgForSubErrors.FileName = msgForSubErrors.FileName + ".copyerror" + } var chunkNumber = 0 var lastReadChunk []byte diff --git a/requests_file_handling.go b/requests_file_handling.go index 73b873e..82bc71e 100644 --- a/requests_file_handling.go +++ b/requests_file_handling.go @@ -17,25 +17,30 @@ func reqWriteFileOrSocket(isAppend bool, proc process, message Message) error { fileName, folderTree := selectFileNaming(message, proc) file := filepath.Join(folderTree, fileName) + fmt.Printf("******************* DEBUG: CHECK IF SOCKET OR FILE: %v\n", file) + + // log.Fatalf("EXITING\n") + // Check the file is a unix socket, and if it is we write the // data to the socket instead of writing it to a normal file. fi, err := os.Stat(file) - if err == nil { - if fi.Mode().Type() == fs.ModeSocket { - // TODO: Write to socket - socket, err := net.Dial("unix", file) - if err != nil { - er := fmt.Errorf("error: methodREQToFile/Append could to open socket file for writing: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err) - return er - } - defer socket.Close() + if err == nil && !os.IsNotExist(err) { + er := fmt.Errorf("info: reqWriteFileOrSocket: failed to stat file, but will continue: %v", folderTree) + proc.errorKernel.logDebug(er, proc.configuration) + } - _, err = socket.Write([]byte(message.Data)) - if err != nil { - er := fmt.Errorf("error: methodREQToFile/Append could not write to socket: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err) - return er - } + if fi != nil && fi.Mode().Type() == fs.ModeSocket { + socket, err := net.Dial("unix", file) + if err != nil { + er := fmt.Errorf("error: methodREQToFile/Append could to open socket file for writing: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err) + return er + } + defer socket.Close() + _, err = socket.Write([]byte(message.Data)) + if err != nil { + er := fmt.Errorf("error: methodREQToFile/Append could not write to socket: subject:%v, folderTree: %v, %v", proc.subject, folderTree, err) + return er } }