Skip to content

Commit

Permalink
update download
Browse files Browse the repository at this point in the history
  • Loading branch information
iikira committed Oct 26, 2019
1 parent 8d368fc commit 3585806
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 37 deletions.
50 changes: 24 additions & 26 deletions internal/pcscommand/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/iikira/BaiduPCS-Go/pcsutil/waitgroup"
"github.com/iikira/BaiduPCS-Go/requester"
"github.com/iikira/BaiduPCS-Go/requester/downloader"
"github.com/iikira/BaiduPCS-Go/requester/downloader/prealloc"
"github.com/oleiade/lane"
"io"
"net/http"
Expand Down Expand Up @@ -91,10 +90,10 @@ func downloadPrintFormat(load int) string {

func download(id int, downloadURL, savePath string, loadBalansers []string, client *requester.HTTPClient, newCfg downloader.Config, downloadOptions *DownloadOptions) error {
var (
file *os.File
fileItf io.WriterAt //空接口和空指针不等价
err error
exitChan chan struct{}
writer downloader.Writer
file *os.File
warn, err error
exitChan chan struct{}
)

if !newCfg.IsTest {
Expand All @@ -112,24 +111,18 @@ func download(id int, downloadURL, savePath string, loadBalansers []string, clie
return fmt.Errorf("%s, path %s: not a directory", StrDownloadInitError, dir)
}

// 初始化权限(Windows)
warn := prealloc.InitPrivilege()
// 打开文件
writer, file, warn, err = downloader.NewDownloaderWriterByFilename(savePath, os.O_CREATE|os.O_WRONLY, 0666)
if warn != nil {
fmt.Fprintf(downloadOptions.Out, "warn: %s\n", warn)
}

// 打开文件
file, err = os.OpenFile(savePath, os.O_CREATE|os.O_WRONLY, 0666)
if file != nil {
defer file.Close()
}
if err != nil {
return fmt.Errorf("%s, %s", StrDownloadInitError, err)
}
fileItf = file
defer file.Close()
}

download := downloader.NewDownloader(downloadURL, fileItf, &newCfg)
download := downloader.NewDownloader(downloadURL, writer, &newCfg)
download.SetClient(client)
download.TryHTTP(!pcsconfig.Config.EnableHTTPS())
download.AddLoadBalanceServer(loadBalansers...)
Expand Down Expand Up @@ -194,24 +187,29 @@ func download(id int, downloadURL, savePath string, loadBalansers []string, clie
close(exitChan)
fmt.Fprintf(downloadOptions.Out, "\n")
if err != nil {
// 下载失败, 删去空文件
if info, infoErr := file.Stat(); infoErr == nil {
if info.Size() == 0 {
pcsCommandVerbose.Infof("[%d] remove empty file: %s\n", id, savePath)
os.Remove(savePath)
if !newCfg.IsTest {
// 下载失败, 删去空文件
if info, infoErr := file.Stat(); infoErr == nil {
if info.Size() == 0 {
pcsCommandVerbose.Infof("[%d] remove empty file: %s\n", id, savePath)
removeErr := os.Remove(savePath)
if removeErr != nil {
pcsCommandVerbose.Infof("[%d] remove file error: %s\n", id, removeErr)
}
}
}
}
return err
}

if downloadOptions.IsExecutedPermission {
err = file.Chmod(0766)
if err != nil {
fmt.Fprintf(downloadOptions.Out, "[%d] 警告, 加执行权限错误: %s\n", id, err)
if !newCfg.IsTest {
if downloadOptions.IsExecutedPermission {
err = file.Chmod(0766)
if err != nil {
fmt.Fprintf(downloadOptions.Out, "[%d] 警告, 加执行权限错误: %s\n", id, err)
}
}
}

if !newCfg.IsTest {
fmt.Fprintf(downloadOptions.Out, "[%d] 下载完成, 保存位置: %s\n", id, savePath)
} else {
fmt.Fprintf(downloadOptions.Out, "[%d] 测试下载结束\n", id)
Expand Down
4 changes: 0 additions & 4 deletions requester/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ type (
monitor *Monitor
instanceState *InstanceState
}

Fder interface {
Fd() uintptr
}
)

//NewDownloader 初始化Downloader
Expand Down
14 changes: 7 additions & 7 deletions requester/downloader/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ package downloader
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/requester/downloader/prealloc"
"io"
"os"
)

// DoDownload 执行下载
func DoDownload(durl string, savePath string, cfg *Config) {
var (
file *os.File
err error
file *os.File
writer io.WriterAt
warn, err error
)

if savePath != "" {
warn := prealloc.InitPrivilege()
writer, file, warn, err = NewDownloaderWriterByFilename(savePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
if warn != nil {
fmt.Printf("warn: %s\n", warn)
}

file, err = os.OpenFile(savePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
}

download := NewDownloader(durl, file, cfg)
download := NewDownloader(durl, writer, cfg)

exitDownloadFunc := make(chan struct{})

Expand Down
17 changes: 17 additions & 0 deletions requester/downloader/prealloc/prealloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,27 @@
// Package prealloc 初始化分配文件包
package prealloc

import (
"os"
)

func InitPrivilege() (err error) {
return nil
}

func PreAlloc(fd uintptr, length int64) error {
file := os.NewFile(fd, "truncfile")
err := file.Truncate(length)
if err != nil {
perr, ok := err.(*os.PathError)
if !ok {
return err
}

return &PreAllocError{
ProcName: perr.Op,
Err: perr.Err,
}
}
return nil
}
28 changes: 28 additions & 0 deletions requester/downloader/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package downloader

import (
"github.com/iikira/BaiduPCS-Go/requester/downloader/prealloc"
"io"
"os"
)

type (
Fder interface {
Fd() uintptr
}

Writer interface {
io.WriterAt
}
)

func NewDownloaderWriterByFilename(name string, flag int, perm os.FileMode) (writer Writer, file *os.File, warn error, err error) {
warn = prealloc.InitPrivilege()
file, err = os.OpenFile(name, flag, perm)
if err != nil {
return
}

writer = file
return
}

0 comments on commit 3585806

Please sign in to comment.