github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/service/filemanager/manager.go (about)

     1  package filemanager
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  
     7  	"github.com/sagernet/sing/service"
     8  )
     9  
    10  type Manager interface {
    11  	BasePath(name string) string
    12  	OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
    13  	Create(name string) (*os.File, error)
    14  	CreateTemp(pattern string) (*os.File, error)
    15  	Chown(name string) error
    16  	Mkdir(path string, perm os.FileMode) error
    17  	MkdirAll(path string, perm os.FileMode) error
    18  	Remove(path string) error
    19  	RemoveAll(path string) error
    20  }
    21  
    22  func BasePath(ctx context.Context, name string) string {
    23  	manager := service.FromContext[Manager](ctx)
    24  	if manager == nil {
    25  		return name
    26  	}
    27  	return manager.BasePath(name)
    28  }
    29  
    30  func OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (*os.File, error) {
    31  	manager := service.FromContext[Manager](ctx)
    32  	if manager == nil {
    33  		return os.OpenFile(name, flag, perm)
    34  	}
    35  	return manager.OpenFile(name, flag, perm)
    36  }
    37  
    38  func Create(ctx context.Context, name string) (*os.File, error) {
    39  	manager := service.FromContext[Manager](ctx)
    40  	if manager == nil {
    41  		return os.Create(name)
    42  	}
    43  	return manager.Create(name)
    44  }
    45  
    46  func CreateTemp(ctx context.Context, pattern string) (*os.File, error) {
    47  	manager := service.FromContext[Manager](ctx)
    48  	if manager == nil {
    49  		return os.CreateTemp("", pattern)
    50  	}
    51  	return manager.CreateTemp(pattern)
    52  }
    53  
    54  func Chown(ctx context.Context, name string) error {
    55  	manager := service.FromContext[Manager](ctx)
    56  	if manager == nil {
    57  		return nil
    58  	}
    59  	return manager.Chown(name)
    60  }
    61  
    62  func Mkdir(ctx context.Context, path string, perm os.FileMode) error {
    63  	manager := service.FromContext[Manager](ctx)
    64  	if manager == nil {
    65  		return os.Mkdir(path, perm)
    66  	}
    67  	return manager.Mkdir(path, perm)
    68  }
    69  
    70  func MkdirAll(ctx context.Context, path string, perm os.FileMode) error {
    71  	manager := service.FromContext[Manager](ctx)
    72  	if manager == nil {
    73  		return os.MkdirAll(path, perm)
    74  	}
    75  	return manager.MkdirAll(path, perm)
    76  }
    77  
    78  func Remove(ctx context.Context, path string) error {
    79  	manager := service.FromContext[Manager](ctx)
    80  	if manager == nil {
    81  		return os.Remove(path)
    82  	}
    83  	return manager.Remove(path)
    84  }
    85  
    86  func RemoveAll(ctx context.Context, path string) error {
    87  	manager := service.FromContext[Manager](ctx)
    88  	if manager == nil {
    89  		return os.RemoveAll(path)
    90  	}
    91  	return manager.RemoveAll(path)
    92  }
    93  
    94  func WriteFile(ctx context.Context, name string, data []byte, perm os.FileMode) error {
    95  	manager := service.FromContext[Manager](ctx)
    96  	if manager == nil {
    97  		return os.WriteFile(name, data, perm)
    98  	}
    99  	file, err := manager.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
   100  	if err != nil {
   101  		return err
   102  	}
   103  	if err != nil {
   104  		return err
   105  	}
   106  	_, err = file.Write(data)
   107  	if err1 := file.Close(); err1 != nil && err == nil {
   108  		err = err1
   109  	}
   110  	return err
   111  }