s.fileIndex needs to be locked before this function is called A file is only deleted if the following conditions are met: - The file name is present in the d.config.fileMap map - The file did not change in terms of size and mtime in the d.config.fileMap since we started the collecting changes proces
(absFilepath string, fileInformation *FileInformation, s *Sync, force bool)
| 115 | // - The file did not change in terms of size and mtime in the d.config.fileMap since we started the collecting changes process |
| 116 | // - The file is present on the filesystem and did not change in terms of size and mtime on the filesystem |
| 117 | func shouldRemoveLocal(absFilepath string, fileInformation *FileInformation, s *Sync, force bool) bool { |
| 118 | if fileInformation == nil { |
| 119 | s.log.Infof("Skip %s because change is nil", absFilepath) |
| 120 | return false |
| 121 | } |
| 122 | |
| 123 | // We don't need to check s.ignoreMatcher, because if a path is ignored it will never be added to the fileMap, because shouldDownload |
| 124 | // and shouldUpload are always false, and hence it never appears in the fileMap and is not copied to the remove fileMap clone |
| 125 | // in the beginning of the downstream mainLoop |
| 126 | |
| 127 | // Only delete if mtime and size did not change |
| 128 | stat, err := os.Lstat(absFilepath) |
| 129 | if err != nil { |
| 130 | if !os.IsNotExist(err) { |
| 131 | s.log.Infof("Skip %s because stat returned %v", absFilepath, err) |
| 132 | } |
| 133 | |
| 134 | return false |
| 135 | } else if stat.Mode()&os.ModeSymlink != 0 { |
| 136 | return true |
| 137 | } |
| 138 | |
| 139 | // Check if deletion is forced |
| 140 | if force { |
| 141 | return true |
| 142 | } |
| 143 | |
| 144 | // We don't delete the file if we haven't tracked it |
| 145 | if stat != nil && s.fileIndex.fileMap[fileInformation.Name] != nil { |
| 146 | if stat.IsDir() != s.fileIndex.fileMap[fileInformation.Name].IsDirectory || stat.IsDir() != fileInformation.IsDirectory { |
| 147 | s.log.Debugf("Skip %s because stat returned unequal isdir with fileMap", absFilepath) |
| 148 | return false |
| 149 | } |
| 150 | |
| 151 | if !fileInformation.IsDirectory { |
| 152 | // We don't delete the file if it has changed in the map since we collected changes |
| 153 | if fileInformation.Mtime == s.fileIndex.fileMap[fileInformation.Name].Mtime && fileInformation.Size == s.fileIndex.fileMap[fileInformation.Name].Size { |
| 154 | // We don't delete the file if it has changed on the filesystem meanwhile |
| 155 | if stat.ModTime().Unix() <= fileInformation.Mtime { |
| 156 | return true |
| 157 | } |
| 158 | |
| 159 | s.log.Debugf("Skip %s because stat.ModTime() %d is greater than fileInformation.Mtime %d", absFilepath, stat.ModTime().Unix(), fileInformation.Mtime) |
| 160 | } else { |
| 161 | // s.log.Infof("Skip %s because Mtime (%d and %d) or Size (%d and %d) is unequal between fileInformation and fileMap", absFilepath, fileInformation.Mtime, s.fileIndex.fileMap[fileInformation.Name].Mtime, fileInformation.Size, s.fileIndex.fileMap[fileInformation.Name].Size) |
| 162 | return true |
| 163 | } |
| 164 | } else { |
| 165 | return true |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return false |
| 170 | } |
no test coverage detected