forked from DPDK/dpdk
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eal: introduce internal wrappers for file operations
Introduce OS-independent wrappers in order to support common EAL code on Unix and Windows: * eal_file_open: open or create a file. * eal_file_lock: lock or unlock an open file. * eal_file_truncate: enforce a given size for an open file. Implementation for Linux and FreeBSD is placed in "unix" subdirectory, which is intended for common code between the two. These thin wrappers require no special maintenance. Common code supporting multi-process doesn't use the new wrappers, because it is inherently Unix-specific and would impose excessive requirements on the wrappers. Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
- Loading branch information
1 parent
67a661e
commit 176bb37
Showing
8 changed files
with
184 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* SPDX-License-Identifier: BSD-3-Clause | ||
* Copyright(c) 2020 Dmitry Kozlyuk | ||
*/ | ||
|
||
#include <sys/file.h> | ||
#include <sys/mman.h> | ||
#include <unistd.h> | ||
|
||
#include <rte_errno.h> | ||
|
||
#include "eal_private.h" | ||
|
||
int | ||
eal_file_open(const char *path, int flags) | ||
{ | ||
static const int MODE_MASK = EAL_OPEN_READONLY | EAL_OPEN_READWRITE; | ||
|
||
int ret, sys_flags; | ||
|
||
switch (flags & MODE_MASK) { | ||
case EAL_OPEN_READONLY: | ||
sys_flags = O_RDONLY; | ||
break; | ||
case EAL_OPEN_READWRITE: | ||
sys_flags = O_RDWR; | ||
break; | ||
default: | ||
rte_errno = ENOTSUP; | ||
return -1; | ||
} | ||
|
||
if (flags & EAL_OPEN_CREATE) | ||
sys_flags |= O_CREAT; | ||
|
||
ret = open(path, sys_flags, 0600); | ||
if (ret < 0) | ||
rte_errno = errno; | ||
|
||
return ret; | ||
} | ||
|
||
int | ||
eal_file_truncate(int fd, ssize_t size) | ||
{ | ||
int ret; | ||
|
||
ret = ftruncate(fd, size); | ||
if (ret) | ||
rte_errno = errno; | ||
|
||
return ret; | ||
} | ||
|
||
int | ||
eal_file_lock(int fd, enum eal_flock_op op, enum eal_flock_mode mode) | ||
{ | ||
int sys_flags = 0; | ||
int ret; | ||
|
||
if (mode == EAL_FLOCK_RETURN) | ||
sys_flags |= LOCK_NB; | ||
|
||
switch (op) { | ||
case EAL_FLOCK_EXCLUSIVE: | ||
sys_flags |= LOCK_EX; | ||
break; | ||
case EAL_FLOCK_SHARED: | ||
sys_flags |= LOCK_SH; | ||
break; | ||
case EAL_FLOCK_UNLOCK: | ||
sys_flags |= LOCK_UN; | ||
break; | ||
} | ||
|
||
ret = flock(fd, sys_flags); | ||
if (ret) | ||
rte_errno = errno; | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
# Copyright(c) 2020 Dmitry Kozlyuk | ||
|
||
sources += files( | ||
'eal_file.c', | ||
) |