|
| 1 | +package sync |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | +) |
| 6 | + |
| 7 | +// s.fileIndex needs to be locked before this function is called |
| 8 | +func shouldRemoveRemote(relativePath string, s *SyncConfig) bool { |
| 9 | + // Exclude changes on the exclude list |
| 10 | + if s.ignoreMatcher != nil { |
| 11 | + if s.ignoreMatcher.MatchesPath(relativePath) { |
| 12 | + return false |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + // Exclude changes on the upload exclude list |
| 17 | + if s.uploadIgnoreMatcher != nil { |
| 18 | + if s.uploadIgnoreMatcher.MatchesPath(relativePath) { |
| 19 | + return false |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + // File / Folder was already deleted from map so event was already processed or should not be processed |
| 24 | + if s.fileIndex.fileMap[relativePath] == nil { |
| 25 | + return false |
| 26 | + } |
| 27 | + |
| 28 | + // Exclude symbolic links |
| 29 | + if s.fileIndex.fileMap[relativePath].IsSymbolicLink { |
| 30 | + return false |
| 31 | + } |
| 32 | + |
| 33 | + return true |
| 34 | +} |
| 35 | + |
| 36 | +// s.fileIndex needs to be locked before this function is called |
| 37 | +func shouldUpload(relativePath string, stat os.FileInfo, s *SyncConfig, isInitial bool) bool { |
| 38 | + // Exclude if stat is nil |
| 39 | + if stat == nil { |
| 40 | + return false |
| 41 | + } |
| 42 | + |
| 43 | + // Exclude changes on the exclude list |
| 44 | + if s.ignoreMatcher != nil { |
| 45 | + if s.ignoreMatcher.MatchesPath(relativePath) { |
| 46 | + return false |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Exclude changes on the upload exclude list |
| 51 | + if s.uploadIgnoreMatcher != nil { |
| 52 | + if s.uploadIgnoreMatcher.MatchesPath(relativePath) { |
| 53 | + // Add to file map and prevent download if local file is newer than the remote one |
| 54 | + if s.fileIndex.fileMap[relativePath] != nil && s.fileIndex.fileMap[relativePath].Mtime < ceilMtime(stat.ModTime()) { |
| 55 | + fileInformation := &fileInformation{ |
| 56 | + Name: relativePath, |
| 57 | + Mtime: ceilMtime(stat.ModTime()), |
| 58 | + Size: stat.Size(), |
| 59 | + IsDirectory: stat.IsDir(), |
| 60 | + } |
| 61 | + |
| 62 | + // Add it to the fileMap |
| 63 | + s.fileIndex.fileMap[relativePath] = fileInformation |
| 64 | + } |
| 65 | + |
| 66 | + return false |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Exclude local symlinks |
| 71 | + if stat.Mode()&os.ModeSymlink != 0 { |
| 72 | + return false |
| 73 | + } |
| 74 | + |
| 75 | + // Check if we already tracked the path |
| 76 | + if s.fileIndex.fileMap[relativePath] != nil { |
| 77 | + // Folder already exists |
| 78 | + if stat.IsDir() { |
| 79 | + // We want to initially walk over all files therefore we return true for a directory |
| 80 | + // Later on a created directory locally that already exists in the fileMap should be ignored |
| 81 | + return isInitial |
| 82 | + } |
| 83 | + |
| 84 | + // Exclude symlinks |
| 85 | + if s.fileIndex.fileMap[relativePath].IsSymbolicLink { |
| 86 | + return false |
| 87 | + } |
| 88 | + |
| 89 | + if isInitial { |
| 90 | + // File is older locally than remote so don't update remote |
| 91 | + if ceilMtime(stat.ModTime()) <= s.fileIndex.fileMap[relativePath].Mtime+1 { |
| 92 | + return false |
| 93 | + } |
| 94 | + } else { |
| 95 | + // File did not change or was changed by downstream |
| 96 | + if ceilMtime(stat.ModTime()) == s.fileIndex.fileMap[relativePath].Mtime && stat.Size() == s.fileIndex.fileMap[relativePath].Size { |
| 97 | + return false |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return true |
| 103 | +} |
| 104 | + |
| 105 | +// s.fileIndex needs to be locked before this function is called |
| 106 | +func shouldDownload(fileInformation *fileInformation, s *SyncConfig) bool { |
| 107 | + // Exclude files on the exclude list |
| 108 | + if s.ignoreMatcher != nil { |
| 109 | + if s.ignoreMatcher.MatchesPath(fileInformation.Name) { |
| 110 | + return false |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Update mode, gid & uid if exists |
| 115 | + if s.fileIndex.fileMap[fileInformation.Name] != nil { |
| 116 | + s.fileIndex.fileMap[fileInformation.Name].RemoteMode = fileInformation.RemoteMode |
| 117 | + s.fileIndex.fileMap[fileInformation.Name].RemoteGID = fileInformation.RemoteGID |
| 118 | + s.fileIndex.fileMap[fileInformation.Name].RemoteUID = fileInformation.RemoteUID |
| 119 | + } |
| 120 | + |
| 121 | + // Exclude files on the exclude list |
| 122 | + if s.downloadIgnoreMatcher != nil { |
| 123 | + if s.downloadIgnoreMatcher.MatchesPath(fileInformation.Name) { |
| 124 | + return false |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Exclude symlinks |
| 129 | + if fileInformation.IsSymbolicLink { |
| 130 | + // Add them to the fileMap though |
| 131 | + s.fileIndex.fileMap[fileInformation.Name] = fileInformation |
| 132 | + return false |
| 133 | + } |
| 134 | + |
| 135 | + // Does file already exist in the filemap? |
| 136 | + if s.fileIndex.fileMap[fileInformation.Name] != nil { |
| 137 | + // Don't override folders that exist in the filemap |
| 138 | + if fileInformation.IsDirectory == false { |
| 139 | + // Redownload file if mtime is newer than saved one |
| 140 | + if fileInformation.Mtime > s.fileIndex.fileMap[fileInformation.Name].Mtime { |
| 141 | + return true |
| 142 | + } |
| 143 | + |
| 144 | + // Redownload file if size changed && file is not older than the one in the fileMap |
| 145 | + // the mTime check is necessary, because otherwise we would override older local files that |
| 146 | + // are not overridden initially |
| 147 | + if fileInformation.Mtime == s.fileIndex.fileMap[fileInformation.Name].Mtime && fileInformation.Size != s.fileIndex.fileMap[fileInformation.Name].Size { |
| 148 | + return true |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return false |
| 153 | + } |
| 154 | + |
| 155 | + return true |
| 156 | +} |
| 157 | + |
| 158 | +// s.fileIndex needs to be locked before this function is called |
| 159 | +// A file is only deleted if the following conditions are met: |
| 160 | +// - The file name is present in the d.config.fileMap map |
| 161 | +// - The file did not change in terms of size and mtime in the d.config.fileMap since we started the collecting changes process |
| 162 | +// - The file is present on the filesystem and did not change in terms of size and mtime on the filesystem |
| 163 | +func shouldRemoveLocal(absFilepath string, fileInformation *fileInformation, s *SyncConfig) bool { |
| 164 | + if fileInformation == nil { |
| 165 | + return false |
| 166 | + } |
| 167 | + |
| 168 | + // Exclude files on the exclude list |
| 169 | + if s.downloadIgnoreMatcher != nil { |
| 170 | + if s.downloadIgnoreMatcher.MatchesPath(fileInformation.Name) { |
| 171 | + return false |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // Only delete if mtime and size did not change |
| 176 | + stat, err := os.Stat(absFilepath) |
| 177 | + if err != nil { |
| 178 | + return false |
| 179 | + } |
| 180 | + |
| 181 | + // We don't delete the file if we haven't tracked it |
| 182 | + if stat != nil && s.fileIndex.fileMap[fileInformation.Name] != nil { |
| 183 | + if stat.IsDir() != s.fileIndex.fileMap[fileInformation.Name].IsDirectory || stat.IsDir() != fileInformation.IsDirectory { |
| 184 | + return false |
| 185 | + } |
| 186 | + |
| 187 | + if fileInformation.IsDirectory == false { |
| 188 | + // We don't delete the file if it has changed in the map since we collected changes |
| 189 | + if fileInformation.Mtime == s.fileIndex.fileMap[fileInformation.Name].Mtime && fileInformation.Size == s.fileIndex.fileMap[fileInformation.Name].Size { |
| 190 | + // We don't delete the file if it has changed on the filesystem meanwhile |
| 191 | + if ceilMtime(stat.ModTime()) <= fileInformation.Mtime { |
| 192 | + return true |
| 193 | + } |
| 194 | + } |
| 195 | + } else { |
| 196 | + return true |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + return false |
| 201 | +} |
0 commit comments