xref: /haiku/src/tools/fs_shell/uio.cpp (revision 89755088d790ff4fe36f8aa77dacb2bd15507108)
1 /*
2  * Copyright 2007-2008, Ingo Weinhold, bonefish@cs.tu-berlin.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "compatibility.h"
7 
8 #include "fssh_uio.h"
9 
10 #include <new>
11 
12 #include <errno.h>
13 #include <sys/uio.h>
14 
15 #include "partition_support.h"
16 
17 
18 static const fssh_size_t kMaxIOVecs = 1024;
19 
20 
21 bool
22 prepare_iovecs(const struct fssh_iovec *vecs, fssh_size_t count,
23 	struct iovec* systemVecs)
24 {
25 	if (count > kMaxIOVecs) {
26 		errno = B_BAD_VALUE;
27 		return false;
28 	}
29 
30 	for (fssh_size_t i = 0; i < count; i++) {
31 		systemVecs[i].iov_base = vecs[i].iov_base;
32 		systemVecs[i].iov_len = vecs[i].iov_len;
33 	}
34 
35 	return true;
36 }
37 
38 
39 fssh_ssize_t
40 fssh_readv(int fd, const struct fssh_iovec *vector, fssh_size_t count)
41 {
42 	struct iovec systemVecs[kMaxIOVecs];
43 	if (!prepare_iovecs(vector, count, systemVecs))
44 		return -1;
45 
46 	fssh_off_t pos = -1;
47 	fssh_size_t length = 0;
48 	if (FSShell::restricted_file_restrict_io(fd, pos, length) < 0)
49 		return -1;
50 
51 	#if !defined(HAIKU_HOST_PLATFORM_FREEBSD)
52 		return readv(fd, systemVecs, count);
53 	#else
54 		return readv_pos(fd, lseek(fd, 0, SEEK_CUR), systemVecs, count);
55 	#endif
56 }
57 
58 
59 fssh_ssize_t
60 fssh_readv_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec,
61 	fssh_size_t count)
62 {
63 	struct iovec systemVecs[kMaxIOVecs];
64 	if (!prepare_iovecs(vec, count, systemVecs))
65 		return -1;
66 
67 	fssh_size_t length = 0;
68 	if (FSShell::restricted_file_restrict_io(fd, pos, length) < 0)
69 		return -1;
70 
71 	return readv_pos(fd, pos, systemVecs, count);
72 }
73 
74 
75 fssh_ssize_t
76 fssh_writev(int fd, const struct fssh_iovec *vector, fssh_size_t count)
77 {
78 	struct iovec systemVecs[kMaxIOVecs];
79 	if (!prepare_iovecs(vector, count, systemVecs))
80 		return -1;
81 
82 	fssh_off_t pos = -1;
83 	fssh_size_t length = 0;
84 	if (FSShell::restricted_file_restrict_io(fd, pos, length) < 0)
85 		return -1;
86 
87 	#if !defined(HAIKU_HOST_PLATFORM_FREEBSD)
88 		return writev(fd, systemVecs, count);
89 	#else
90 		return writev_pos(fd, lseek(fd, 0, SEEK_CUR), systemVecs, count);
91 	#endif
92 }
93 
94 
95 fssh_ssize_t
96 fssh_writev_pos(int fd, fssh_off_t pos, const struct fssh_iovec *vec,
97 				fssh_size_t count)
98 {
99 	struct iovec systemVecs[kMaxIOVecs];
100 	if (!prepare_iovecs(vec, count, systemVecs))
101 		return -1;
102 
103 	fssh_size_t length = 0;
104 	if (FSShell::restricted_file_restrict_io(fd, pos, length) < 0)
105 		return -1;
106 
107 	return writev_pos(fd, pos, systemVecs, count);
108 }
109