ioutil.WriteFile失败,并显示“无此文件或目录”

时间:2020-07-27 15:04:29

标签: go

我正在解决一个问题,我无法理解为什么ioutils.WriteFile返回no such file or directory错误。

完整错误:

Error open /data/cdrs/cv2/swoffnet/alliance/NV100708.cdr: no such file or directory

存在alliance目录,但WriteFile无法创建文件,这令人困惑,因为查看https://golang.org/src/io/ioutil/ioutil.go?s=2549:2617#L81 API,它是 如果不存在,它将创建一个文件;如果存在,则会截断文件。

ls  /data/cdrs/cv2/swoffnet/alliance
M5V00722.cdr      NV100715.cdr      NV100720.cdr      NV110719.cdr      NVX00718.cdr
M5V00722.cdr.old  NV100718.cdr      NV110715.cdr      NV110720.cdr      NVX00719.cdr
NV100708.cdr      NV100719.cdr      NV110718.cdr      NVX00715.cdr      NVX00720.cdr

对目录的权限也很好

bash-5.0# ls -l /data/
total 0
drwxrwxrwx    2 1000     1000             0 May 18 12:37 cdrs

bash-5.0# ls -l /data/cdrs/
total 0
drwxrwxrwx    2 1000     1000             0 Jun 30 04:30 cv2
drwxrwxrwx    2 1000     1000             0 Jul 14 06:11 orcawave
-rwxrwxrwx    1 1000     1000             0 Jul 27 13:05 pprof.pprof
drwxrwxrwx    2 1000     1000             0 Jun  8 10:43 sansay


bash-5.0# ls -l /data/cdrs/cv2/
total 0
drwxrwxrwx    2 1000     1000             0 Jul 10 12:32 neoffnet
drwxrwxrwx    2 1000     1000             0 Jun 30 04:30 swoffnet
bash-5.0# ls -l /data/cdrs/cv2/swoffnet/
total 0
drwxrwxrwx    2 1000     1000             0 Jun 30 04:30 alliance

bash-5.0# cd alliance/
bash-5.0# ls -al
total 78944
drwxrwxrwx    2 1000     1000             0 Jun 30 04:30 .
drwxrwxrwx    2 1000     1000             0 Jun 30 04:30 ..
-rwxrwxrwx    1 1000     1000       1760174 Jul 27 13:04 M5V00722.cdr
-rwxrwxrwx    1 1000     1000       1760174 Jul 27 09:24 M5V00722.cdr.old
-rwxrwxrwx    1 1000     1000             0 Jul  9 14:28 NV100708.cdr
-rwxrwxrwx    1 1000     1000      29045104 Jul 15 16:58 NV100715.cdr
-rwxrwxrwx    1 1000     1000      27711200 Jul 20 15:01 NV100718.cdr
-rwxrwxrwx    1 1000     1000      11821920 Jul 20 15:02 NV100719.cdr
-rwxrwxrwx    1 1000     1000       8388160 Jul 20 15:03 NV100720.cdr
-rwxrwxrwx    1 1000     1000          2464 Jul 15 17:11 NV110715.cdr
-rwxrwxrwx    1 1000     1000          1408 Jul 20 15:03 NV110718.cdr
-rwxrwxrwx    1 1000     1000          1408 Jul 20 15:03 NV110719.cdr
-rwxrwxrwx    1 1000     1000           704 Jul 20 15:03 NV110720.cdr
-rwxrwxrwx    1 1000     1000        136224 Jul 15 16:58 NVX00715.cdr
-rwxrwxrwx    1 1000     1000        125840 Jul 20 15:03 NVX00718.cdr
-rwxrwxrwx    1 1000     1000         40128 Jul 20 15:16 NVX00719.cdr
-rwxrwxrwx    1 1000     1000         40128 Jul 20 15:16 NVX00720.cdr

也没有空间问题

bash-5.0# df -h
Filesystem                Size      Used Available Use% Mounted on
overlay                 123.9G     18.0G    105.8G  15% /
tmpfs                    64.0M         0     64.0M   0% /dev
tmpfs                     7.8G         0      7.8G   0% /sys/fs/cgroup
//mountedarc.file.core.windows.net/cdr-archive-rawcdr-files
                          1.0T    330.1G    693.9G  32% /data/cdrs

实际文件大小也不大

-rw-rw---- 1 NV1 NV1     31608720 Jul 08 10:55 NV100708.cdr

实际代码如下

func (f *FTP) Copy(source, destination string, fileInfo os.FileInfo) error {

    log.Debug("Starting copy of file from FTP")
    file := fileInfo.(ftpFile)
    fullPath := source + file.Name()
    log.Debugf("fullPath %q:", fullPath)
    ftpResponse, err := f.Client.Retr(fullPath)
    if err != nil {
        return err
    }

    defer ftpResponse.Close()

    buf, err := ioutil.ReadAll(ftpResponse)
    if err != nil {
        return err
    }
    storeFilePath := fmt.Sprintf("%s%s", destination, file.Name())
    log.Debugf("storeFilePath %q:", storeFilePath)

    err = ioutil.WriteFile(storeFilePath, buf, 0644)
    if err != nil {
        if _, err2 := os.Stat(destination); os.IsNotExist(err2) {
            log.Debugf("%q Folder does not exist.", destination)
        } else {
            log.Debugf("%q Folder Exist.", destination)
        }

        log.Debugf("ioutils.WriteFile error %+v:", err)
        return err
    }

    return nil
}

使用%q选项进行更新,如评论中所述。

日志输出

{"level":"debug","msg":"Starting copy of file from FTP","time":"2020-07-27T15:45:13Z"}
{"level":"debug","msg":"fullPath \"/Download/NV100708.cdr\":","time":"2020-07-27T15:45:13Z"}
{"level":"debug","msg":"storeFilePath \"/data/cdrs/cv2/swoffnet/alliance/NV100708.cdr\":","time":"2020-07-27T15:47:47Z"}
{"level":"debug","msg":"\"/data/cdrs/cv2/swoffnet/alliance/\" Folder Exist.","time":"2020-07-27T15:47:47Z"}
{"level":"debug","msg":"ioutils.WriteFile error open /data/cdrs/cv2/swoffnet/alliance/NV100708.cdr: no such file or directory:","time":"2020-07-27T15:47:47Z"}
{"level":"error","msg":"Error open /data/cdrs/cv2/swoffnet/alliance/NV100708.cdr: no such file or directory","time":"2020-07-27T15:47:47Z"}
bash-5.0# mount | grep cdrs

//mountedarc.file.core.windows.net/cdr-archive-rawcdr-files on /data/cdrs type cifs (rw,relatime,vers=3.0,cache=strict,username=cdruser,domain=,uid=1000,forceuid,gid=1000,forcegid,addr=xxx.xxx.xxx.xxx,file_mode=0777,dir_mode=0777,soft,persistenthandles,nounix,serverino,mapposix,nobrl,mfsymlinks,rsize=1048576,wsize=1048576,echo_interval=60,actimeo=1)

strace输出

[pid    12] 07:16:21.065345 write(1, "{\"level\":\"debug\",\"msg\":\"storeFilePath \\\"/data/cdrs/cv2/swoffnet/alliance/NV100708.cdr\\\":\",\"time\":\"2020-07-28T07:16:21Z\"}\n", 121 <unfinished ...>
[pid     6] 07:16:21.065402 nanosleep({tv_sec=0, tv_nsec=20000},  <unfinished ...>
[pid    12] 07:16:21.065746 <... write resumed>) = 121
[pid     6] 07:16:21.065878 <... nanosleep resumed>NULL) = 0
[pid    12] 07:16:21.065915 openat(AT_FDCWD, "/data/cdrs/cv2/swoffnet/alliance/NV100708.cdr", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644 <unfinished ...>
[pid     6] 07:16:21.065987 nanosleep({tv_sec=0, tv_nsec=20000}, NULL) = 0
[pid     6] 07:16:21.066195 nanosleep({tv_sec=0, tv_nsec=20000}, NULL) = 0
[pid     6] 07:16:21.066401 nanosleep({tv_sec=0, tv_nsec=20000}, NULL) = 0
[pid     6] 07:16:21.066574 nanosleep({tv_sec=0, tv_nsec=20000}, NULL) = 0
[pid     6] 07:16:21.066831 nanosleep({tv_sec=0, tv_nsec=20000}, NULL) = 0
[pid     6] 07:16:21.067228 nanosleep({tv_sec=0, tv_nsec=20000}, NULL) = 0
[pid     6] 07:16:21.067538 nanosleep({tv_sec=0, tv_nsec=20000}, NULL) = 0
[pid     6] 07:16:21.067832 nanosleep({tv_sec=0, tv_nsec=20000}, NULL) = 0
[pid     6] 07:16:21.068123 nanosleep({tv_sec=0, tv_nsec=20000}, NULL) = 0
[pid     6] 07:16:21.068501 nanosleep({tv_sec=0, tv_nsec=20000}, NULL) = 0
[pid     6] 07:16:21.068884 nanosleep({tv_sec=0, tv_nsec=20000}, NULL) = 0
[pid     6] 07:16:21.077390 epoll_pwait(3, [{EPOLLIN|EPOLLOUT, {u32=112086064, u64=140673175931952}}], 128, 0, NULL, 1) = 1
[pid     6] 07:16:21.077554 futex(0xc000217648, FUTEX_WAKE_PRIVATE, 1 <unfinished ...>
[pid    11] 07:16:21.077645 <... futex resumed>) = 0
[pid     6] 07:16:21.077709 <... futex resumed>) = 1
[pid    11] 07:16:21.077777 epoll_pwait(3,  <unfinished ...>
[pid     6] 07:16:21.077828 nanosleep({tv_sec=0, tv_nsec=20000},  <unfinished ...>
[pid    11] 07:16:21.078793 <... epoll_pwait resumed>[], 128, 0, NULL, 1) = 0
[pid     6] 07:16:21.078869 <... nanosleep resumed>NULL) = 0
[pid    11] 07:16:21.078898 epoll_pwait(3,  <unfinished ...>
[pid     6] 07:16:21.078939 futex(0x14927f8, FUTEX_WAIT_PRIVATE, 0, {tv_sec=2, tv_nsec=803346940} <unfinished ...>
[pid    12] 07:16:21.215531 <... openat resumed>) = -1 ENOENT (No such file or directory)
[pid    12] 07:16:21.215620 futex(0x14927f8, FUTEX_WAKE_PRIVATE, 1) = 1
[pid     6] 07:16:21.215870 <... futex resumed>) = 0
[pid    12] 07:16:21.215906 newfstatat(AT_FDCWD, "/data/cdrs/cv2/swoffnet/alliance/",  <unfinished ...>
[pid     6] 07:16:21.215981 nanosleep({tv_sec=0, tv_nsec=20000},  <unfinished ...>
[pid    12] 07:16:21.216031 <... newfstatat resumed>{st_mode=S_IFDIR|0777, st_size=0, ...}, 0) = 0
[pid     6] 07:16:21.216162 <... nanosleep resumed>NULL) = 0
[pid    12] 07:16:21.216214 write(1, "{\"level\":\"info\",\"msg\":\"\\\"/data/cdrs/cv2/swoffnet/alliance/\\\" Folder Exist \\u003cnil\\u003e\",\"time\":\"2020-07-28T07:16:21Z\"}\n", 122 <unfinished ...>
[pid     6] 07:16:21.216272 nanosleep({tv_sec=0, tv_nsec=20000},  <unfinished ...>
[pid    12] 07:16:21.216312 <... write resumed>) = 122
[pid    12] 07:16:21.216410 write(1, "{\"level\":\"debug\",\"msg\":\"ioutils.WriteFile error open /data/cdrs/cv2/swoffnet/alliance/NV100708.cdr: no such file or directory:\",\"time\":\"2020-07-28T07:16:21Z\"}\n", 159 <unfinished ...>

仅使用-e openat

的strace输出
[pid    12] 07:28:56.028250 openat(AT_FDCWD, "/proc/stat", O_RDONLY|O_CLOEXEC) = 12
[pid    12] 07:28:56.029908 openat(AT_FDCWD, "/proc/1/fd", O_RDONLY|O_CLOEXEC) = 12
[pid    12] 07:28:56.030561 openat(AT_FDCWD, "/proc/1/limits", O_RDONLY|O_CLOEXEC) = 12
[pid    12] 07:29:01.026271 openat(AT_FDCWD, "/proc/1/stat", O_RDONLY|O_CLOEXEC) = 12
[pid    12] 07:29:01.027840 openat(AT_FDCWD, "/proc/stat", O_RDONLY|O_CLOEXEC) = 12
[pid    12] 07:29:01.029306 openat(AT_FDCWD, "/proc/1/fd", O_RDONLY|O_CLOEXEC) = 12
[pid    12] 07:29:01.030200 openat(AT_FDCWD, "/proc/1/limits", O_RDONLY|O_CLOEXEC) = 12
[pid     7] 07:29:03.102419 openat(AT_FDCWD, "/data/cdrs/cv2/swoffnet/alliance/NV100708.cdr", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644) = -1 ENOENT (No such file or directory)
[pid     7] 07:29:03.168763 --- SIGURG {si_signo=SIGURG, si_code=SI_TKILL, si_pid=1, si_uid=0} ---
[pid    11] 07:29:06.026587 openat(AT_FDCWD, "/proc/1/stat", O_RDONLY|O_CLOEXEC) = 9
[pid    11] 07:29:06.027934 --- SIGURG {si_signo=SIGURG, si_code=SI_TKILL, si_pid=1, si_uid=0} ---
[pid    12] 07:29:06.029353 openat(AT_FDCWD, "/proc/stat", O_RDONLY|O_CLOEXEC) = 9
[pid    12] 07:29:06.031111 openat(AT_FDCWD, "/proc/1/fd", O_RDONLY|O_CLOEXEC) = 9
[pid    12] 07:29:06.032023 openat(AT_FDCWD, "/proc/1/limits", O_RDONLY|O_CLOEXEC) = 9
[pid    11] 07:29:08.940638 --- SIGURG {si_signo=SIGURG, si_code=SI_TKILL, si_pid=1, si_uid=0} ---
[pid     7] 07:29:11.032103 openat(AT_FDCWD, "/proc/1/stat", O_RDONLY|O_CLOEXEC) = 9
[pid     7] 07:29:11.035680 openat(AT_FDCWD, "/proc/stat", O_RDONLY|O_CLOEXEC) = 9
[pid     7] 07:29:11.038118 openat(AT_FDCWD, "/proc/1/fd", O_RDONLY|O_CLOEXEC) = 9
[pid     7] 07:29:11.038533 openat(AT_FDCWD, "/proc/1/limits", O_RDONLY|O_CLOEXEC) = 9
[pid    13] 07:29:16.026302 openat(AT_FDCWD, "/proc/1/stat", O_RDONLY|O_CLOEXEC) = 9
[pid    13] 07:29:16.027579 openat(AT_FDCWD, "/proc/stat", O_RDONLY|O_CLOEXEC) = 9
[pid    13] 07:29:16.029206 openat(AT_FDCWD, "/proc/1/fd", O_RDONLY|O_CLOEXEC) = 9
[pid    13] 07:29:16.030201 openat(AT_FDCWD, "/proc/1/limits", O_RDONLY|O_CLOEXEC) = 9
[pid     7] 07:29:18.941127 --- SIGURG {si_signo=SIGURG, si_code=SI_TKILL, si_pid=1, si_uid=0} ---
[pid    13] 07:29:21.026297 openat(AT_FDCWD, "/proc/1/stat", O_RDONLY|O_CLOEXEC) = 9
[pid     7] 07:29:21.027728 openat(AT_FDCWD, "/proc/stat", O_RDONLY|O_CLOEXEC) = 9
[pid     7] 07:29:21.029307 openat(AT_FDCWD, "/proc/1/fd", O_RDONLY|O_CLOEXEC) = 9
[pid     7] 07:29:21.030110 openat(AT_FDCWD, "/proc/1/limits", O_RDONLY|O_CLOEXEC) = 9

最后运行了

strace echo test data > NV100708.cdr
bash: NV100708.cdr: No such file or directory
bash-5.0# strace echo test data > XFILE00181.cdr
execve("/bin/echo", ["echo", "test", "data"], 0x7ffc45798400 /* 108 vars */) = 0
arch_prctl(ARCH_SET_FS, 0x7fd1f5bf3d48) = 0
set_tid_address(0x7fd1f5bf431c)         = 155
mprotect(0x7fd1f5bf0000, 4096, PROT_READ) = 0
mprotect(0x564812b28000, 16384, PROT_READ) = 0
getuid()                                = 0
write(1, "test data\n", 10)             = 10
exit_group(0)                           = ?
+++ exited with 0 +++

似乎问题仅在于NV100708.cdr文件名。也

strace echo test data > NV100708.txt ## success
strace echo test data > NV100708.c ## sucesss
strace echo test data > NV100708.cd ## success
strace echo test data > NV100708.cdr ## error
strace echo test data > NV100708.cdrs ## success

编写基本脚本(https://pastebin.com/PMpStsB9)以将名称和符文切片一起打印

M5V00722.cdr  -- > [77 53 86 48 48 55 50 50 46 99 100 114]
M5V00722.cdr.old  -- > [77 53 86 48 48 55 50 50 46 99 100 114 46 111 108 100]
M5V00725.cdr  -- > [77 53 86 48 48 55 50 53 46 99 100 114]
M5V00726.cdr  -- > [77 53 86 48 48 55 50 54 46 99 100 114]
M5V00727.cdr  -- > [77 53 86 48 48 55 50 55 46 99 100 114]
NV100708.cdr  -- > [78 86 49 48 48 55 48 56 46 99 100 114]
NV100715.cdr  -- > [78 86 49 48 48 55 49 53 46 99 100 114]
NV100718.cdr  -- > [78 86 49 48 48 55 49 56 46 99 100 114]
NV100719.cdr  -- > [78 86 49 48 48 55 49 57 46 99 100 114]
NV100720.cdr  -- > [78 86 49 48 48 55 50 48 46 99 100 114]
NV100725.cdr  -- > [78 86 49 48 48 55 50 53 46 99 100 114]
NV100726.cdr  -- > [78 86 49 48 48 55 50 54 46 99 100 114]
NV100727.cdr  -- > [78 86 49 48 48 55 50 55 46 99 100 114]
NV110715.cdr  -- > [78 86 49 49 48 55 49 53 46 99 100 114]
NV110718.cdr  -- > [78 86 49 49 48 55 49 56 46 99 100 114]
NV110719.cdr  -- > [78 86 49 49 48 55 49 57 46 99 100 114]
NV110720.cdr  -- > [78 86 49 49 48 55 50 48 46 99 100 114]
NV110725.cdr  -- > [78 86 49 49 48 55 50 53 46 99 100 114]
NV110726.cdr  -- > [78 86 49 49 48 55 50 54 46 99 100 114]
NV110727.cdr  -- > [78 86 49 49 48 55 50 55 46 99 100 114]
NVX00715.cdr  -- > [78 86 88 48 48 55 49 53 46 99 100 114]
NVX00718.cdr  -- > [78 86 88 48 48 55 49 56 46 99 100 114]
NVX00719.cdr  -- > [78 86 88 48 48 55 49 57 46 99 100 114]
NVX00720.cdr  -- > [78 86 88 48 48 55 50 48 46 99 100 114]
NVX00725.cdr  -- > [78 86 88 48 48 55 50 53 46 99 100 114]
NVX00726.cdr  -- > [78 86 88 48 48 55 50 54 46 99 100 114]
NVX00727.cdr  -- > [78 86 88 48 48 55 50 55 46 99 100 114]
XFILE00181.cdr  -- > [88 70 73 76 69 48 48 49 56 49 46 99 100 114]

文件NV100708.cdr存在,并且文件名中也没有特殊字符

另请参阅Unable to delete a stubborn file

0 个答案:

没有答案