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

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

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

问题

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

  1. package cmd
  2. import (
  3. "bufio"
  4. "fmt"
  5. "net"
  6. "os"
  7. "github.com/spf13/cobra"
  8. "wpgenius.io/util"
  9. )
  10. var cachePurgerCmd = &cobra.Command{
  11. Use: "cache-purger",
  12. Short: "Listen for request to purge NGINX page cache",
  13. Run: func(cmd *cobra.Command, args []string) {
  14. dstream, err := net.Listen("tcp", ":9876")
  15. if err != nil {
  16. util.HandleError(err, "Can not start listener..")
  17. return
  18. }
  19. fmt.Println("Listening for purge requests...")
  20. defer dstream.Close()
  21. for {
  22. con, err := dstream.Accept()
  23. if err != nil {
  24. util.HandleError(err, "Can not accept connection")
  25. os.Exit(1)
  26. }
  27. go handleRequest(con)
  28. }
  29. },
  30. }
  31. func handleRequest(con net.Conn) {
  32. path, err := bufio.NewReader(con).ReadString('\n')
  33. if err != nil {
  34. return
  35. }
  36. defer con.Close()
  37. err = os.Remove(path)
  38. if err != nil {
  39. con.Write([]byte("ERROR"))
  40. fmt.Println(err)
  41. util.HandleError(err, "Can not delete cache file")
  42. return
  43. }
  44. con.Write([]byte("SUCCESS"))
  45. }
  46. func init() {
  47. rootCmd.AddCommand(cachePurgerCmd)
  48. }

尽管文件/目录存在,但仍然出现“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

  1. package cmd
  2. import (
  3. "bufio"
  4. "fmt"
  5. "net"
  6. "os"
  7. "github.com/spf13/cobra"
  8. "wpgenius.io/util"
  9. )
  10. var cachePurgerCmd = &cobra.Command{
  11. Use: "cache-purger",
  12. Short: "Listen for request to purge NGINX page cache",
  13. Run: func(cmd *cobra.Command, args []string) {
  14. dstream, err := net.Listen("tcp", ":9876")
  15. if err != nil {
  16. util.HandleError(err, "Can not start listener..")
  17. return
  18. }
  19. fmt.Println("Listening for purge requests...")
  20. defer dstream.Close()
  21. for {
  22. con, err := dstream.Accept()
  23. if err != nil {
  24. util.HandleError(err, "Can not accept connection")
  25. os.Exit(1)
  26. }
  27. go handleRequest(con)
  28. }
  29. },
  30. }
  31. func handleRequest(con net.Conn) {
  32. path, err := bufio.NewReader(con).ReadString('\n')
  33. if err != nil {
  34. return
  35. }
  36. defer con.Close()
  37. err = os.Remove(path)
  38. if err != nil {
  39. con.Write([]byte("ERROR"))
  40. fmt.Println(err)
  41. util.HandleError(err, "Can not delete cache file")
  42. return
  43. }
  44. con.Write([]byte("SUCCESS"))
  45. }
  46. func init() {
  47. rootCmd.AddCommand(cachePurgerCmd)
  48. }

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:

确定