2023-10-06 00:05:45 +01: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"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/client/tailscale/apitype"
|
|
|
|
"tailscale.com/logtail/backoff"
|
|
|
|
)
|
|
|
|
|
2023-10-06 23:41:14 +01:00
|
|
|
// HasFilesWaiting reports whether any files are buffered in [Handler.Dir].
|
|
|
|
// This always returns false when [Handler.DirectFileMode] is false.
|
2023-10-12 17:28:46 +01:00
|
|
|
func (m *Manager) HasFilesWaiting() bool {
|
|
|
|
if m == nil || m.Dir == "" || m.DirectFileMode {
|
2023-10-06 00:05:45 +01:00
|
|
|
return false
|
|
|
|
}
|
2023-10-12 17:28:46 +01:00
|
|
|
if m.knownEmpty.Load() {
|
2023-10-06 00:05:45 +01: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.
|
|
|
|
return false
|
|
|
|
}
|
2023-10-12 17:28:46 +01:00
|
|
|
f, err := os.Open(m.Dir)
|
2023-10-06 00:05:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
for {
|
|
|
|
des, err := f.ReadDir(10)
|
|
|
|
for _, de := range des {
|
|
|
|
name := de.Name()
|
2023-10-06 23:41:14 +01:00
|
|
|
if strings.HasSuffix(name, partialSuffix) {
|
2023-10-06 00:05:45 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if name, ok := strings.CutSuffix(name, deletedSuffix); ok { // for Windows + tests
|
|
|
|
// After we're done looping over files, then try
|
|
|
|
// to delete this file. Don't do it proactively,
|
|
|
|
// as the OS may return "foo.jpg.deleted" before "foo.jpg"
|
|
|
|
// and we don't want to delete the ".deleted" file before
|
|
|
|
// enumerating to the "foo.jpg" file.
|
2023-10-12 17:28:46 +01:00
|
|
|
defer tryDeleteAgain(filepath.Join(m.Dir, name))
|
2023-10-06 00:05:45 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if de.Type().IsRegular() {
|
2023-10-12 17:28:46 +01:00
|
|
|
_, err := os.Stat(filepath.Join(m.Dir, name+deletedSuffix))
|
2023-10-06 00:05:45 +01:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err == nil {
|
2023-10-12 17:28:46 +01:00
|
|
|
tryDeleteAgain(filepath.Join(m.Dir, name))
|
2023-10-06 00:05:45 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
2023-10-12 17:28:46 +01:00
|
|
|
m.knownEmpty.Store(true)
|
2023-10-06 00:05:45 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitingFiles returns the list of files that have been sent by a
|
2023-10-06 23:41:14 +01:00
|
|
|
// peer that are waiting in [Handler.Dir].
|
|
|
|
// This always returns nil when [Handler.DirectFileMode] is false.
|
2023-10-12 17:28:46 +01:00
|
|
|
func (m *Manager) WaitingFiles() (ret []apitype.WaitingFile, err error) {
|
|
|
|
if m == nil || m.Dir == "" {
|
|
|
|
return nil, ErrNoTaildrop
|
2023-10-06 00:05:45 +01:00
|
|
|
}
|
2023-10-12 17:28:46 +01:00
|
|
|
if m.DirectFileMode {
|
2023-10-06 00:05:45 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
2023-10-12 17:28:46 +01:00
|
|
|
f, err := os.Open(m.Dir)
|
2023-10-06 00:05:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
var deleted map[string]bool // "foo.jpg" => true (if "foo.jpg.deleted" exists)
|
|
|
|
for {
|
|
|
|
des, err := f.ReadDir(10)
|
|
|
|
for _, de := range des {
|
|
|
|
name := de.Name()
|
2023-10-06 23:41:14 +01:00
|
|
|
if strings.HasSuffix(name, partialSuffix) {
|
2023-10-06 00:05:45 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if name, ok := strings.CutSuffix(name, deletedSuffix); ok { // for Windows + tests
|
|
|
|
if deleted == nil {
|
|
|
|
deleted = map[string]bool{}
|
|
|
|
}
|
|
|
|
deleted[name] = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if de.Type().IsRegular() {
|
|
|
|
fi, err := de.Info()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, apitype.WaitingFile{
|
|
|
|
Name: filepath.Base(name),
|
|
|
|
Size: fi.Size(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(deleted) > 0 {
|
|
|
|
// Filter out any return values "foo.jpg" where a
|
|
|
|
// "foo.jpg.deleted" marker file exists on disk.
|
|
|
|
all := ret
|
|
|
|
ret = ret[:0]
|
|
|
|
for _, wf := range all {
|
|
|
|
if !deleted[wf.Name] {
|
|
|
|
ret = append(ret, wf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// And do some opportunistic deleting while we're here.
|
|
|
|
// Maybe Windows is done virus scanning the file we tried
|
|
|
|
// to delete a long time ago and will let us delete it now.
|
|
|
|
for name := range deleted {
|
2023-10-12 17:28:46 +01:00
|
|
|
tryDeleteAgain(filepath.Join(m.Dir, name))
|
2023-10-06 00:05:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Slice(ret, func(i, j int) bool { return ret[i].Name < ret[j].Name })
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// tryDeleteAgain tries to delete path (and path+deletedSuffix) after
|
|
|
|
// it failed earlier. This happens on Windows when various anti-virus
|
|
|
|
// tools hook into filesystem operations and have the file open still
|
|
|
|
// while we're trying to delete it. In that case we instead mark it as
|
|
|
|
// deleted (writing a "foo.jpg.deleted" marker file), but then we
|
|
|
|
// later try to clean them up.
|
|
|
|
//
|
|
|
|
// fullPath is the full path to the file without the deleted suffix.
|
|
|
|
func tryDeleteAgain(fullPath string) {
|
|
|
|
if err := os.Remove(fullPath); err == nil || os.IsNotExist(err) {
|
|
|
|
os.Remove(fullPath + deletedSuffix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-06 23:41:14 +01: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 17:28:46 +01:00
|
|
|
func (m *Manager) DeleteFile(baseName string) error {
|
|
|
|
if m == nil || m.Dir == "" {
|
|
|
|
return ErrNoTaildrop
|
2023-10-06 00:05:45 +01:00
|
|
|
}
|
2023-10-12 17:28:46 +01:00
|
|
|
if m.DirectFileMode {
|
2023-10-06 00:05:45 +01:00
|
|
|
return errors.New("deletes not allowed in direct mode")
|
|
|
|
}
|
2023-10-12 17:28:46 +01:00
|
|
|
path, ok := m.joinDir(baseName)
|
2023-10-06 00:05:45 +01:00
|
|
|
if !ok {
|
|
|
|
return errors.New("bad filename")
|
|
|
|
}
|
|
|
|
var bo *backoff.Backoff
|
2023-10-12 17:28:46 +01:00
|
|
|
logf := m.Logf
|
|
|
|
t0 := m.Clock.Now()
|
2023-10-06 00:05:45 +01:00
|
|
|
for {
|
|
|
|
err := os.Remove(path)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2023-10-06 23:41:14 +01:00
|
|
|
err = redactErr(err)
|
2023-10-06 00:05:45 +01: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-12 17:28:46 +01:00
|
|
|
if m.Clock.Since(t0) < 5*time.Second {
|
2023-10-06 00:05:45 +01:00
|
|
|
bo.BackOff(context.Background(), err)
|
|
|
|
continue
|
|
|
|
}
|
2023-10-06 23:41:14 +01:00
|
|
|
if err := touchFile(path + deletedSuffix); err != nil {
|
2023-10-06 00:05:45 +01:00
|
|
|
logf("peerapi: failed to leave deleted marker: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logf("peerapi: failed to DeleteFile: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-06 23:41:14 +01:00
|
|
|
func touchFile(path string) error {
|
2023-10-06 00:05:45 +01:00
|
|
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
2023-10-06 23:41:14 +01:00
|
|
|
return redactErr(err)
|
2023-10-06 00:05:45 +01:00
|
|
|
}
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
|
2023-10-06 23:41:14 +01: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 17:28:46 +01:00
|
|
|
func (m *Manager) OpenFile(baseName string) (rc io.ReadCloser, size int64, err error) {
|
|
|
|
if m == nil || m.Dir == "" {
|
|
|
|
return nil, 0, ErrNoTaildrop
|
2023-10-06 00:05:45 +01:00
|
|
|
}
|
2023-10-12 17:28:46 +01:00
|
|
|
if m.DirectFileMode {
|
2023-10-06 00:05:45 +01:00
|
|
|
return nil, 0, errors.New("opens not allowed in direct mode")
|
|
|
|
}
|
2023-10-12 17:28:46 +01:00
|
|
|
path, ok := m.joinDir(baseName)
|
2023-10-06 00:05:45 +01:00
|
|
|
if !ok {
|
|
|
|
return nil, 0, errors.New("bad filename")
|
|
|
|
}
|
|
|
|
if fi, err := os.Stat(path + deletedSuffix); err == nil && fi.Mode().IsRegular() {
|
|
|
|
tryDeleteAgain(path)
|
|
|
|
return nil, 0, &fs.PathError{Op: "open", Path: redacted, Err: fs.ErrNotExist}
|
|
|
|
}
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
2023-10-06 23:41:14 +01:00
|
|
|
return nil, 0, redactErr(err)
|
2023-10-06 00:05:45 +01:00
|
|
|
}
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
2023-10-06 23:41:14 +01:00
|
|
|
return nil, 0, redactErr(err)
|
2023-10-06 00:05:45 +01:00
|
|
|
}
|
|
|
|
return f, fi.Size(), nil
|
|
|
|
}
|