SEEK_HOLE和SEEK_DATA在Ubuntu 12.04.2 LTS不工作 [英] SEEK_HOLE and SEEK_DATA not working in Ubuntu 12.04.2 LTS

查看:806
本文介绍了SEEK_HOLE和SEEK_DATA在Ubuntu 12.04.2 LTS不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当编译我的错误:

  CC holetest.c -o holetest
holetest.c:在函数'test_seek:
holetest.c:48:19:错误:'SEEK_HOLE'未申报(在一次使用此功能)
holetest.c:48:19:注:每个未声明的标识符为它出现在每个函数报道只有一次
holetest.c:51:19:错误:'SEEK_DATA'未申报(在一次使用此功能)
使:*** [holetest]错误1

如果我删除SEEK_HOLE和SEEK_DATA我没有问题。

我错过了一个包含或库?

的Makefile:

 所有:holetestholetest:holetest.c
    RM -f holetest
    GCC holetest.c -o holetest

holetest.c:

 的#include< SYS / stat.h>
#包括LT&;&fcntl.h GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&errno.h中GT;
#包括LT&;&string.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&;&unistd.h中GT;
#包括LT&;&stdlib.h中GT;#定义文件名/tmp/partly.sparse
的#define FILE_SIZE(1 <<;小于30)
#定义START_STRING的文件\\ n启动
#定义START_LEN的strlen(START_STRING)
#定义END_STRING的文件\\ n \\ NEND
#定义END_LEN的strlen(END_STRING)#定义调试(男,...)fprintf中(标准错误,%I:DEBUG%10.10s:%3.0D:M\\ n,GETPID(),__FILE__,__LINE__,## __ VA_ARGS__); fflush(标准错误);
#定义LOG_ERR(男,...)fprintf中(标准错误,%I:错误错误号:%I%10.10s:%3.0D:M\\ n,GETPID(),错误号,__FILE__,__LINE__,## __VA_ARGS__); fflush(标准错误);#定义quit_if(条件,...)做{\\
        如果(条件){\\
                LOG_ERR(__ VA_ARGS__); \\
                PERROR(NULL); \\
                退出(错误); \\
        } \\
}而(0);INT make_partly_sparse(为const char *文件名,大小off_t){
        INT R,FD;
        FD =开放(文件名,O_RDWR | O_CREAT,0755);
        quit_if(FD&LT; 1,无法创建%S,文件名);
        R =写(FD,START_STRING,START_LEN);
        quit_if(R&LT; START_LEN,无法写入%S,文件名);
        R = lseek的(FD,FILE_SIZE - END_LEN,SEEK_SET);
        quit_if(R&LT; 0,无法寻求%S,文件名);
        R =写(FD,END_STRING,END_LEN);
        quit_if(R&LT; END_LEN,无法写入%S,文件名);
        R =接近(FD);
        quit_if(R&LT; 0,无法关闭%S,文件名);
        返回0;
}INT test_seek(为const char *文件名){
        INT R,FD;
        FD =开放(文件名,O_RDWR | O_CREAT,0755);
        quit_if(FD&LT; 1,无法打开%S,文件名);
        调试(求在%li与洞,START_LEN);
        R =了lseek(FD,0,SEEK_HOLE);
        quit_if(R&LT; 0,无法寻求%S,文件名);
        quit_if(!R = START_LEN,SEEK_HOLE不受支持%I,R);
        R =了lseek(FD,0,SEEK_DATA);
        quit_if(R&LT; 0,无法寻求%S,文件名);
        quit_if(R =(FILE_SIZE - END_LEN),不支持SEEK_DATA%I,R!);
        R =接近(FD);
        quit_if(R&LT; 0,无法关闭%S,文件名);
        返回0;
}INT主(INT ARGC,CHAR *的argv []){
        调试(制造稀疏文件:%s,文件名);
        make_partly_sparse(文件名,FILE_SIZE);
        test_seek(文件名);
        返回0;
}

系统:

  $猫的/ etc / LSB释放
DISTRIB_ID = Ubuntu的
DISTRIB_RELEASE = 12.04
DISTRIB_ codeNAME = precise
DISTRIB_DESCRIPTION =Ubuntu的LTS 12.04.2$的uname -a
Linux的TUX 3.2.0-45泛型#70,Ubuntu的SMP周三5月29日20时12分06秒UTC 2013 x86_64的x86_64的x86_64的GNU / Linux的$ grep的外部/ etc / fstab文件
UUID = be3aacb3-6457-4ba1-92bb-0f63ad514f40 / EXT4错误=重新挂载-RO 0 1

更新:

现在编译,但不起作用,SEEK_HOLE跳过就在洞口,并寻求到该文件的末尾。

  $使
RM -f holetest
GCC -D_GNU_SOURCE holetest.c -o holetest$ ./holetest
18731:DEBUG holetest.c:60:制作稀疏文件:/tmp/partly.sparse
18731:DEBUG holetest.c:47:14洞求
18731:错误错误号:0 holetest.c:50:SEEK_HOLE不受支持1073741824
成功$杜/tmp/partly.sparse
8 /tmp/partly.sparse$ LS -LA /tmp/partly.sparse
-rwxr-XR-X 1克里斯·克里斯1073741824 8月16日14:08 /tmp/partly.sparse


解决方案

stdio.h中将它们定义为;

  / *为第三个参数`fseek的的可能性。
   这些值不应当改变。 * /
#定义SEEK_SET 0 / *从文件开始寻找。 * /
#定义SEEK_CUR 1 / *从当前位置查找。 * /
的#define SEEK_END 2 / *从文件末尾求。 * /
#IFDEF __USE_GNU
#定义SEEK_DATA 3 / *寻找到下一个数据。 * /
#定义SEEK_HOLE 4 / *寻找到下一洞。 * /
#万一

__ USE_GNU 是,当你定义设置 _GNU_SOURCE 的内部符号,这意味着使用它们需要与 -D_GNU_SOURCE 进行编译。

  $ GCC test.c的
test.c的:在函数'test_seek:
test.c以48:26:错误:'SEEK_HOLE'未申报(在一次使用此功能)
test.c以48:26:注:每个未声明的标识符为它出现在每个函数报道只有一次
test.c的:51:26:错误:'SEEK_DATA'未申报(在一次使用此功能)
$ GCC -D_GNU_SOURCE test.c的
$

When compiling I get the error:

cc     holetest.c   -o holetest
holetest.c: In function ‘test_seek’:
holetest.c:48:19: error: ‘SEEK_HOLE’ undeclared (first use in this function)
holetest.c:48:19: note: each undeclared identifier is reported only once for each function it appears in
holetest.c:51:19: error: ‘SEEK_DATA’ undeclared (first use in this function)
make: *** [holetest] Error 1

If I remove SEEK_HOLE and SEEK_DATA I have no issues.

Have I missed an include or a library?

Makefile:

all: holetest

holetest: holetest.c
    rm -f holetest
    gcc holetest.c -o holetest

holetest.c:

#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

#define FILENAME "/tmp/partly.sparse"
#define FILE_SIZE (1<<30)
#define START_STRING "start of file\n"
#define START_LEN strlen(START_STRING)
#define END_STRING "\nend of file\n"
#define END_LEN strlen(END_STRING)

#define debug(M, ...) fprintf(stderr, "%i: DEBUG %10.10s:%3.0d: " M "\n", getpid(), __FILE__, __LINE__, ##__VA_ARGS__); fflush(stderr);
#define log_err(M, ...) fprintf(stderr, "%i: ERROR errno:%i %10.10s:%3.0d: " M "\n", getpid(), errno, __FILE__, __LINE__, ##__VA_ARGS__); fflush(stderr);

#define quit_if(COND, ...) do { \
        if(COND) { \
                log_err(__VA_ARGS__); \
                perror(NULL); \
                exit(errno); \
        } \
} while(0);

int make_partly_sparse(const char *filename, off_t size) {
        int r, fd;
        fd = open(filename, O_RDWR|O_CREAT, 0755);
        quit_if(fd < 1, "unable to create %s", filename);
        r = write(fd, START_STRING, START_LEN);
        quit_if(r < START_LEN, "unable to write %s", filename);
        r = lseek(fd, FILE_SIZE - END_LEN, SEEK_SET);
        quit_if(r < 0, "unable to seek %s", filename);
        r = write(fd, END_STRING, END_LEN);
        quit_if(r < END_LEN, "unable to write %s", filename);
        r = close(fd);
        quit_if(r < 0, "unable to close %s", filename);
        return 0;
}

int test_seek(const char *filename) {
        int r, fd;
        fd = open(filename, O_RDWR|O_CREAT, 0755);
        quit_if(fd < 1, "unable to open %s", filename);
        debug("seeking hole at %li", START_LEN);
        r = lseek(fd, 0, SEEK_HOLE);
        quit_if(r < 0, "unable to seek %s", filename);
        quit_if(r != START_LEN, "SEEK_HOLE unsupported %i", r);
        r = lseek(fd, 0, SEEK_DATA);
        quit_if(r < 0, "unable to seek %s", filename);
        quit_if(r != (FILE_SIZE - END_LEN), "SEEK_DATA unsupported %i", r);
        r = close(fd);
        quit_if(r < 0, "unable to close %s", filename);
        return 0;
}

int main(int argc, char *argv[]) {
        debug("making sparse file: %s", FILENAME);
        make_partly_sparse(FILENAME, FILE_SIZE);
        test_seek(FILENAME);
        return 0;
}

System:

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"

$ uname -a
Linux tux 3.2.0-45-generic #70-Ubuntu SMP Wed May 29 20:12:06 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

$ grep "ext" /etc/fstab
UUID=be3aacb3-6457-4ba1-92bb-0f63ad514f40 /               ext4    errors=remount-ro 0       1

Update:

It now compiles but doesn't work, SEEK_HOLE skips right over the hole and seeks to the end of the file.

$ make
rm -f holetest
gcc -D_GNU_SOURCE holetest.c -o holetest

$ ./holetest 
18731: DEBUG holetest.c: 60: making sparse file: /tmp/partly.sparse
18731: DEBUG holetest.c: 47: seeking hole at 14
18731: ERROR errno:0 holetest.c: 50: SEEK_HOLE unsupported 1073741824
Success

$ du /tmp/partly.sparse 
8   /tmp/partly.sparse

$ ls -lA /tmp/partly.sparse 
-rwxr-xr-x 1 chris chris 1073741824 Aug 16 14:08 /tmp/partly.sparse

解决方案

stdio.h defines them as;

/* The possibilities for the third argument to `fseek'.
   These values should not be changed.  */
#define SEEK_SET        0       /* Seek from beginning of file.  */
#define SEEK_CUR        1       /* Seek from current position.  */
#define SEEK_END        2       /* Seek from end of file.  */
#ifdef __USE_GNU
# define SEEK_DATA      3       /* Seek to next data.  */
# define SEEK_HOLE      4       /* Seek to next hole.  */
#endif

__USE_GNU is an internal symbol that is set when you define _GNU_SOURCE, which means that to use them you need to compile with -D_GNU_SOURCE.

$ gcc test.c 
test.c: In function ‘test_seek’:
test.c:48:26: error: ‘SEEK_HOLE’ undeclared (first use in this function)
test.c:48:26: note: each undeclared identifier is reported only once for each function it appears in
test.c:51:26: error: ‘SEEK_DATA’ undeclared (first use in this function)
$ gcc -D_GNU_SOURCE test.c 
$ 

这篇关于SEEK_HOLE和SEEK_DATA在Ubuntu 12.04.2 LTS不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆