2023-10-05 23:05:45 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
package taildrop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/client/tailscale/apitype"
|
|
|
|
"tailscale.com/logtail/backoff"
|
|
|
|
)
|
|
|
|
|
2023-10-06 22:41:14 +00:00
|
|
|
// HasFilesWaiting reports whether any files are buffered in [Handler.Dir].
|
|
|
|
// This always returns false when [Handler.DirectFileMode] is false.
|
2023-10-17 20:46:05 +00:00
|
|
|
func (m *Manager) HasFilesWaiting() (has bool) {
|
|
|
|
if m == nil || m.opts.Dir == "" || m.opts.DirectFileMode {
|
2023-10-05 23:05:45 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
|
|
|
|
// Optimization: this is usually empty, so avoid opening
|
|
|
|
// the directory and checking. We can't cache the actual
|
|
|
|
// has-files-or-not values as the macOS/iOS client might
|
|
|
|
// in the future use+delete the files directly. So only
|
|
|
|
// keep this negative cache.
|
|
|
|
totalReceived := m.totalReceived.Load()
|
|
|
|
if totalReceived == m.emptySince.Load() {
|
2023-10-05 23:05:45 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
|
|
|
|
// Check whether there is at least one one waiting file.
|
|
|
|
err := rangeDir(m.opts.Dir, func(de fs.DirEntry) bool {
|
|
|
|
name := de.Name()
|
|
|
|
if isPartialOrDeleted(name) || !de.Type().IsRegular() {
|
|
|
|
return true
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
_, err := os.Stat(filepath.Join(m.opts.Dir, name+deletedSuffix))
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
has = true
|
|
|
|
return false
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
// If there are no more waiting files, record totalReceived as emptySince
|
|
|
|
// so that we can short-circuit the expensive directory traversal
|
|
|
|
// if no files have been received after the start of this call.
|
|
|
|
if err == nil && !has {
|
|
|
|
m.emptySince.Store(totalReceived)
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
return has
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitingFiles returns the list of files that have been sent by a
|
2023-10-06 22:41:14 +00:00
|
|
|
// peer that are waiting in [Handler.Dir].
|
|
|
|
// This always returns nil when [Handler.DirectFileMode] is false.
|
2023-10-12 16:28:46 +00:00
|
|
|
func (m *Manager) WaitingFiles() (ret []apitype.WaitingFile, err error) {
|
2023-10-17 20:46:05 +00:00
|
|
|
if m == nil || m.opts.Dir == "" {
|
2023-10-12 16:28:46 +00:00
|
|
|
return nil, ErrNoTaildrop
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
if m.opts.DirectFileMode {
|
2023-10-05 23:05:45 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
if err := rangeDir(m.opts.Dir, func(de fs.DirEntry) bool {
|
|
|
|
name := de.Name()
|
|
|
|
if isPartialOrDeleted(name) || !de.Type().IsRegular() {
|
|
|
|
return true
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
_, err := os.Stat(filepath.Join(m.opts.Dir, name+deletedSuffix))
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
fi, err := de.Info()
|
|
|
|
if err != nil {
|
|
|
|
return true
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
ret = append(ret, apitype.WaitingFile{
|
|
|
|
Name: filepath.Base(name),
|
|
|
|
Size: fi.Size(),
|
|
|
|
})
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
return true
|
|
|
|
}); err != nil {
|
|
|
|
return nil, redactError(err)
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
|
|
|
sort.Slice(ret, func(i, j int) bool { return ret[i].Name < ret[j].Name })
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2023-10-06 22:41:14 +00:00
|
|
|
// DeleteFile deletes a file of the given baseName from [Handler.Dir].
|
|
|
|
// This method is only allowed when [Handler.DirectFileMode] is false.
|
2023-10-12 16:28:46 +00:00
|
|
|
func (m *Manager) DeleteFile(baseName string) error {
|
2023-10-17 20:46:05 +00:00
|
|
|
if m == nil || m.opts.Dir == "" {
|
2023-10-12 16:28:46 +00:00
|
|
|
return ErrNoTaildrop
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
if m.opts.DirectFileMode {
|
2023-10-05 23:05:45 +00:00
|
|
|
return errors.New("deletes not allowed in direct mode")
|
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
path, err := joinDir(m.opts.Dir, baseName)
|
2023-10-12 23:50:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
|
|
|
var bo *backoff.Backoff
|
2023-10-17 20:46:05 +00:00
|
|
|
logf := m.opts.Logf
|
|
|
|
t0 := m.opts.Clock.Now()
|
2023-10-05 23:05:45 +00:00
|
|
|
for {
|
|
|
|
err := os.Remove(path)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2023-10-17 20:46:05 +00:00
|
|
|
err = redactError(err)
|
2023-10-05 23:05:45 +00:00
|
|
|
// Put a retry loop around deletes on Windows. Windows
|
|
|
|
// file descriptor closes are effectively asynchronous,
|
|
|
|
// as a bunch of hooks run on/after close, and we can't
|
|
|
|
// necessarily delete the file for a while after close,
|
|
|
|
// as we need to wait for everybody to be done with
|
|
|
|
// it. (on Windows, unlike Unix, a file can't be deleted
|
|
|
|
// if it's open anywhere)
|
|
|
|
// So try a few times but ultimately just leave a
|
|
|
|
// "foo.jpg.deleted" marker file to note that it's
|
|
|
|
// deleted and we clean it up later.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if bo == nil {
|
|
|
|
bo = backoff.NewBackoff("delete-retry", logf, 1*time.Second)
|
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
if m.opts.Clock.Since(t0) < 5*time.Second {
|
2023-10-05 23:05:45 +00:00
|
|
|
bo.BackOff(context.Background(), err)
|
|
|
|
continue
|
|
|
|
}
|
2023-10-06 22:41:14 +00:00
|
|
|
if err := touchFile(path + deletedSuffix); err != nil {
|
2023-10-05 23:05:45 +00:00
|
|
|
logf("peerapi: failed to leave deleted marker: %v", err)
|
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
m.deleter.Insert(baseName + deletedSuffix)
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
|
|
|
logf("peerapi: failed to DeleteFile: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-06 22:41:14 +00:00
|
|
|
func touchFile(path string) error {
|
2023-10-05 23:05:45 +00:00
|
|
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
2023-10-17 20:46:05 +00:00
|
|
|
return redactError(err)
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
|
2023-10-06 22:41:14 +00:00
|
|
|
// OpenFile opens a file of the given baseName from [Handler.Dir].
|
|
|
|
// This method is only allowed when [Handler.DirectFileMode] is false.
|
2023-10-12 16:28:46 +00:00
|
|
|
func (m *Manager) OpenFile(baseName string) (rc io.ReadCloser, size int64, err error) {
|
2023-10-17 20:46:05 +00:00
|
|
|
if m == nil || m.opts.Dir == "" {
|
2023-10-12 16:28:46 +00:00
|
|
|
return nil, 0, ErrNoTaildrop
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
if m.opts.DirectFileMode {
|
2023-10-05 23:05:45 +00:00
|
|
|
return nil, 0, errors.New("opens not allowed in direct mode")
|
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
path, err := joinDir(m.opts.Dir, baseName)
|
2023-10-12 23:50:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
2023-10-17 20:46:05 +00:00
|
|
|
if _, err := os.Stat(path + deletedSuffix); err == nil {
|
|
|
|
return nil, 0, redactError(&fs.PathError{Op: "open", Path: path, Err: fs.ErrNotExist})
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
2023-10-17 20:46:05 +00:00
|
|
|
return nil, 0, redactError(err)
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
2023-10-17 20:46:05 +00:00
|
|
|
return nil, 0, redactError(err)
|
2023-10-05 23:05:45 +00:00
|
|
|
}
|
|
|
|
return f, fi.Size(), nil
|
|
|
|
}
|