简体   繁体   English

在xv6中实现lseek

[英]Implementing lseek in xv6

First off I need to say it's completely possible I'm missing something. 首先,我需要说我完全有可能遗漏某些东西。 My assignment is to essentially implement 'fprintf'. 我的任务是实质上实现“ fprintf”。 Now while appending to the file isn't required, I like to go above and beyond. 现在虽然不需要附加到文件,但我想超越。 My issue is, I can't find a definition for lseek in xv6, meaning I have to implement it on my own, but I genuinely don't know how to go about it. 我的问题是,我在xv6中找不到lseek的定义,这意味着我必须自己实现它,但是我真的不知道该怎么做。

Tried reading 512 bytes at a time on an infinite loop in attempt to move the cursor over to the end, as a way to hardcode it, but if the file isn't opened with O_RDWR or I try this with stdout it fails. 尝试在无限循环中一次读取512个字节,以尝试将光标移到末尾,作为对它进行硬编码的一种方法,但是如果未使用O_RDWR打开文件,或者我尝试使用stdout尝试打开文件,则它将失败。

I've also tried writing an empty string on an infinite loop. 我也尝试过在无限循环中写一个空字符串。 Knew it wouldn't work, but tried anyways. 知道它是行不通的,但是还是尝试了。

I can read xv6 fairly well (The user level programs), but I can't understand the source code of lseek for the life of me 我可以很好地阅读xv6(用户级程序),但是我一生都无法理解lseek的源代码

It doesn't have to be a genuine lseek. 它不一定是真正的lseek。 I just need to be able to get to the end of an fd and continue writing, but this cannot be reliant on filemode. 我只需要能够达到fd的结尾并继续写入即可,但这不能依赖于filemode。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

I found the solution. 我找到了解决方案。 The reason O_APPEND doesn't work is because the definition of open(), in sysfile.c, doesn't do anything with append. O_APPEND不起作用的原因是因为sysfile.c中的open()定义对追加没有任何作用。 In sys_open, they hardcode a value of 0 for f->off (offset), and this is what I need to change. 在sys_open中,他们将f-> off(偏移)的值硬编码为0,这就是我需要更改的值。 My planned solution is to figure out the filesize (in bytes) of the file, and set the offset to that number. 我计划的解决方案是弄清文件的文件大小(以字节为单位),并将偏移量设置为该数字。 Probably gonna use stat(). 可能要使用stat()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM