1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00

initial implementation where toFile also writes to sockets

This commit is contained in:
postmannen 2023-06-06 12:23:26 +02:00
parent ce1eba687a
commit ea9738950d
2 changed files with 35 additions and 16 deletions

View file

@ -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

View file

@ -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
}
}