This is the flow of data and events here, I think... +-----------------------+ | | - - - - > ItemStarted | handleFile | - - - - > ItemFinished (on shortcuts) | | +-----------------------+ | | copyChan (copyBlocksState; unless shortcut taken) |
(ctx context.Context, file protocol.FileInfo, copyChan chan<- copyBlocksState)
| 1120 | // handleFile queues the copies and pulls as necessary for a single new or |
| 1121 | // changed file. |
| 1122 | func (f *sendReceiveFolder) handleFile(ctx context.Context, file protocol.FileInfo, copyChan chan<- copyBlocksState) error { |
| 1123 | curFile, hasCurFile, err := f.model.sdb.GetDeviceFile(f.folderID, protocol.LocalDeviceID, file.Name) |
| 1124 | if err != nil { |
| 1125 | return err |
| 1126 | } |
| 1127 | |
| 1128 | have, _ := blockDiff(curFile.Blocks, file.Blocks) |
| 1129 | |
| 1130 | tempName := fs.TempName(file.Name) |
| 1131 | |
| 1132 | populateOffsets(file.Blocks) |
| 1133 | |
| 1134 | blocks := append([]protocol.BlockInfo{}, file.Blocks...) |
| 1135 | reused := make([]int, 0, len(file.Blocks)) |
| 1136 | |
| 1137 | if f.Type != config.FolderTypeReceiveEncrypted { |
| 1138 | blocks, reused = f.reuseBlocks(ctx, blocks, reused, file, tempName) |
| 1139 | } |
| 1140 | |
| 1141 | // The sharedpullerstate will know which flags to use when opening the |
| 1142 | // temp file depending if we are reusing any blocks or not. |
| 1143 | if len(reused) == 0 { |
| 1144 | // Otherwise, discard the file ourselves in order for the |
| 1145 | // sharedpuller not to panic when it fails to exclusively create a |
| 1146 | // file which already exists |
| 1147 | f.inWritableDir(f.mtimefs.Remove, tempName) |
| 1148 | } |
| 1149 | |
| 1150 | // Reorder blocks |
| 1151 | blocks = f.blockPullReorderer.Reorder(blocks) |
| 1152 | |
| 1153 | f.evLogger.Log(events.ItemStarted, map[string]string{ |
| 1154 | "folder": f.folderID, |
| 1155 | "item": file.Name, |
| 1156 | "type": "file", |
| 1157 | "action": "update", |
| 1158 | }) |
| 1159 | |
| 1160 | s := newSharedPullerState(file, f.mtimefs, f.folderID, tempName, blocks, reused, f.IgnorePerms || file.NoPermissions, hasCurFile, curFile, !f.DisableSparseFiles, !f.DisableFsync) |
| 1161 | |
| 1162 | f.sl.DebugContext(ctx, "Handling file", slogutil.FilePath(file.Name), "blocksToCopy", len(blocks), "reused", len(reused)) |
| 1163 | |
| 1164 | cs := copyBlocksState{ |
| 1165 | sharedPullerState: s, |
| 1166 | blocks: blocks, |
| 1167 | have: len(have), |
| 1168 | } |
| 1169 | |
| 1170 | copyChan <- cs |
| 1171 | return nil |
| 1172 | } |
| 1173 | |
| 1174 | func (f *sendReceiveFolder) reuseBlocks(ctx context.Context, blocks []protocol.BlockInfo, reused []int, file protocol.FileInfo, tempName string) ([]protocol.BlockInfo, []int) { |
| 1175 | // Check for an old temporary file which might have some blocks we could |