为什么刷新 RandomAccessFile 总是失败?

问题描述 投票:0回答:1

我使用“rw”模式并希望将内容刷新到设备中:

RandomAccessFile random= new RandomAccessFile(path.toFile(), "rw");
random.write(1);

但是他们总是在刷新操作上失败:

//Force failed
random.getChannel().force(true);

//sync failed
random.getFD().sync();

没有详细原因:

sync failed
java.io.SyncFailedException: sync failed
    at java.io.FileDescriptor.sync(Native Method)

其他模式也会产生相同的错误:“

rwd
”、“
rws
”。为什么以及如何解决?

类似的问题:如何刷新“RandomAccessFile”(java)?.

在这个问题中,有人提到使用

getFD().sync()
方法,这显然对他们来说似乎是正确的。然而,对我来说,经过测试,
getFD().sync()
getChannel().force(true)
都始终抛出错误(尽管数据仍然可以正确写入设备)。我想知道这些方法是否总是失败或者我错过了什么。

java io randomaccessfile
1个回答
0
投票

sync()
方法是
native
方法,并且在Linux和Windows上:

  • 它直接映射到操作系统系统调用/Win32 库调用,并且
  • 未诊断操作系统的故障代码。

因此我们只能尝试通过排除过程来诊断问题。 在 Linux 中,

fsync(2)
手动条目列出了以下可能的错误代码:

   EBADF  fd is not a valid open file descriptor.

   EIO    An error occurred during synchronization.  This error may relate
          to  data written to some other file descriptor on the same file.
          Since Linux 4.13, errors from write-back will be reported to all
          file  descriptors  that  might have written the data which trig‐
          gered the error.  Some filesystems (e.g., NFS) keep close  track
          of  which data came through which file descriptor, and give more
          precise reporting.  Other filesystems (e.g., most local filesys‐
          tems)  will report errors to all file descriptors that were open
          on the file when the error was recorded.

   ENOSPC Disk space was exhausted while synchronizing.

   EROFS, EINVAL
          fd is bound to a special file (e.g., a pipe,  FIFO,  or  socket)
          which does not support synchronization.

   ENOSPC, EDQUOT
          fd  is  bound  to a file on NFS or another filesystem which does
          not allocate space at the time of a write(2)  system  call,  and
          some previous write failed due to insufficient storage space.

我们可以打折 EBADF 和 EROFS。

EINVAL 会出现我假设您没有尝试在设备文件、管道、套接字、FIFO 或其他奇特的东西上进行随机访问 I/O! 如果文件系统驱动程序不支持

fsync
,您也可能会遇到此问题,但我不知道有任何这样的示例。

EOI 通常表示硬盘或 SSD 故障。 您应该看到其他证据。 如果与远程服务器通信出现问题等,您还可以为远程文件系统(例如 NFS)获取此信息。这将取决于文件系统挂载参数。

ENOSPC 表示文件系统已满。 您应该看到其他证据。

其中一些事情的证据将在系统日志、

df
输出等中。


总而言之,可能的原因有多种。 您需要做更多调查。

© www.soinside.com 2019 - 2024. All rights reserved.