为什么Linux二进制文件中有一些文件?例如,/var/ log /wtmp日志?更确切地说,为什么日志是二进制形式的。
发布于 2016-09-17 20:39:20
“固定大小的记录”有一些好处。所以utmp/wtmp记录的历史结构是
struct utmp {
char ut_line[8]; /* tty name */
char ut_name[8]; /* user id */
char ut_host[16]; /* host name, if remote */
long ut_time; /* time on */
};(这是来自SunOS 4机器的;我之所以选择它,是因为它很容易阅读)。
这使得将数据附加到文件并以反向顺序显示数据变得非常容易,只需跳过向后的sizeof(struct utmp)字节即可。这使得像last这样的程序很容易以相反的顺序报告。
确切的数据结构随着时间的推移而改变,但记录的大小仍然是固定的。
目前的FreeBSD机器有:
struct utmpx {
short ut_type; /* Type of entry. */
struct timeval ut_tv; /* Time entry was made. */
char ut_id[8]; /* Record identifier. */
pid_t ut_pid; /* Process ID. */
char ut_user[32]; /* User login name. */
char ut_line[16]; /* Device name. */
#if __BSD_VISIBLE
char ut_host[128]; /* Remote hostname. */
#else
char __ut_host[128];
#endif
char __ut_spare[64];
};utmp和lastlog的另一个优点是具有稀疏文件的能力。
例如,使用lastlog ( finger命令用来显示最后登录时间),数据存储在基于uid * sizeof(struct lastlog)的偏移量中。因此,通过查找计算出的位置,您可以快速、轻松地找到uid 12345678的最后登录时间。
对于文本文件,这些好处是不存在的;每个记录都是可变宽度的,或者必须填充。结果更大,更难处理,可能需要解析(long ut_time比尝试解析ASCII日期字符串更容易处理)。
ASCII对于人类和人类可能需要操作的数据都是很好的。二进制对程序来说(有时)更好,特别是对于人类不一定需要看到的原始数据。
https://unix.stackexchange.com/questions/310554
复制相似问题