为什么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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...