MCPcopy
hub / github.com/syncthing/syncthing / handleFile

Method handleFile

lib/model/folder_sendrecv.go:1122–1172  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1120// handleFile queues the copies and pulls as necessary for a single new or
1121// changed file.
1122func (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
1174func (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

Callers 8

processNeededMethod · 0.95
TestHandleFileFunction · 0.80
TestHandleFileWithTempFunction · 0.80
TestCopierFinderFunction · 0.80
TestCopyOwnerFunction · 0.80

Calls 10

reuseBlocksMethod · 0.95
inWritableDirMethod · 0.95
TempNameFunction · 0.92
FilePathFunction · 0.92
blockDiffFunction · 0.85
populateOffsetsFunction · 0.85
newSharedPullerStateFunction · 0.85
GetDeviceFileMethod · 0.65
ReorderMethod · 0.65
LogMethod · 0.65

Tested by 7

TestHandleFileFunction · 0.64
TestHandleFileWithTempFunction · 0.64
TestCopierFinderFunction · 0.64
TestCopyOwnerFunction · 0.64