太一的博客

一个程序学徒

文件与IO(二)

stat

功能:读取文件元数据

1
2
3
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);

struct stat

1
2
3
4
5
6
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink /* number of hard links */
}

复制文件描述符

复制文件描述符有三种方法

dup

dup2

fcntl

fcntl(fd, F_DUPFD, fd_start);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int filetype(struct stat *buf);
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)

int main(int argc, char *argv[])
{
int fd;
fd = open("test2.txt", O_WRONLY);
if (fd == -1)
ERR_EXIT("open error");
close(1);
dup(fd);
printf("1111\n");
return 0;
}

fcntl

功能:操纵文件描述符,改变已打开的文件的属性

int fcntl(int fd, int cmd, ... /* arg */ );

fcntl 常用操作

  • 复制文件描述符
    • F_DUPFD (long)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int filetype(struct stat *buf);
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)

int main(int argc, char *argv[])
{
int fd;
fd = open("test.txt", O_WRONLY);
if (fd == -1)
ERR_EXIT("open error");
// close(1);
// dup(fd);
close(1);
if (fcntl(fd, F_DUPFD, 0) < 0)
ERR_EXIT("dup fd error");
printf("1111xx\n");
return 0;
}

  • 文件描述符标志
    • F_GETFD (void)
    • F_SETFD (long)
  • 文件状态标志
    • F_GETFL (void)
    • F_SETFL (long)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int filetype(struct stat *buf);
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)

int main(int argc, char *argv[])
{
char buf[1024] = {0};
int ret;
int flags;
flags = fcntl(0, F_GETFL, 0);
if (flags == -1)
ERR_EXIT("fcntl get flag error");

ret = fcntl(0, F_SETFL, flags | O_NONBLOCK);
if (ret == -1)
ERR_EXIT("fcntl set flag error");

ret = read(0, buf, 1024);
if (ret == -1)
ERR_EXIT("read error");

printf("buf=%s\n", buf);

}
  • 文件锁
    • F_GETLK
    • F_SETLK, F_SETLKW
1
2
3
4
5
6
7
struct flock {
short l_type;
short l_whence;
off_t l_start;
off_t l_len;
pid_t l_pid;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int filetype(struct stat *buf);
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)

int main(int argc, char *argv[])
{
int fd;
fd = open("test2.txt", O_CREAT | O_RDWR | O_TRUNC, 0644);
if (fd == -1)
ERR_EXIT("open error");

struct flock lock;
memset(&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;

if (fcntl(fd, F_SETLK, &lock) == 0)
{
printf("lock succes\n");
printf("press any key to unlock\n");
getchar();
lock.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &lock) == 0)
printf("unlock success\n");
else
ERR_EXIT("unlock fail");
}
else
ERR_EXIT("lock fail");


}