MCPcopy Index your code
hub / github.com/syncthing/syncthing / finalClose

Method finalClose

lib/model/sharedpullerstate.go:321–368  ·  view source on GitHub ↗

finalClose atomically closes and returns closed status of a file. A true first return value means the file was closed and should be finished, with the error indicating the success or failure of the close. A false first return value indicates the file is not ready to be closed, or is already closed a

()

Source from the content-addressed store, hash-verified

319// return value indicates the file is not ready to be closed, or is already
320// closed and should in either case not be finished off now.
321func (s *sharedPullerState) finalClose() (bool, error) {
322 s.mut.Lock()
323 defer s.mut.Unlock()
324
325 if s.closed {
326 // Already closed
327 return false, nil
328 }
329
330 if s.pullNeeded+s.copyNeeded != 0 && s.err == nil {
331 // Not done yet, and not errored
332 return false, nil
333 }
334
335 if s.writer == nil {
336 // If we didn't even create a temp file up to this point, now is the
337 // time to do so. This also truncates the file to the correct size
338 // if we're using sparse file.
339 if err := s.addWriterLocked(); err != nil {
340 return false, err
341 }
342 }
343
344 if len(s.file.Encrypted) > 0 {
345 if err := s.finalizeEncrypted(); err != nil && s.err == nil {
346 // This is our error as we weren't errored before.
347 s.err = err
348 }
349 }
350
351 if s.writer != nil {
352 if err := s.writer.SyncClose(s.fsync); err != nil && s.err == nil {
353 // This is our error as we weren't errored before.
354 s.err = err
355 }
356 s.writer = nil
357 }
358
359 s.closed = true
360
361 // Unhide the temporary file when we close it, as it's likely to
362 // immediately be renamed to the final name. If this is a failed temp
363 // file we will also unhide it, but I'm fine with that as we're now
364 // leaving it around for potentially quite a while.
365 s.fs.Unhide(s.tempName)
366
367 return true, s.err
368}
369
370// finalizeEncrypted adds a trailer to the encrypted file containing the
371// serialized FileInfo and the length of that FileInfo. When initializing a

Callers 3

TestReadOnlyDirFunction · 0.95
finisherRoutineMethod · 0.80

Calls 5

addWriterLockedMethod · 0.95
finalizeEncryptedMethod · 0.95
UnlockMethod · 0.80
SyncCloseMethod · 0.80
UnhideMethod · 0.65

Tested by 2

TestReadOnlyDirFunction · 0.76