parent
71bb5cc4ed
commit
9f098362fb
|
|
@ -30,7 +30,7 @@ import (
|
||||||
|
|
||||||
"d7y.io/dragonfly/v2/cmd/dependency/base"
|
"d7y.io/dragonfly/v2/cmd/dependency/base"
|
||||||
"d7y.io/dragonfly/v2/internal/dferrors"
|
"d7y.io/dragonfly/v2/internal/dferrors"
|
||||||
"d7y.io/dragonfly/v2/pkg/basic"
|
"d7y.io/dragonfly/v2/pkg/os/user"
|
||||||
"d7y.io/dragonfly/v2/pkg/strings"
|
"d7y.io/dragonfly/v2/pkg/strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -215,7 +215,7 @@ func (cfg *CacheOption) checkOutput() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
outputDir, _ := path.Split(cfg.Output)
|
outputDir, _ := path.Split(cfg.Output)
|
||||||
if err := MkdirAll(outputDir, 0777, basic.UserID, basic.UserGroup); err != nil {
|
if err := MkdirAll(outputDir, 0777, os.Getuid(), os.Getgid()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -229,7 +229,7 @@ func (cfg *CacheOption) checkOutput() error {
|
||||||
if err := syscall.Access(dir, syscall.O_RDWR); err == nil {
|
if err := syscall.Access(dir, syscall.O_RDWR); err == nil {
|
||||||
break
|
break
|
||||||
} else if os.IsPermission(err) || dir == "/" {
|
} else if os.IsPermission(err) || dir == "/" {
|
||||||
return fmt.Errorf("user[%s] path[%s] %v", basic.Username, cfg.Output, err)
|
return fmt.Errorf("user[%s] path[%s] %v", user.Username(), cfg.Output, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,8 @@ import (
|
||||||
"d7y.io/dragonfly/v2/cmd/dependency/base"
|
"d7y.io/dragonfly/v2/cmd/dependency/base"
|
||||||
"d7y.io/dragonfly/v2/internal/dferrors"
|
"d7y.io/dragonfly/v2/internal/dferrors"
|
||||||
logger "d7y.io/dragonfly/v2/internal/dflog"
|
logger "d7y.io/dragonfly/v2/internal/dflog"
|
||||||
"d7y.io/dragonfly/v2/pkg/basic"
|
|
||||||
"d7y.io/dragonfly/v2/pkg/net/url"
|
"d7y.io/dragonfly/v2/pkg/net/url"
|
||||||
|
"d7y.io/dragonfly/v2/pkg/os/user"
|
||||||
pkgstrings "d7y.io/dragonfly/v2/pkg/strings"
|
pkgstrings "d7y.io/dragonfly/v2/pkg/strings"
|
||||||
"d7y.io/dragonfly/v2/pkg/unit"
|
"d7y.io/dragonfly/v2/pkg/unit"
|
||||||
)
|
)
|
||||||
|
|
@ -232,7 +232,7 @@ func (cfg *ClientOption) checkOutput() error {
|
||||||
return fmt.Errorf("path[%s] is not absolute path", cfg.Output)
|
return fmt.Errorf("path[%s] is not absolute path", cfg.Output)
|
||||||
}
|
}
|
||||||
outputDir, _ := path.Split(cfg.Output)
|
outputDir, _ := path.Split(cfg.Output)
|
||||||
if err := MkdirAll(outputDir, 0777, basic.UserID, basic.UserGroup); err != nil {
|
if err := MkdirAll(outputDir, 0777, os.Getuid(), os.Getgid()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -247,7 +247,7 @@ func (cfg *ClientOption) checkOutput() error {
|
||||||
if err := syscall.Access(dir, syscall.O_RDWR); err == nil {
|
if err := syscall.Access(dir, syscall.O_RDWR); err == nil {
|
||||||
break
|
break
|
||||||
} else if os.IsPermission(err) || dir == "/" {
|
} else if os.IsPermission(err) || dir == "/" {
|
||||||
return fmt.Errorf("user[%s] path[%s] %v", basic.Username, cfg.Output, err)
|
return fmt.Errorf("user[%s] path[%s] %v", user.Username(), cfg.Output, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -54,9 +54,9 @@ import (
|
||||||
"d7y.io/dragonfly/v2/client/util"
|
"d7y.io/dragonfly/v2/client/util"
|
||||||
"d7y.io/dragonfly/v2/internal/dferrors"
|
"d7y.io/dragonfly/v2/internal/dferrors"
|
||||||
logger "d7y.io/dragonfly/v2/internal/dflog"
|
logger "d7y.io/dragonfly/v2/internal/dflog"
|
||||||
"d7y.io/dragonfly/v2/pkg/basic"
|
|
||||||
"d7y.io/dragonfly/v2/pkg/idgen"
|
"d7y.io/dragonfly/v2/pkg/idgen"
|
||||||
"d7y.io/dragonfly/v2/pkg/net/http"
|
"d7y.io/dragonfly/v2/pkg/net/http"
|
||||||
|
"d7y.io/dragonfly/v2/pkg/os/user"
|
||||||
dfdaemonserver "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/server"
|
dfdaemonserver "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/server"
|
||||||
"d7y.io/dragonfly/v2/pkg/safe"
|
"d7y.io/dragonfly/v2/pkg/safe"
|
||||||
"d7y.io/dragonfly/v2/pkg/source"
|
"d7y.io/dragonfly/v2/pkg/source"
|
||||||
|
|
@ -796,7 +796,7 @@ func checkOutput(output string) error {
|
||||||
return fmt.Errorf("path[%s] is not absolute path", output)
|
return fmt.Errorf("path[%s] is not absolute path", output)
|
||||||
}
|
}
|
||||||
outputDir, _ := path.Split(output)
|
outputDir, _ := path.Split(output)
|
||||||
if err := config.MkdirAll(outputDir, 0777, basic.UserID, basic.UserGroup); err != nil {
|
if err := config.MkdirAll(outputDir, 0777, os.Getuid(), os.Getgid()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -805,7 +805,7 @@ func checkOutput(output string) error {
|
||||||
if err := syscall.Access(dir, syscall.O_RDWR); err == nil {
|
if err := syscall.Access(dir, syscall.O_RDWR); err == nil {
|
||||||
break
|
break
|
||||||
} else if os.IsPermission(err) || dir == "/" {
|
} else if os.IsPermission(err) || dir == "/" {
|
||||||
return fmt.Errorf("user[%s] path[%s] %v", basic.Username, output, err)
|
return fmt.Errorf("user[%s] path[%s] %v", user.Username(), output, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ import (
|
||||||
"d7y.io/dragonfly/v2/client/config"
|
"d7y.io/dragonfly/v2/client/config"
|
||||||
"d7y.io/dragonfly/v2/internal/dferrors"
|
"d7y.io/dragonfly/v2/internal/dferrors"
|
||||||
logger "d7y.io/dragonfly/v2/internal/dflog"
|
logger "d7y.io/dragonfly/v2/internal/dflog"
|
||||||
"d7y.io/dragonfly/v2/pkg/basic"
|
|
||||||
dfdaemonclient "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/client"
|
dfdaemonclient "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -236,8 +235,8 @@ func newExportRequest(cfg *config.DfcacheConfig) *dfdaemonv1.ExportTaskRequest {
|
||||||
UrlMeta: &commonv1.UrlMeta{
|
UrlMeta: &commonv1.UrlMeta{
|
||||||
Tag: cfg.Tag,
|
Tag: cfg.Tag,
|
||||||
},
|
},
|
||||||
Uid: int64(basic.UserID),
|
Uid: int64(os.Getuid()),
|
||||||
Gid: int64(basic.UserGroup),
|
Gid: int64(os.Getgid()),
|
||||||
LocalOnly: cfg.LocalOnly,
|
LocalOnly: cfg.LocalOnly,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,6 @@ import (
|
||||||
|
|
||||||
"d7y.io/dragonfly/v2/client/config"
|
"d7y.io/dragonfly/v2/client/config"
|
||||||
logger "d7y.io/dragonfly/v2/internal/dflog"
|
logger "d7y.io/dragonfly/v2/internal/dflog"
|
||||||
"d7y.io/dragonfly/v2/pkg/basic"
|
|
||||||
"d7y.io/dragonfly/v2/pkg/digest"
|
"d7y.io/dragonfly/v2/pkg/digest"
|
||||||
dfdaemonclient "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/client"
|
dfdaemonclient "d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/client"
|
||||||
"d7y.io/dragonfly/v2/pkg/source"
|
"d7y.io/dragonfly/v2/pkg/source"
|
||||||
|
|
@ -192,8 +191,8 @@ func downloadFromSource(ctx context.Context, cfg *config.DfgetConfig, hdr map[st
|
||||||
}
|
}
|
||||||
|
|
||||||
// change file owner
|
// change file owner
|
||||||
if err = os.Chown(target.Name(), basic.UserID, basic.UserGroup); err != nil {
|
if err = os.Chown(target.Name(), os.Getuid(), os.Getgid()); err != nil {
|
||||||
return fmt.Errorf("change file owner to uid[%d] gid[%d]: %w", basic.UserID, basic.UserGroup, err)
|
return fmt.Errorf("change file owner to uid[%d] gid[%d]: %w", os.Getuid(), os.Getgid(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Rename(target.Name(), cfg.Output); err != nil {
|
if err = os.Rename(target.Name(), cfg.Output); err != nil {
|
||||||
|
|
@ -243,8 +242,8 @@ func newDownRequest(cfg *config.DfgetConfig, hdr map[string]string) *dfdaemonv1.
|
||||||
Application: cfg.Application,
|
Application: cfg.Application,
|
||||||
},
|
},
|
||||||
Pattern: cfg.Pattern,
|
Pattern: cfg.Pattern,
|
||||||
Uid: int64(basic.UserID),
|
Uid: int64(os.Getuid()),
|
||||||
Gid: int64(basic.UserGroup),
|
Gid: int64(os.Getgid()),
|
||||||
KeepOriginalOffset: cfg.KeepOriginalOffset,
|
KeepOriginalOffset: cfg.KeepOriginalOffset,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,10 +35,10 @@ import (
|
||||||
"d7y.io/dragonfly/v2/client/dfget"
|
"d7y.io/dragonfly/v2/client/dfget"
|
||||||
"d7y.io/dragonfly/v2/cmd/dependency"
|
"d7y.io/dragonfly/v2/cmd/dependency"
|
||||||
logger "d7y.io/dragonfly/v2/internal/dflog"
|
logger "d7y.io/dragonfly/v2/internal/dflog"
|
||||||
"d7y.io/dragonfly/v2/pkg/basic"
|
|
||||||
"d7y.io/dragonfly/v2/pkg/dfnet"
|
"d7y.io/dragonfly/v2/pkg/dfnet"
|
||||||
"d7y.io/dragonfly/v2/pkg/dfpath"
|
"d7y.io/dragonfly/v2/pkg/dfpath"
|
||||||
"d7y.io/dragonfly/v2/pkg/net/ip"
|
"d7y.io/dragonfly/v2/pkg/net/ip"
|
||||||
|
"d7y.io/dragonfly/v2/pkg/os/user"
|
||||||
"d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/client"
|
"d7y.io/dragonfly/v2/pkg/rpc/dfdaemon/client"
|
||||||
"d7y.io/dragonfly/v2/pkg/source"
|
"d7y.io/dragonfly/v2/pkg/source"
|
||||||
"d7y.io/dragonfly/v2/pkg/types"
|
"d7y.io/dragonfly/v2/pkg/types"
|
||||||
|
|
@ -95,7 +95,7 @@ var rootCmd = &cobra.Command{
|
||||||
|
|
||||||
fmt.Printf("--%s-- %s\n", start.Format("2006-01-02 15:04:05"), dfgetConfig.URL)
|
fmt.Printf("--%s-- %s\n", start.Format("2006-01-02 15:04:05"), dfgetConfig.URL)
|
||||||
fmt.Printf("dfget version: %s\n", version.GitVersion)
|
fmt.Printf("dfget version: %s\n", version.GitVersion)
|
||||||
fmt.Printf("current user: %s, default peer ip: %s\n", basic.Username, ip.IPv4)
|
fmt.Printf("current user: %s, default peer ip: %s\n", user.Username(), ip.IPv4)
|
||||||
fmt.Printf("output path: %s\n", dfgetConfig.Output)
|
fmt.Printf("output path: %s\n", dfgetConfig.Output)
|
||||||
|
|
||||||
// do get file
|
// do get file
|
||||||
|
|
|
||||||
|
|
@ -1,66 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2020 The Dragonfly Authors
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package basic
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/user"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
logger "d7y.io/dragonfly/v2/internal/dflog"
|
|
||||||
pkgstrings "d7y.io/dragonfly/v2/pkg/strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
HomeDir string
|
|
||||||
TmpDir string
|
|
||||||
Username string
|
|
||||||
UserID int
|
|
||||||
UserGroup int
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
u, err := user.Current()
|
|
||||||
if err != nil {
|
|
||||||
logger.Warnf("Failed to get current user: %s", err.Error())
|
|
||||||
logger.Info("Use uid as Username")
|
|
||||||
|
|
||||||
UserID = syscall.Getuid()
|
|
||||||
Username = fmt.Sprintf("%d", UserID)
|
|
||||||
UserGroup = syscall.Getgid()
|
|
||||||
HomeDir = "/"
|
|
||||||
} else {
|
|
||||||
Username = u.Username
|
|
||||||
UserID, _ = strconv.Atoi(u.Uid)
|
|
||||||
UserGroup, _ = strconv.Atoi(u.Gid)
|
|
||||||
|
|
||||||
HomeDir = u.HomeDir
|
|
||||||
HomeDir = strings.TrimSpace(HomeDir)
|
|
||||||
if pkgstrings.IsBlank(HomeDir) {
|
|
||||||
panic("home dir is empty")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TmpDir = os.TempDir()
|
|
||||||
TmpDir = strings.TrimSpace(TmpDir)
|
|
||||||
if pkgstrings.IsBlank(TmpDir) {
|
|
||||||
TmpDir = "/tmp"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -21,10 +21,10 @@ package dfpath
|
||||||
import (
|
import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"d7y.io/dragonfly/v2/pkg/basic"
|
"d7y.io/dragonfly/v2/pkg/os/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
var DefaultWorkHome = filepath.Join(basic.HomeDir, ".dragonfly")
|
var DefaultWorkHome = filepath.Join(user.HomeDir(), ".dragonfly")
|
||||||
var DefaultCacheDir = filepath.Join(DefaultWorkHome, "cache")
|
var DefaultCacheDir = filepath.Join(DefaultWorkHome, "cache")
|
||||||
var DefaultConfigDir = filepath.Join(DefaultWorkHome, "config")
|
var DefaultConfigDir = filepath.Join(DefaultWorkHome, "config")
|
||||||
var DefaultLogDir = filepath.Join(DefaultWorkHome, "logs")
|
var DefaultLogDir = filepath.Join(DefaultWorkHome, "logs")
|
||||||
|
|
|
||||||
|
|
@ -28,8 +28,6 @@ import (
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"d7y.io/dragonfly/v2/pkg/basic"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSha256(t *testing.T) {
|
func TestSha256(t *testing.T) {
|
||||||
|
|
@ -56,7 +54,7 @@ func TestMd5Reader(t *testing.T) {
|
||||||
|
|
||||||
func TestHashFile(t *testing.T) {
|
func TestHashFile(t *testing.T) {
|
||||||
var expected = "5d41402abc4b2a76b9719d911017c592"
|
var expected = "5d41402abc4b2a76b9719d911017c592"
|
||||||
path := filepath.Join(basic.TmpDir, uuid.NewString())
|
path := filepath.Join(os.TempDir(), uuid.NewString())
|
||||||
f, err := os.OpenFile(path, syscall.O_CREAT|syscall.O_TRUNC|syscall.O_RDWR, fs.FileMode(0644))
|
f, err := os.OpenFile(path, syscall.O_CREAT|syscall.O_TRUNC|syscall.O_RDWR, fs.FileMode(0644))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2022 The Dragonfly Authors
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/user"
|
||||||
|
|
||||||
|
logger "d7y.io/dragonfly/v2/internal/dflog"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HomeDir is the path to the user's home directory.
|
||||||
|
func HomeDir() string {
|
||||||
|
u, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("Failed to get current user: %s. Use / as HomeDir", err.Error())
|
||||||
|
return "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
return u.HomeDir
|
||||||
|
}
|
||||||
|
|
||||||
|
// Username is the username.
|
||||||
|
func Username() string {
|
||||||
|
u, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("Failed to get current user: %s. Use os.Getuid() as username", err.Error())
|
||||||
|
return fmt.Sprint(os.Getuid())
|
||||||
|
}
|
||||||
|
|
||||||
|
return u.Username
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue