nuclei/pkg/output/file_output_writer.go
Tarun Koyalwar dc44105baf
nuclei v3 : misc updates (#4247)
* use parsed options while signing

* update project layout to v3

* fix .gitignore

* remove example template

* misc updates

* bump tlsx version

* hide template sig warning with env

* js: retain value while using log

* fix nil pointer derefernce

* misc doc update

---------

Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com>
2023-10-17 17:44:13 +05:30

50 lines
1.1 KiB
Go

package output
import (
"os"
"sync"
)
// fileWriter is a concurrent file based output writer.
type fileWriter struct {
file *os.File
mu sync.Mutex
}
// NewFileOutputWriter creates a new buffered writer for a file
func newFileOutputWriter(file string, resume bool) (*fileWriter, error) {
var output *os.File
var err error
if resume {
output, err = os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
} else {
output, err = os.Create(file)
}
if err != nil {
return nil, err
}
return &fileWriter{file: output}, nil
}
// WriteString writes an output to the underlying file
func (w *fileWriter) Write(data []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
if _, err := w.file.Write(data); err != nil {
return 0, err
}
if _, err := w.file.Write([]byte("\n")); err != nil {
return 0, err
}
return len(data) + 1, nil
}
// Close closes the underlying writer flushing everything to disk
func (w *fileWriter) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
//nolint:errcheck // we don't care whether sync failed or succeeded.
w.file.Sync()
return w.file.Close()
}