Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(xfs): xfs 包 用于处理文件系统业务, 目前支持文件拷贝 & 文件夹递归拷贝 #31

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 13 additions & 50 deletions diskq/diskq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -81,18 +80,15 @@
l := NewTestLogger(t)

dqName := "test_disk_queue" + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()

Check failure on line 83 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.TempDir returns the user's temporary directory, for example /tmp

Check failure on line 83 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.TempDir returns the user's temporary directory, for example /tmp
defer os.RemoveAll(tmpDir)

Check failure on line 84 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.RemoveAll deletes the user's entire temporary directory, not a subdirectory therein (SA9007)

Check failure on line 84 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.RemoveAll deletes the user's entire temporary directory, not a subdirectory therein (SA9007)
dq := newDiskq(dqName, tmpDir, 1024, 4, 1<<10, 2500, 2*time.Second, l)
defer dq.Close()
NotNil(t, dq)
Equal(t, int64(0), dq.Depth())

msg := []byte("test")
err = dq.Put(msg)
err := dq.Put(msg)
Nil(t, err)
Equal(t, int64(1), dq.Depth())

Expand All @@ -103,11 +99,8 @@
func TestDiskQueueRoll(t *testing.T) {
l := NewTestLogger(t)
dqName := "test_disk_queue_roll" + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()

Check failure on line 102 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.TempDir returns the user's temporary directory, for example /tmp

Check failure on line 102 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.TempDir returns the user's temporary directory, for example /tmp
defer os.RemoveAll(tmpDir)

Check failure on line 103 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.RemoveAll deletes the user's entire temporary directory, not a subdirectory therein (SA9007)

Check failure on line 103 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.RemoveAll deletes the user's entire temporary directory, not a subdirectory therein (SA9007)
msg := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
ml := int64(len(msg))
dq := newDiskq(dqName, tmpDir, 10*(ml+4), int32(ml), 1<<10, 2500, 2*time.Second, l)
Expand All @@ -133,11 +126,8 @@
func TestDiskQueuePeek(t *testing.T) {
l := NewTestLogger(t)
dqName := "test_disk_queue_peek" + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()

Check failure on line 129 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.TempDir returns the user's temporary directory, for example /tmp

Check failure on line 129 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.TempDir returns the user's temporary directory, for example /tmp
defer os.RemoveAll(tmpDir)

Check failure on line 130 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.RemoveAll deletes the user's entire temporary directory, not a subdirectory therein (SA9007)

Check failure on line 130 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.RemoveAll deletes the user's entire temporary directory, not a subdirectory therein (SA9007)
msg := bytes.Repeat([]byte{0}, 10)
ml := int64(len(msg))
dq := newDiskq(dqName, tmpDir, 10*(ml+4), int32(ml), 1<<10, 2500, 2*time.Second, l)
Expand Down Expand Up @@ -216,11 +206,8 @@
func TestDiskQueueEmpty(t *testing.T) {
l := NewTestLogger(t)
dqName := "test_disk_queue_empty" + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()

Check failure on line 209 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.TempDir returns the user's temporary directory, for example /tmp

Check failure on line 209 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.TempDir returns the user's temporary directory, for example /tmp
defer os.RemoveAll(tmpDir)

Check failure on line 210 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.RemoveAll deletes the user's entire temporary directory, not a subdirectory therein (SA9007)

Check failure on line 210 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.RemoveAll deletes the user's entire temporary directory, not a subdirectory therein (SA9007)
msg := bytes.Repeat([]byte{0}, 10)
dq := newDiskq(dqName, tmpDir, 100, 0, 1<<10, 2500, 2*time.Second, l)
defer dq.Close()
Expand Down Expand Up @@ -284,11 +271,8 @@
func TestDiskQueueCorruption(t *testing.T) {
l := NewTestLogger(t)
dqName := "test_disk_queue_corruption" + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()

Check failure on line 274 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.TempDir returns the user's temporary directory, for example /tmp

Check failure on line 274 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.TempDir returns the user's temporary directory, for example /tmp
defer os.RemoveAll(tmpDir)

Check failure on line 275 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.RemoveAll deletes the user's entire temporary directory, not a subdirectory therein (SA9007)

Check failure on line 275 in diskq/diskq_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

this call to os.RemoveAll deletes the user's entire temporary directory, not a subdirectory therein (SA9007)
// require a non-zero message length for the corrupt (len 0) test below
dq := newDiskq(dqName, tmpDir, 1000, 10, 1<<10, 5, 2*time.Second, l)
defer dq.Close()
Expand Down Expand Up @@ -383,10 +367,7 @@
func TestDiskQueueSyncAfterRead(t *testing.T) {
l := NewTestLogger(t)
dqName := "test_disk_queue_read_after_sync" + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()
defer os.RemoveAll(tmpDir)
dq := newDiskq(dqName, tmpDir, 1<<11, 0, 1<<10, 2500, 50*time.Millisecond, l)
defer dq.Close()
Expand Down Expand Up @@ -434,10 +415,7 @@

l := NewTestLogger(t)
dqName := "test_disk_queue_torture" + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()
defer os.RemoveAll(tmpDir)
dq := newDiskq(dqName, tmpDir, 262144, 0, 1<<10, 2500, 2*time.Second, l)
NotNil(t, dq)
Expand Down Expand Up @@ -521,10 +499,7 @@
func TestDiskQueueResize(t *testing.T) {
l := NewTestLogger(t)
dqName := "test_disk_queue_resize" + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()
defer os.RemoveAll(tmpDir)
msg := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
ml := int64(len(msg))
Expand Down Expand Up @@ -604,10 +579,7 @@
b.StopTimer()
l := NewTestLogger(b)
dqName := "bench_disk_queue_put" + strconv.Itoa(b.N) + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()
defer os.RemoveAll(tmpDir)
dq := newDiskq(dqName, tmpDir, 1024768*100, 0, 1<<20, 2500, 2*time.Second, l)
defer dq.Close()
Expand Down Expand Up @@ -653,10 +625,7 @@
func benchmarkDiskWrite(size int64, b *testing.B) {
b.StopTimer()
fileName := "bench_disk_queue_put" + strconv.Itoa(b.N) + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()
defer os.RemoveAll(tmpDir)
f, _ := os.OpenFile(path.Join(tmpDir, fileName), os.O_RDWR|os.O_CREATE, 0600)
b.SetBytes(size)
Expand Down Expand Up @@ -699,10 +668,7 @@
func benchmarkDiskWriteBuffered(size int64, b *testing.B) {
b.StopTimer()
fileName := "bench_disk_queue_put" + strconv.Itoa(b.N) + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()
defer os.RemoveAll(tmpDir)
f, _ := os.OpenFile(path.Join(tmpDir, fileName), os.O_RDWR|os.O_CREATE, 0600)
b.SetBytes(size)
Expand Down Expand Up @@ -755,10 +721,7 @@
b.StopTimer()
l := NewTestLogger(b)
dqName := "bench_disk_queue_get" + strconv.Itoa(b.N) + strconv.Itoa(int(time.Now().Unix()))
tmpDir, err := ioutil.TempDir("", fmt.Sprintf("nsq-test-%d", time.Now().UnixNano()))
if err != nil {
panic(err)
}
tmpDir := os.TempDir()
defer os.RemoveAll(tmpDir)
dq := newDiskq(dqName, tmpDir, 1024768, 0, 1<<30, 2500, 2*time.Second, l)
defer dq.Close()
Expand Down
92 changes: 92 additions & 0 deletions xfs/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package xfs

import (
"fmt"
"io"
"os"
"path/filepath"
)

// CopyDirectory 递归地复制整个目录
func CopyDirectory(src string, dst string) error {
src = filepath.Clean(src)
dst = filepath.Clean(dst)

si, err := os.Stat(src)
if err != nil {
return err
}
if !si.IsDir() {
return fmt.Errorf("source is not a directory")
}

_, err = os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return err
}

err = os.MkdirAll(dst, si.Mode())
if err != nil {
return err
}

entries, err := os.ReadDir(src)
if err != nil {
return err
}

for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())

if entry.IsDir() {
err = CopyDirectory(srcPath, dstPath)
if err != nil {
return err
}
} else {
// 跳过符号链接
if entry.Type()&os.ModeSymlink != 0 {
continue
}

err = CopyFile(srcPath, dstPath)
if err != nil {
return err
}
}
}

return nil
}

// CopyFile 复制单个文件
func CopyFile(src, dst string) error {
sourceFileStat, err := os.Stat(src)
if err != nil {
return err
}

if !sourceFileStat.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", src)
}

source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()

destination, err := os.Create(dst)
if err != nil {
return err
}
defer destination.Close()

_, err = io.Copy(destination, source)
if err != nil {
return err
}

return os.Chmod(dst, sourceFileStat.Mode())
}
Loading