在Go协程中使用`os.Remove`时出现了`no such file or directory`错误。

huangapple go评论138阅读模式
英文:

`no such file or directory` with `os.Remove` inside go routine

问题

我使用Cobra框架为我的CLI应用程序添加了一个新的命令。该命令用于启动一个接受socket连接的TCP服务器。它接收一个有效路径(absolute path)指向一个文件/目录,并尝试删除它。以下是该命令的代码:

package cmd

import (
	"bufio"
	"fmt"
	"net"
	"os"

	"github.com/spf13/cobra"
	"wpgenius.io/util"
)

var cachePurgerCmd = &cobra.Command{
	Use:   "cache-purger",
	Short: "Listen for request to purge NGINX page cache",
	Run: func(cmd *cobra.Command, args []string) {
		dstream, err := net.Listen("tcp", ":9876")

		if err != nil {
			util.HandleError(err, "Can not start listener..")
			return
		}

		fmt.Println("Listening for purge requests...")

		defer dstream.Close()

		for {
			con, err := dstream.Accept()

			if err != nil {
				util.HandleError(err, "Can not accept connection")
				os.Exit(1)
			}

			go handleRequest(con)
		}
	},
}

func handleRequest(con net.Conn) {
	path, err := bufio.NewReader(con).ReadString('\n')

	if err != nil {
		return
	}

	defer con.Close()

	err = os.Remove(path)

	if err != nil {
		con.Write([]byte("ERROR"))
		fmt.Println(err)
		util.HandleError(err, "Can not delete cache file")
		return
	}

	con.Write([]byte("SUCCESS"))
}

func init() {
	rootCmd.AddCommand(cachePurgerCmd)
}

尽管文件/目录存在,但仍然出现“no such file or directory”错误。我通过将os.Remove添加到main函数中进行了一次简单的检查,以确保问题不是与路径相关,并且我可以看到它成功删除了文件/目录。

我不确定这是否与go routingtcp server有关!

非常感谢您的帮助!

英文:

I added a new command to my CLI application using the Cobra framework. This command is supposed to start a TCP server that accepts socket connections. It receives a payload which is an absolute path to a file/directory and tries to delete it. Here is the code for the command

package cmd

import (
	"bufio"
	"fmt"
	"net"
	"os"

	"github.com/spf13/cobra"
	"wpgenius.io/util"
)

var cachePurgerCmd = &cobra.Command{
	Use:   "cache-purger",
	Short: "Listen for request to purge NGINX page cache",
	Run: func(cmd *cobra.Command, args []string) {
		dstream, err := net.Listen("tcp", ":9876")

		if err != nil {
			util.HandleError(err, "Can not start listener..")
			return
		}

		fmt.Println("Listening for purge requests...")

		defer dstream.Close()

		for {
			con, err := dstream.Accept()

			if err != nil {
				util.HandleError(err, "Can not accept connection")
				os.Exit(1)
			}

			go handleRequest(con)
		}
	},
}

func handleRequest(con net.Conn) {
	path, err := bufio.NewReader(con).ReadString('\n')

	if err != nil {
		return
	}

	defer con.Close()

	err = os.Remove(path)

	if err != nil {
		con.Write([]byte("ERROR"))
		fmt.Println(err)
		util.HandleError(err, "Can not delete cache file")
		return
	}

	con.Write([]byte("SUCCESS"))
}

func init() {
	rootCmd.AddCommand(cachePurgerCmd)
}

Although the file/directory exists, I still get no such file or directory error.
I did a sanity check by simply adding the os.Remove to the main function to make sure it's not related to the path and I can see it successfully delete the file/directory.

I'm not sure if it has something to do with go routing or with the tcp server!

Any help will be highly appreciated!

答案1

得分: 0

我猜测你的意思是,在你输入的路径中,关键点在于...

英文:

I guess the point is \n in the path you input.

huangapple
  • 本文由 发表于 2022年5月23日 07:24:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/72341938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定