write error:file too large!!!

这个错误出现在开发板中,文件系统为ext3

无ulimit

 

解决方案有2种:

方案一 :limit

 

 1        #include <sys/time.h>
 2        #include <sys/resource.h>
 3 int modify_limit()
 4 {
 5         struct rlimit limitf;
 6         if (getrlimit(RLIMIT_CORE, &limitf)==0) {
 7                 printf("limitf.rlim_cur=0x%x,limitf.rlim_max =0x%x  \n",limitf.rlim_cur,limitf.rlim_max );
 8                 limitf.rlim_cur=RLIM_INFINITY;
 9                 limitf.rlim_max =RLIM_INFINITY;
10 
11                 
12                 if(setrlimit(RLIMIT_FSIZE,&limitf)!=0){
13                         /* failed. try raising just to the old max */
14                         perror("setrlimit error:");
15                         return -1;
16                 }
17                 printf("setrlimit success!!!,\n");
18                 printf("limitf.rlim_cur=0x%016x,limitf.rlim_max =0x%016x  \n",limitf.rlim_cur,limitf.rlim_max );
19         }
20         return 0;
21 }

 

 

 

 

方案二:使用O_LARGEFILE参数:

 

 1 {
 2     int n=WRITE_TIME;
 3     char buf[BUF_LEN]={0xFF};
 4     char out_file[256]={0};
 5     int fd=0;
 6     
 7     switch(argc)
 8     {
 9         case 1:
10             memcpy(&out_file,DEFAULT_OUT_FILE,sizeof(DEFAULT_OUT_FILE));
11             break;
12         case 2:
13             memcpy(&out_file,&argv[1],strlen(&argv[1]));
14             break;
15         default:
16             return -1;
17     }
18 
19     //modify_limit();
20 
21     fd=open(out_file,O_WRONLY|O_CREAT|O_LARGEFILE);
22     if(fd<=0)
23     {
24         perror("open error:");
25         return -1;
26     }
27     
28     while(n--)
29         if(safe_write(fd,&buf,sizeof(buf)) != sizeof(buf))
30         {    
31             perror("wirte error:");
32             break;
33         }
34     fsync(fd);
35     close(fd);
36 }

需要增加声明在include之前:

 

1 #define _LARGEFILE_SOURCE
2 #define _LARGEFILE64_SOURCE
3 #define _FILE_OFFSET_BITS 64

编译时带参数:

 

1 gcc -o test_wirte New0001.c  -D_FILE_OFFSET_BITS=64 -D_LARGE_FILE

 

 

 

验证,第一方案 失败,第二方案 成功。

 

 

 附带完整 测试代码:

 

 1 #define _LARGEFILE_SOURCE
 2 #define _LARGEFILE64_SOURCE
 3 #define _FILE_OFFSET_BITS 64
 4 
 5 
 6 #include <stdio.h>
 7 #include <fcntl.h>
 8 #include <string.h>
 9 #include <errno.h>
10 
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 
14 
15 #include <sys/time.h>
16 #include <sys/resource.h>
17 int modify_limit()
18 {
19         struct rlimit limitf;
20         if (getrlimit(RLIMIT_CORE, &limitf)==0) {
21                 printf("limitf.rlim_cur=0x%x,limitf.rlim_max =0x%x  \n",limitf.rlim_cur,limitf.rlim_max );
22                 limitf.rlim_cur=RLIM_INFINITY;
23                 limitf.rlim_max =RLIM_INFINITY;
24 
25                 
26                 if(setrlimit(RLIMIT_FSIZE,&limitf)!=0){
27                         /* failed. try raising just to the old max */
28                         /*
29                         ............
30                         */
31                         perror("setrlimit error:");
32                         return -1;
33                 }
34                 printf("setrlimit success!!!,\n");
35                 printf("limitf.rlim_cur=0x%016x,limitf.rlim_max =0x%016x  \n",limitf.rlim_cur,limitf.rlim_max );
36         }
37         return 0;
38 }
39 
40 ssize_t  safe_write(int fd, const void *buf, size_t count)
41 {
42     ssize_t n;
43 
44     do {
45         n = write(fd, buf, count);
46     } while (n < 0 /*&& errno == EINTR*/);
47 
48     return n;
49 }
50 #define BUF_LEN 65535
51 #define WRITE_TIME 102400
52 #define DEFAULT_OUT_FILE "/mnt/out_file"
53 
54 int main(int argc,char *argv[])
55 {
56     int n=WRITE_TIME;
57     char buf[BUF_LEN]={0xFF};
58     char out_file[256]={0};
59     int fd=0;
60     
61     switch(argc)
62     {
63         case 1:
64             memcpy(&out_file,DEFAULT_OUT_FILE,sizeof(DEFAULT_OUT_FILE));
65             break;
66         case 2:
67             memcpy(&out_file,&argv[1],strlen(&argv[1]));
68             break;
69         default:
70             return -1;
71     }
72 
73     //modify_limit();
74 
75     fd=open(out_file,O_WRONLY|O_CREAT|O_LARGEFILE);
76     if(fd<=0)
77     {
78         perror("open error:");
79         return -1;
80     }
81     
82     while(n--)
83         if(safe_write(fd,&buf,sizeof(buf)) != sizeof(buf))
84         {    
85             perror("wirte error:");
86             break;
87         }
88     fsync(fd);
89     close(fd);
90 }

 

 

 

posted on 2012-08-16 12:28  J.evan  阅读(1714)  评论(0编辑  收藏  举报