Skip to content

Commit 5a0d112

Browse files
committed
PosixPassthroguh and PosixFileSystem updated
1 parent 66319df commit 5a0d112

7 files changed

+162
-41
lines changed

.clang-format

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
BasedOnStyle: WebKit
2+
AlignConsecutiveAssignments: false
3+
AlignConsecutiveMacros: true
4+
AllowAllArgumentsOnNextLine: false
5+
AllowAllConstructorInitializersOnNextLine: false
6+
AllowAllParametersOfDeclarationOnNextLine: false
7+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
8+
AlwaysBreakTemplateDeclarations: Yes
9+
AllowShortBlocksOnASingleLine: Empty
10+
AllowShortEnumsOnASingleLine: true
11+
AllowShortFunctionsOnASingleLine: None
12+
BinPackArguments: false
13+
BinPackParameters: false
14+
BreakBeforeBraces: Custom
15+
BraceWrapping:
16+
AfterCaseLabel: false
17+
AfterClass: false
18+
AfterControlStatement: Never
19+
AfterEnum: false
20+
AfterFunction: true
21+
AfterStruct: false
22+
SplitEmptyFunction: false
23+
SplitEmptyRecord: false
24+
BreakConstructorInitializers: AfterColon
25+
ColumnLimit: 100
26+
FixNamespaceComments: true
27+
IncludeBlocks: Regroup
28+
IndentCaseBlocks: false
29+
IndentCaseLabels: true
30+
PointerAlignment: Left
31+
SpaceAfterTemplateKeyword: false
32+
SpaceBeforeCpp11BracedList: true
33+
SpaceBeforeParens: Always
34+
SpacesInContainerLiterals: false

include/ldpaio/interface/posix_file_system.hpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,15 @@
1111

1212
ssize_t read (int fd, void* buf, size_t size);
1313

14+
ssize_t write (int fd, const void* buf, size_t size);
1415

15-
#endif //LDPAIO_POSIX_FILE_SYSTEM_H
16+
ssize_t pread (int fd, void* buf, size_t size, off_t offset);
17+
18+
ssize_t pwrite (int fd, const void* buf, size_t size, off_t offset);
19+
20+
int open (const char* pathname, int flags);
21+
22+
int open (const char* pathname, int flags, mode_t mode);
23+
24+
25+
#endif // LDPAIO_POSIX_FILE_SYSTEM_H
+29-19
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,54 @@
11
/**
2-
* Written by Ricardo Macedo.
3-
* Copyright (c) 2021 INESC TEC.
4-
**/
2+
* Written by Ricardo Macedo.
3+
* Copyright (c) 2021 INESC TEC.
4+
**/
55

66
#ifndef LDPAIO_POSIX_PASSTHROUGH_HPP
77
#define LDPAIO_POSIX_PASSTHROUGH_HPP
88

99
#define __USE_GNU
1010
#define _GNU_SOURCE
1111

12-
#include <sys/types.h>
13-
#include <sys/stat.h>
12+
#include <dlfcn.h>
1413
#include <fcntl.h>
14+
#include <sys/stat.h>
15+
#include <sys/types.h>
1516
#include <unistd.h>
16-
#include <dlfcn.h>
1717

1818
namespace ldpaio {
1919

20-
typedef ssize_t (*real_read_t)(int, void*, size_t);
20+
typedef ssize_t (*real_read_t) (int, void*, size_t);
21+
typedef ssize_t (*real_write_t) (int, const void*, size_t);
22+
typedef ssize_t (*real_pread_t) (int, void*, size_t, off_t);
23+
typedef ssize_t (*real_pwrite_t) (int, const void*, size_t, off_t);
24+
typedef ssize_t (*real_open_t) (const char*, int);
25+
typedef ssize_t (*real_open_2_t) (const char*, int, mode_t);
26+
27+
28+
class PosixPassthrough {
2129

22-
ssize_t passthrough_read (int fd, void* buf, ssize_t counter);
30+
public:
2331

24-
ssize_t passthrough_write (int fd, const void* buf, ssize_t counter);
32+
static ssize_t passthrough_read (int fd, void *buf, ssize_t counter);
2533

26-
ssize_t passthrough_pread (int fd, void* buf, ssize_t counter, off_t offset);
34+
static ssize_t passthrough_write (int fd, const void *buf, ssize_t counter);
2735

28-
ssize_t passthrough_pwrite (int fd, const void* buf, ssize_t counter, off_t offset);
36+
static ssize_t passthrough_pread (int fd, void *buf, ssize_t counter, off_t offset);
2937

30-
// open calls
31-
int passthrough_open (const char* pathname, int flags);
38+
static ssize_t passthrough_pwrite (int fd, const void *buf, ssize_t counter, off_t offset);
3239

33-
int passthrough_open (const char* pathname, int flags, mode_t mode);
40+
// open calls
41+
static int passthrough_open (const char* pathname, int flags);
3442

35-
int passthrough_creat (const char* pathname, mode_t mode);
43+
static int passthrough_open (const char* pathname, int flags, mode_t mode);
3644

37-
int passthrough_openat (int dirfd, const char* pathname, int flags);
45+
static int passthrough_creat (const char* pathname, mode_t mode);
3846

39-
int passthrough_openat (int dirfd, const char* pathname, int flags, mode_t mode);
47+
static int passthrough_openat (int dirfd, const char* pathname, int flags);
4048

41-
}
49+
static int passthrough_openat (int dirfd, const char* pathname, int flags, mode_t mode);
4250

51+
};
52+
} // namespace ldpaio
4353

44-
#endif //LDPAIO_POSIX_PASSTHROUGH_HPP
54+
#endif // LDPAIO_POSIX_PASSTHROUGH_HPP

run-clang-format.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
# ---------------------------------------------------------------------------- #
3+
4+
set -o errexit -o pipefail -o nounset
5+
6+
cd "$( realpath "$0" | xargs dirname )" &&
7+
find . -name '*.[ch]' -o -name '*.[ch]pp' | xargs clang-format -i
8+
9+
# ---------------------------------------------------------------------------- #

src/interface/posix_file_system.cpp

+40-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,48 @@
11
/**
2-
* Written by Ricardo Macedo.
3-
* Copyright (c) 2021 INESC TEC.
4-
**/
2+
* Written by Ricardo Macedo.
3+
* Copyright (c) 2021 INESC TEC.
4+
**/
55

6-
7-
#include <ldpaio/interface/posix_file_system.hpp>
8-
#include <ldpaio/interface/posix_passthrough.hpp>
96
#include <cstring>
107
#include <iostream>
8+
#include <ldpaio/interface/posix_file_system.hpp>
9+
#include <ldpaio/interface/posix_passthrough.hpp>
1110

12-
11+
// read call. ...
1312
ssize_t read (int fd, void* buf, size_t size)
1413
{
1514
std::cout << "One more read ...\n";
16-
return ldpaio::passthrough_read (fd, buf, size);
17-
}
15+
return ldpaio::PosixPassthrough::passthrough_read (fd, buf, size);
16+
}
17+
18+
// write call. ...
19+
ssize_t write (int fd, const void* buf, size_t size)
20+
{
21+
std::cout << "One more write ...\n";
22+
return ldpaio::PosixPassthrough::passthrough_write (fd, buf, size);
23+
}
24+
25+
// pread call. ...
26+
ssize_t pread (int fd, void* buf, size_t size, off_t offset)
27+
{
28+
std::cout << "One more pread ...\n";
29+
return ldpaio::PosixPassthrough::passthrough_pread (fd, buf, size, offset);
30+
}
31+
32+
// pwrite call. ...
33+
ssize_t pwrite (int fd, const void* buf, size_t size, off_t offset)
34+
{
35+
std::cout << "One more pwrite ...\n";
36+
return ldpaio::PosixPassthrough::passthrough_pwrite (fd, buf, size, offset);
37+
}
38+
39+
int open (const char* pathname, int flags) {
40+
std::cout << "One more open ... \n";
41+
return ldpaio::PosixPassthrough::passthrough_open (pathname, flags);
42+
}
43+
44+
int open (const char* pathname, int flags, mode_t mode) {
45+
std::cout << "One more open (w/ mode) ... \n";
46+
return ldpaio::PosixPassthrough::passthrough_open (pathname, flags, mode);
47+
}
48+

src/interface/posix_passthrough.cpp

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
/**
2-
* Written by Ricardo Macedo.
3-
* Copyright (c) 2021 INESC TEC.
4-
**/
2+
* Written by Ricardo Macedo.
3+
* Copyright (c) 2021 INESC TEC.
4+
**/
55

66
#include <ldpaio/interface/posix_passthrough.hpp>
77

88
namespace ldpaio {
99

10-
ssize_t passthrough_read (int fd, void* buf, ssize_t counter) {
11-
return ((real_read_t) dlsym (RTLD_NEXT, "read"))(fd, buf, counter);
12-
}
10+
ssize_t PosixPassthrough::passthrough_read (int fd, void* buf, ssize_t counter)
11+
{
12+
return ((real_read_t)dlsym (RTLD_NEXT, "read")) (fd, buf, counter);
13+
}
1314

15+
ssize_t PosixPassthrough::passthrough_write (int fd, const void* buf, ssize_t counter)
16+
{
17+
return ((real_write_t)dlsym (RTLD_NEXT, "write")) (fd, buf, counter);
18+
}
1419

15-
}
20+
ssize_t PosixPassthrough::passthrough_pread (int fd, void* buf, ssize_t counter, off_t offset)
21+
{
22+
return ((real_pread_t)dlsym (RTLD_NEXT, "pread")) (fd, buf, counter, offset);
23+
}
24+
25+
ssize_t PosixPassthrough::passthrough_pwrite (int fd, const void* buf, ssize_t counter, off_t offset)
26+
{
27+
return ((real_pwrite_t)dlsym (RTLD_NEXT, "pwrite")) (fd, buf, counter, offset);
28+
}
29+
30+
int PosixPassthrough::passthrough_open (const char* pathname, int flags) {
31+
return ((real_open_t)dlsym (RTLD_NEXT, "open")) (pathname, flags);
32+
}
33+
34+
int PosixPassthrough::passthrough_open (const char* pathname, int flags, mode_t mode) {
35+
return ((real_open_2_t)dlsym (RTLD_NEXT, "open")) (pathname, flags, mode);
36+
}
37+
38+
} // namespace ldpaio

tests/ldpaio_test.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44
**/
55

66
#include <cstdio>
7-
#include <unistd.h>
87
#include <cstdlib>
8+
#include <unistd.h>
9+
#include <fcntl.h>
910

1011
#define BUFFER_SIZE 1024
1112

12-
int main (int argc, char* argv[]) {
13+
int main (int argc, char* argv[])
14+
{
1315

1416
ssize_t read_bytes = 0;
1517
ssize_t written_bytes = 0;
16-
char* buffer = static_cast<char*>(malloc(sizeof(char) * BUFFER_SIZE));
18+
char* buffer = static_cast<char*> (std::malloc (sizeof (char) * BUFFER_SIZE));
19+
20+
int fd_write = ::open ("tmp.txt", O_CREAT | O_TRUNC | O_RDWR, 0600);
1721

1822
while ((read_bytes = ::read (STDIN_FILENO, buffer, BUFFER_SIZE)) > 0) {
19-
written_bytes += write (STDOUT_FILENO, buffer, read_bytes);
23+
written_bytes += write (fd_write, buffer, read_bytes);
2024
}
2125

26+
close (fd_write);
2227
delete (buffer);
2328

2429
return 0;
25-
2630
}

0 commit comments

Comments
 (0)