如何解决为什么vfs_write添加任意数量的零字节
我在嵌入式系统的内核2.6.37上编写了一些非常特定的模块驱动程序。而且我需要将一些数据存储到文件中。因此,当模块被“插入”时,我会这样做:
static struct file *filp;
static loff_t pos,mycnt;
static __init int mymodule_init(void) {
oldfs = get_fs();
set_fs(get_ds());
filp = filp_open("/home/mydata.txt",O_WRONLY|O_CREAT,0644);
set_fs(oldfs);
pos = 0; mycnt=0;
......
,然后在需要时调用写数据的函数:
static int mywrite(const char __user *buf,size_t count) {
char str[256]; int i,j;
mm_segment_t oldfs;
int ret;
//Filter out zeroes,if exist
for(i = 0; i < 255; i++) {
if (i == count) break;
if (buf[i] == 0) break;
str[i+1] = buf[i];
}
str[0]=i++; //Put length in a first byte
//Checking for zeroes ------
for(j = 0; j < i; j++) {
if (str[j] == 0) {
printk("0 exist!\n");
str[j] = 0x02; }
}
//Write 'str' to the file ------
oldfs = get_fs();
set_fs(get_ds());
pos = mycnt;
ret = vfs_write(filp,str,i,&pos);
filp->f_pos = pos;
mycnt += i;
if (mycnt != pos) {
pos = mycnt;
vfs_llseek(filp,pos,SEEK_SET);
printk("Corrected!!!!!!!!!!!!!!!\n"); }
set_fs(oldfs);
//Checking for zeroes again ------
for(j = 0; j < i; j++) {
if (str[j] == 0)
printk("0 exist!\n");
}
return count;
}
现在,问题在于生成的文件有时包含(稀有)零块,这些零块本不应该存在。您可以在编写功能代码的特殊代码中查看检查是否没有零,只有假定的ASCII普通文本。每个字符串的第一个字节也包含其长度。并查看该文件,我看到它没有计数零。这些零仍然存在。
另外,我将vfs_write之后的“ pos”与期望值进行了比较-与插入零的次数完全相同(在我的20KB文件的测试示例中为3次)。而且“更正”不起作用...
那么,我该怎么办?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。