#include #include #include #include #include #include #include #ifndef FS_IOC_SETFLAGS #define FS_IOC_SETFLAGS _IOW('f', 2, long) #warning defining SETFLAGS locally #endif #ifndef FS_IOC_GETFLAGS #define FS_IOC_GETFLAGS _IOR('f', 1, long) #warning defining GETFLAGS locally #endif #ifndef FS_NOCOW_FL #define FS_NOCOW_FL 0x00800000 /* Do not cow file */ #endif int main(int argc, char **argv) { int fd; int r; long flags; if (argc < 2) { printf("usage: %s file\n", argv[0]); exit(1); } fd = open(argv[1], O_RDONLY); if (fd == -1) { perror("open()"); return 1; } printf("GETFLAGS ioctl 0x%x\n", FS_IOC_GETFLAGS); printf("SETFLAGS ioctl 0x%x\n", FS_IOC_SETFLAGS); r = ioctl(fd, FS_IOC_GETFLAGS, &flags); if (r == -1) { perror("ioctl(GETFLAGS)"); return 1; } else { printf("file flags before: 0x%lx\n", flags); } if(strcmp(argv[0], "cow")==0) { printf("Remove NOCOW flag for %s\n", argv[1]); flags ^= FS_NOCOW_FL; } else { printf("Set NOCOW flag for %s\n", argv[1]); flags |= FS_NOCOW_FL; } r = ioctl(fd, FS_IOC_SETFLAGS, &flags); printf("ioctl returned: %d\n", r); if (r == -1) { perror("ioctl()"); return 1; } r = ioctl(fd, FS_IOC_GETFLAGS, &flags); if (r == -1) { perror("ioctl(GETFLAGS)"); return 1; } else { printf("file flags after: 0x%lx\n", flags); } return 0; }