@ -39,6 +39,7 @@ import (
"tailscale.com/net/sockstats"
"tailscale.com/net/sockstats"
"tailscale.com/tailcfg"
"tailscale.com/tailcfg"
"tailscale.com/taildrop"
"tailscale.com/taildrop"
"tailscale.com/tstime"
"tailscale.com/types/views"
"tailscale.com/types/views"
"tailscale.com/util/clientmetric"
"tailscale.com/util/clientmetric"
"tailscale.com/version/distro"
"tailscale.com/version/distro"
@ -586,12 +587,14 @@ func (h *peerAPIHandler) handleServeSockStats(w http.ResponseWriter, r *http.Req
}
}
type incomingFile struct {
type incomingFile struct {
name string // "foo.jpg"
clock tstime . Clock
started time . Time
size int64 // or -1 if unknown; never 0
name string // "foo.jpg"
w io . Writer // underlying writer
started time . Time
ph * peerAPIHandler
size int64 // or -1 if unknown; never 0
partialPath string // non-empty in direct mode
w io . Writer // underlying writer
sendFileNotify func ( ) // called when done
partialPath string // non-empty in direct mode
mu sync . Mutex
mu sync . Mutex
copied int64
copied int64
@ -603,25 +606,23 @@ func (f *incomingFile) markAndNotifyDone() {
f . mu . Lock ( )
f . mu . Lock ( )
f . done = true
f . done = true
f . mu . Unlock ( )
f . mu . Unlock ( )
b := f . ph . ps . b
f . sendFileNotify ( )
b . sendFileNotify ( )
}
}
func ( f * incomingFile ) Write ( p [ ] byte ) ( n int , err error ) {
func ( f * incomingFile ) Write ( p [ ] byte ) ( n int , err error ) {
n , err = f . w . Write ( p )
n , err = f . w . Write ( p )
b := f . ph . ps . b
var needNotify bool
var needNotify bool
defer func ( ) {
defer func ( ) {
if needNotify {
if needNotify {
b . sendFileNotify ( )
f . sendFileNotify ( )
}
}
} ( )
} ( )
if n > 0 {
if n > 0 {
f . mu . Lock ( )
f . mu . Lock ( )
defer f . mu . Unlock ( )
defer f . mu . Unlock ( )
f . copied += int64 ( n )
f . copied += int64 ( n )
now := b . clock . Now ( )
now := f . clock . Now ( )
if f . lastNotify . IsZero ( ) || now . Sub ( f . lastNotify ) > time . Second {
if f . lastNotify . IsZero ( ) || now . Sub ( f . lastNotify ) > time . Second {
f . lastNotify = now
f . lastNotify = now
needNotify = true
needNotify = true
@ -760,11 +761,12 @@ func (h *peerAPIHandler) handlePeerPut(w http.ResponseWriter, r *http.Request) {
var inFile * incomingFile
var inFile * incomingFile
if r . ContentLength != 0 {
if r . ContentLength != 0 {
inFile = & incomingFile {
inFile = & incomingFile {
name : baseName ,
clock : h . ps . b . clock ,
started : h . ps . b . clock . Now ( ) ,
name : baseName ,
size : r . ContentLength ,
started : h . ps . b . clock . Now ( ) ,
w : f ,
size : r . ContentLength ,
ph : h ,
w : f ,
sendFileNotify : h . ps . b . sendFileNotify ,
}
}
if h . ps . taildrop . DirectFileMode {
if h . ps . taildrop . DirectFileMode {
inFile . partialPath = partialFile
inFile . partialPath = partialFile