gpt4 book ai didi

java - Java中解析/proc/mounts

转载 作者:行者123 更新时间:2023-12-02 00:07:18 28 4
gpt4 key购买 nike

我正在尝试解析在 Linux 环境中运行 cat/proc/mounts 的结果。

当挂载包含空格等特殊字符时,它们会被转义。这方面的一个例子是

/home/user/media\040/image.dd/media/dd ... 其中\040 实际上是一个空格。

我正在尝试在java中解析它,但不想编写手动解析器,因为我必须相信这已经完成并且比我做的更稳健。是否有一个类负责解码这个?我找到了一两个八进制解码器,但它们不适用于文本和八进制混合。

最佳答案

您可以在 C 中使用 getmntent 解析/proc/mounts。

您可以根据规则在 Java 中实现它:

空格 (\040)、制表符 (\011)、换行符 (\012) 和反斜杠 (\134)

或者无意味着交换分区。

男人得到:

mntent结构体定义如下:

       struct mntent {
char *mnt_fsname; /* name of mounted file system */
char *mnt_dir; /* file system path prefix */
char *mnt_type; /* mount type (see mntent.h) */
char *mnt_opts; /* mount options (see mntent.h) */
int mnt_freq; /* dump frequency in days */
int mnt_passno; /* pass number on parallel fsck */
};

Since fields in the mtab and fstab files are separated by whitespace, octal escapes are
used to represent the four characters space (\040), tab (\011), newline (\012) and back-
slash (\134) in those files when they occur in one of the four strings in a mntent struc-
ture. The routines addmntent() and getmntent() will convert from string representation to
escaped representation and back.

另一种方式:直接解析“mount”的结果。通过“on”、“type”等来分割它们可能更简单。

关于java - Java中解析/proc/mounts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13574188/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com