Skip to content

Commit

Permalink
Merge pull request #10 from kha7iq/fix-index-outofbound-issue
Browse files Browse the repository at this point in the history
fix: index outofbound issue
  • Loading branch information
kha7iq authored May 24, 2023
2 parents 1415991 + c26c5f0 commit 8e3f1eb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
5 changes: 4 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ checksum:
changelog:
sort: 'asc'
filters:
exclude: ['^docs:', '^test:', 'Merge pull request', 'Merge branch', 'go mod tidy']
exclude: ['^docs:', '^chore:', '^test:', 'Merge pull request', 'Merge branch', 'go mod tidy']

archives:
- name_template: >-
Expand Down Expand Up @@ -90,6 +90,9 @@ aurs:
license: MIT
private_key: '{{ .Env.AUR_KEY }}'
git_url: 'ssh://[email protected]/ncp-bin.git'
commit_author:
name: Abdul Khaliq
email: [email protected]
package: |-
# bin
install -Dm755 "./ncp" "${pkgdir}/usr/bin/ncp"
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ NCP is a file transfer utility that enables efficient copying of files to and fr

## Features

- Easy file transfer to and from an NFS server
- Easy upload transfer to and from an NFS server
- Support for upload and download operations
- Multi-architecture binaries available for installation (e.g deb, apk, rpm, exe)
- Compatible with Windows and macOS operating systems
- Option to specify UID and GID for write operations using a global flag
- Display upload and download speeds in real-time.
- Show the elapsed time for write operations.
- Show total file size
- Display upload and download speeds, file size and elapsed time for write operations.

<img alt="NCP" src="./.github/img/ncp.gif" width="800" />

Expand Down
39 changes: 20 additions & 19 deletions cmd/from/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func FromServer() *cli.Command {
uid, gid := helper.CheckUID(u, g)

rootDir := filepath.Dir(nc.nfsMountFolder)
dir := filepath.Base(nc.nfsMountFolder)
basePath := filepath.Base(nc.nfsMountFolder)

mount, err := nfs.DialMount(nc.nfsHost, false)
if err != nil {
Expand All @@ -72,28 +72,28 @@ func FromServer() *cli.Command {
}

mount.Close()
if isDirectory(nfs, basePath) {

if isDirectory(nfs, dir) {

dirs, files, err := listFilesAndFolders(nfs, dir)
dirs, files, err := listFilesAndFolders(nfs, basePath)
if err != nil {
log.Fatalf("unable to get list of files and folders %v", err)
}
if len(dirs) == 0 {
dirs = append(dirs, basePath)
}
for _, v := range dirs {
if err = createDirIfNotExist(v); err != nil {
log.Fatalf("fail to create folder %V", err)
}
}

for _, sf := range files {
if err = transferFile(nfs, sf, sf, truncate); err != nil {
log.Fatalf("fail to copy files with error %V", err)
}
}
}
if !isDirectory(nfs, dir) {

if err = transferFile(nfs, dir, dir, truncate); err != nil {
if !isDirectory(nfs, basePath) {
if err = transferFile(nfs, basePath, basePath, truncate); err != nil {
log.Fatalf("fail to transfer files %v", err)
}
}
Expand Down Expand Up @@ -134,9 +134,9 @@ func transferFile(nfs *nfs.Target, srcfile string, targetfile string, truncate b
defer wr.Close()

// Copy files with progress size
n, err := io.CopyN(wr, io.TeeReader(t, progress), size)
wrBytes, err := io.CopyN(wr, io.TeeReader(t, progress), size)
if err != nil {
log.Fatalf("error copying: n=%d, %s", n, err.Error())
log.Fatalf("error copying file: written bytes=%d, %s", wrBytes, err.Error())
return err
}
expectedSum := h.Sum(nil)
Expand Down Expand Up @@ -205,16 +205,17 @@ func listFilesAndFolders(v *nfs.Target, dir string) ([]string, []string, error)
return dirs, files, nil
}

// isDirectory takes a path strings and check the attributes if givin path
// is a dirctory or not
// isDirectory takes a path string and checks if the given path is a directory or not on NFS server
func isDirectory(v *nfs.Target, dir string) bool {
outDirs, _ := v.ReadDirPlus(dir)
for _, outDir := range outDirs {
if outDir.Name() != "." && outDir.Name() != ".." {
if outDir.IsDir() {
return true
}
}
// Get the attributes of the path
attr, _, err := v.GetAttr(dir)
if err != nil {
log.Println("error getting directory attributes:", err)
return false
}
if attr.IsDir() {
return true
}

return false
}
20 changes: 10 additions & 10 deletions cmd/to/to.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,6 @@ func ToServer() *cli.Command {
}
}

// isDirectory takes a path strings and check the attributes if givin path
// is a dirctory or not
func isDirectory(path string) (bool, error) {
info, err := os.Stat(path)
if err != nil {
return false, err
}
return info.IsDir(), nil
}

// listFileAndFolders take a directory path and returns a slice containng files and another containing folders
func getFoldersAndFiles(path string, basePath string) ([]string, []string, error) {
var folders []string
Expand Down Expand Up @@ -232,3 +222,13 @@ func transferFile(nfs *nfs.Target, srcfile string, targetfile string, turnicatio
progress.Finish()
return nil
}

// isDirectory takes a path strings and check the attributes if givin path
// is a dirctory or not
func isDirectory(path string) (bool, error) {
info, err := os.Stat(path)
if err != nil {
return false, err
}
return info.IsDir(), nil
}

0 comments on commit 8e3f1eb

Please sign in to comment.