xref: /dpdk/lib/eal/unix/eal_file.c (revision 30a1de105a5f40d77b344a891c4a68f79e815c43)
1*99a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2*99a2dd95SBruce Richardson  * Copyright(c) 2020 Dmitry Kozlyuk
3*99a2dd95SBruce Richardson  */
4*99a2dd95SBruce Richardson 
5*99a2dd95SBruce Richardson #include <sys/file.h>
6*99a2dd95SBruce Richardson #include <fcntl.h>
7*99a2dd95SBruce Richardson #include <unistd.h>
8*99a2dd95SBruce Richardson 
9*99a2dd95SBruce Richardson #include <rte_errno.h>
10*99a2dd95SBruce Richardson 
11*99a2dd95SBruce Richardson #include "eal_private.h"
12*99a2dd95SBruce Richardson 
13*99a2dd95SBruce Richardson int
eal_file_open(const char * path,int flags)14*99a2dd95SBruce Richardson eal_file_open(const char *path, int flags)
15*99a2dd95SBruce Richardson {
16*99a2dd95SBruce Richardson 	static const int MODE_MASK = EAL_OPEN_READONLY | EAL_OPEN_READWRITE;
17*99a2dd95SBruce Richardson 
18*99a2dd95SBruce Richardson 	int ret, sys_flags;
19*99a2dd95SBruce Richardson 
20*99a2dd95SBruce Richardson 	switch (flags & MODE_MASK) {
21*99a2dd95SBruce Richardson 	case EAL_OPEN_READONLY:
22*99a2dd95SBruce Richardson 		sys_flags = O_RDONLY;
23*99a2dd95SBruce Richardson 		break;
24*99a2dd95SBruce Richardson 	case EAL_OPEN_READWRITE:
25*99a2dd95SBruce Richardson 		sys_flags = O_RDWR;
26*99a2dd95SBruce Richardson 		break;
27*99a2dd95SBruce Richardson 	default:
28*99a2dd95SBruce Richardson 		rte_errno = ENOTSUP;
29*99a2dd95SBruce Richardson 		return -1;
30*99a2dd95SBruce Richardson 	}
31*99a2dd95SBruce Richardson 
32*99a2dd95SBruce Richardson 	if (flags & EAL_OPEN_CREATE)
33*99a2dd95SBruce Richardson 		sys_flags |= O_CREAT;
34*99a2dd95SBruce Richardson 
35*99a2dd95SBruce Richardson 	ret = open(path, sys_flags, 0600);
36*99a2dd95SBruce Richardson 	if (ret < 0)
37*99a2dd95SBruce Richardson 		rte_errno = errno;
38*99a2dd95SBruce Richardson 
39*99a2dd95SBruce Richardson 	return ret;
40*99a2dd95SBruce Richardson }
41*99a2dd95SBruce Richardson 
42*99a2dd95SBruce Richardson int
eal_file_truncate(int fd,ssize_t size)43*99a2dd95SBruce Richardson eal_file_truncate(int fd, ssize_t size)
44*99a2dd95SBruce Richardson {
45*99a2dd95SBruce Richardson 	int ret;
46*99a2dd95SBruce Richardson 
47*99a2dd95SBruce Richardson 	ret = ftruncate(fd, size);
48*99a2dd95SBruce Richardson 	if (ret)
49*99a2dd95SBruce Richardson 		rte_errno = errno;
50*99a2dd95SBruce Richardson 
51*99a2dd95SBruce Richardson 	return ret;
52*99a2dd95SBruce Richardson }
53*99a2dd95SBruce Richardson 
54*99a2dd95SBruce Richardson int
eal_file_lock(int fd,enum eal_flock_op op,enum eal_flock_mode mode)55*99a2dd95SBruce Richardson eal_file_lock(int fd, enum eal_flock_op op, enum eal_flock_mode mode)
56*99a2dd95SBruce Richardson {
57*99a2dd95SBruce Richardson 	int sys_flags = 0;
58*99a2dd95SBruce Richardson 	int ret;
59*99a2dd95SBruce Richardson 
60*99a2dd95SBruce Richardson 	if (mode == EAL_FLOCK_RETURN)
61*99a2dd95SBruce Richardson 		sys_flags |= LOCK_NB;
62*99a2dd95SBruce Richardson 
63*99a2dd95SBruce Richardson 	switch (op) {
64*99a2dd95SBruce Richardson 	case EAL_FLOCK_EXCLUSIVE:
65*99a2dd95SBruce Richardson 		sys_flags |= LOCK_EX;
66*99a2dd95SBruce Richardson 		break;
67*99a2dd95SBruce Richardson 	case EAL_FLOCK_SHARED:
68*99a2dd95SBruce Richardson 		sys_flags |= LOCK_SH;
69*99a2dd95SBruce Richardson 		break;
70*99a2dd95SBruce Richardson 	case EAL_FLOCK_UNLOCK:
71*99a2dd95SBruce Richardson 		sys_flags |= LOCK_UN;
72*99a2dd95SBruce Richardson 		break;
73*99a2dd95SBruce Richardson 	}
74*99a2dd95SBruce Richardson 
75*99a2dd95SBruce Richardson 	ret = flock(fd, sys_flags);
76*99a2dd95SBruce Richardson 	if (ret)
77*99a2dd95SBruce Richardson 		rte_errno = errno;
78*99a2dd95SBruce Richardson 
79*99a2dd95SBruce Richardson 	return ret;
80*99a2dd95SBruce Richardson }
81