fix(inputs.directory_monitor): allow cross filesystem directories (#12124)
fixes: #12121
This commit is contained in:
parent
a536764bf1
commit
09d5a9c8b6
|
|
@ -321,18 +321,36 @@ func (monitor *DirectoryMonitor) sendMetrics(metrics []telegraf.Metric) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (monitor *DirectoryMonitor) moveFile(filePath string, directory string) {
|
func (monitor *DirectoryMonitor) moveFile(srcPath string, dstBaseDir string) {
|
||||||
basePath := strings.Replace(filePath, monitor.Directory, "", 1)
|
// Appends any subdirectories in the srcPath to the dstBaseDir and
|
||||||
newPath := filepath.Join(directory, basePath)
|
// creates those subdirectories.
|
||||||
|
basePath := strings.Replace(srcPath, monitor.Directory, "", 1)
|
||||||
err := os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
|
dstPath := filepath.Join(dstBaseDir, basePath)
|
||||||
|
err := os.MkdirAll(filepath.Dir(dstPath), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
monitor.Log.Errorf("Error creating directory hierachy for " + filePath + ". Error: " + err.Error())
|
monitor.Log.Errorf("Error creating directory hierachy for " + srcPath + ". Error: " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.Rename(filePath, newPath)
|
inputFile, err := os.Open(srcPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
monitor.Log.Errorf("Error while moving file '" + filePath + "' to another directory. Error: " + err.Error())
|
monitor.Log.Errorf("Could not open input file: %s", err)
|
||||||
|
}
|
||||||
|
defer inputFile.Close()
|
||||||
|
|
||||||
|
outputFile, err := os.Create(dstPath)
|
||||||
|
if err != nil {
|
||||||
|
monitor.Log.Errorf("Could not open output file: %s", err)
|
||||||
|
}
|
||||||
|
defer outputFile.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(outputFile, inputFile)
|
||||||
|
if err != nil {
|
||||||
|
monitor.Log.Errorf("Writing to output file failed: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.Remove(srcPath)
|
||||||
|
if err != nil {
|
||||||
|
monitor.Log.Errorf("Failed removing original file: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue