Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
dtn-tool exchange: fix relative path detection
Browse files Browse the repository at this point in the history
Previously, the code assumed that it will always be run from the current
working directory. Of course, this is not always the case. Thus, this
closes #38.
  • Loading branch information
oxzi committed Jun 17, 2021
1 parent ce8acbb commit b751261
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions cmd/dtn-tool/exchange.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020 Alvar Penning
// SPDX-FileCopyrightText: 2020, 2021 Alvar Penning
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -33,16 +33,6 @@ type exchange struct {
bundleReadChan chan bpv7.Bundle
}

func cleanFilepath(f string) string {
const basepath = "."
if rel, err := filepath.Rel(basepath, f); err != nil {
log.WithField("path", f).WithError(err).Fatal("Failed to clean file path")
return ""
} else {
return rel
}
}

// startExchange to exchange Bundles between client and a dtnd.
func startExchange(args []string) {
if len(args) != 3 {
Expand Down Expand Up @@ -80,6 +70,16 @@ func startExchange(args []string) {
ex.handler()
}

// cleanFilepath creates a relative path from the initial path to a new file's path.
func (ex *exchange) cleanFilepath(f string) string {
if rel, err := filepath.Rel(ex.directory, f); err != nil {
log.WithField("path", f).WithError(err).Fatal("Failed to clean file path")
return ""
} else {
return rel
}
}

func (ex *exchange) handler() {
defer func() {
_ = ex.watcher.Close()
Expand All @@ -98,7 +98,7 @@ func (ex *exchange) handler() {
return
}

if _, ok := ex.knownFiles.Load(cleanFilepath(e.Name)); ok {
if _, ok := ex.knownFiles.Load(ex.cleanFilepath(e.Name)); ok {
log.WithField("file", e.Name).Debug("Skipping file; already known")
continue
}
Expand Down Expand Up @@ -128,13 +128,13 @@ func (ex *exchange) handler() {
return
}

filepath := path.Join(ex.directory, hex.EncodeToString([]byte(b.ID().String())))
filePath := path.Join(ex.directory, hex.EncodeToString([]byte(b.ID().String())))
logger := log.WithFields(log.Fields{
"bundle": b.ID(),
"file": filepath,
"file": filePath,
})

if f, err := os.Create(filepath); err != nil {
if f, err := os.Create(filePath); err != nil {
logger.WithError(err).Error("Creating file errored")
return
} else if err := b.MarshalCbor(f); err != nil {
Expand All @@ -143,7 +143,7 @@ func (ex *exchange) handler() {
logger.WithError(err).Error("Closing file errored")
}

ex.knownFiles.Store(cleanFilepath(filepath), struct{}{})
ex.knownFiles.Store(ex.cleanFilepath(filePath), struct{}{})

logger.Info("Saved received Bundle")
}
Expand Down

0 comments on commit b751261

Please sign in to comment.