vfs_ceph: use consistent code style when setting errno
[Samba.git] / source3 / lib / util_file.c
blob5d6afc3c0b12d399d190f454589966d9f96830b0
1 /*
2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 3 of the License, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
21 #include "lib/util/util_file.h"
22 #include "source3/lib/util_file.h"
23 #include "lib/util/debug.h"
24 #include "lib/util/samba_util.h"
25 #include "lib/util/sys_rw.h"
26 #include "lib/util/sys_popen.h"
27 #include "lib/async_req/async_sock.h"
28 #include "lib/util/tevent_unix.h"
30 struct file_ploadv_state {
31 struct tevent_context *ev;
32 struct tevent_req *subreq;
33 size_t maxsize;
34 int fd;
35 uint8_t *buf;
38 static void file_ploadv_cleanup_fn(
39 struct tevent_req *req, enum tevent_req_state req_state);
40 static void file_ploadv_readable(struct tevent_req *subreq);
42 struct tevent_req *file_ploadv_send(TALLOC_CTX *mem_ctx,
43 struct tevent_context *ev,
44 char * const argl[], size_t maxsize)
46 struct tevent_req *req = NULL;
47 struct file_ploadv_state *state = NULL;
49 req = tevent_req_create(mem_ctx, &state, struct file_ploadv_state);
50 if (req == NULL) {
51 return NULL;
53 state->ev = ev;
54 state->maxsize = maxsize;
56 state->fd = sys_popenv(argl);
57 if (state->fd == -1) {
58 tevent_req_error(req, errno);
59 return tevent_req_post(req, ev);
61 tevent_req_set_cleanup_fn(req, file_ploadv_cleanup_fn);
63 state->subreq = wait_for_read_send(state, state->ev, state->fd, false);
64 if (tevent_req_nomem(state->subreq, req)) {
65 return tevent_req_post(req, ev);
67 tevent_req_set_callback(state->subreq, file_ploadv_readable, req);
68 return req;
71 static void file_ploadv_cleanup_fn(
72 struct tevent_req *req, enum tevent_req_state req_state)
74 struct file_ploadv_state *state = tevent_req_data(
75 req, struct file_ploadv_state);
77 TALLOC_FREE(state->subreq);
78 if (state->fd != -1) {
79 sys_pclose(state->fd);
80 state->fd = -1;
84 static void file_ploadv_readable(struct tevent_req *subreq)
86 struct tevent_req *req = tevent_req_callback_data(
87 subreq, struct tevent_req);
88 struct file_ploadv_state *state = tevent_req_data(
89 req, struct file_ploadv_state);
90 uint8_t buf[1024];
91 uint8_t *tmp;
92 ssize_t nread;
93 size_t bufsize;
94 int err;
95 bool ok;
97 ok = wait_for_read_recv(subreq, &err);
98 TALLOC_FREE(subreq);
99 state->subreq = NULL;
100 if (!ok) {
101 tevent_req_error(req, err);
102 return;
105 nread = sys_read(state->fd, buf, sizeof(buf));
106 if (nread == -1) {
107 tevent_req_error(req, errno);
108 return;
110 if (nread == 0) {
111 tevent_req_done(req);
112 return;
115 bufsize = talloc_get_size(state->buf);
116 if (bufsize > 0) {
118 * Last round we've added the trailing '\0'. Remove it
119 * for this round.
121 bufsize -= 1;
124 if (((bufsize + nread) < bufsize) ||
125 ((bufsize + nread + 1) < bufsize)) {
126 /* overflow */
127 tevent_req_error(req, EMSGSIZE);
128 return;
131 if ((state->maxsize != 0) && ((bufsize + nread) > state->maxsize)) {
132 tevent_req_error(req, EMSGSIZE);
133 return;
136 tmp = talloc_realloc(state, state->buf, uint8_t, bufsize + nread + 1);
137 if (tevent_req_nomem(tmp, req)) {
138 return;
140 state->buf = tmp;
142 memcpy(state->buf + bufsize, buf, nread);
143 state->buf[bufsize+nread] = '\0';
145 state->subreq = wait_for_read_send(state, state->ev, state->fd, false);
146 if (tevent_req_nomem(state->subreq, req)) {
147 return;
149 tevent_req_set_callback(state->subreq, file_ploadv_readable, req);
152 int file_ploadv_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
153 uint8_t **buf)
155 struct file_ploadv_state *state = tevent_req_data(
156 req, struct file_ploadv_state);
157 int err;
159 if (tevent_req_is_unix_error(req, &err)) {
160 return err;
162 *buf = talloc_move(mem_ctx, &state->buf);
164 tevent_req_received(req);
166 return 0;
171 Load a pipe into memory and return an array of pointers to lines in the data
172 must be freed with TALLOC_FREE.
175 char **file_lines_ploadv(TALLOC_CTX *mem_ctx,
176 char * const argl[],
177 int *numlines)
179 char *p = NULL;
180 size_t size;
182 p = file_ploadv(argl, &size);
183 if (!p) {
184 return NULL;
187 return file_lines_parse(p, size, numlines, mem_ctx);