- 論壇徽章:
- 0
|
回復(fù) 1# xiaoruoax
經(jīng)過嘗試發(fā)現(xiàn),解決方法很簡單。
只需要,先通過EXT2_IOC_GETFLAGS獲取 ext2 flags,然后,將要設(shè)置的flag通過 “|” 操作附加上去,然后通過EXT2_IOC_SETFLAGS設(shè)置就可以成功,在CentOS 6.3 (x86_64)上面測試通過。設(shè)置完成后,通過lsattr可以查看設(shè)置的結(jié)果(chattr可以設(shè)置flags)。
示例代碼如下:
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <linux/fs.h>
- #include <linux/ext2_fs.h>
- #include <sys/ioctl.h>
- #include <errno.h>
- int main(int argc, char *argv[])
- {
- int fd;
- long flags;
- if (argc != 2) {
- printf("Usage: %s <filename>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- if ((fd = open(argv[1], O_RDONLY)) < 0) {
- perror("open");
- exit(EXIT_FAILURE);
- }
- /* Get attributes from the file */
- if (ioctl(fd, EXT2_IOC_GETFLAGS, &flags)) {
- perror("ioctl");
- exit(EXIT_FAILURE);
- }
- /* Set attributes to the file */
- flags |= (EXT2_SYNC_FL | EXT2_NODUMP_FL);
- if (ioctl(fd, EXT2_IOC_SETFLAGS, &flags)) {
- perror("ioctl");
- close(fd);
- exit(EXIT_FAILURE);
- }
- if (flags & EXT2_SYNC_FL) {
- puts("SYNC flag set");
- }
- if (flags & EXT2_NODUMP_FL) {
- puts("NODUMP flag set");
- }
- close(fd);
- exit(EXIT_SUCCESS);
- }
復(fù)制代碼 |
|