python: Fix erroneous increments of reference counts
[Samba.git] / source3 / lib / util_file.c
blob1c468ea5501debdcc3c7954c86e5e88b8cbe3c0f
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_file.h"
22 #include "lib/util/debug.h"
23 #include "lib/util/samba_util.h"
24 #include "lib/util/sys_rw.h"
25 #include "lib/util/sys_popen.h"
26 #include "lib/async_req/async_sock.h"
27 #include "lib/util/tevent_unix.h"
29 struct file_ploadv_state {
30 struct tevent_context *ev;
31 size_t maxsize;
32 int fd;
33 uint8_t *buf;
36 static int file_ploadv_state_destructor(struct file_ploadv_state *s);
37 static void file_ploadv_readable(struct tevent_req *subreq);
39 struct tevent_req *file_ploadv_send(TALLOC_CTX *mem_ctx,
40 struct tevent_context *ev,
41 char * const argl[], size_t maxsize)
43 struct tevent_req *req = NULL, *subreq = NULL;
44 struct file_ploadv_state *state = NULL;
46 req = tevent_req_create(mem_ctx, &state, struct file_ploadv_state);
47 if (req == NULL) {
48 return NULL;
50 state->ev = ev;
51 state->maxsize = maxsize;
53 state->fd = sys_popenv(argl);
54 if (state->fd == -1) {
55 tevent_req_error(req, errno);
56 return tevent_req_post(req, ev);
58 talloc_set_destructor(state, file_ploadv_state_destructor);
60 subreq = wait_for_read_send(state, state->ev, state->fd, false);
61 if (tevent_req_nomem(subreq, req)) {
62 return tevent_req_post(req, ev);
64 tevent_req_set_callback(subreq, file_ploadv_readable, req);
65 return req;
68 static int file_ploadv_state_destructor(struct file_ploadv_state *s)
70 if (s->fd != -1) {
71 sys_pclose(s->fd);
72 s->fd = -1;
74 return 0;
77 static void file_ploadv_readable(struct tevent_req *subreq)
79 struct tevent_req *req = tevent_req_callback_data(
80 subreq, struct tevent_req);
81 struct file_ploadv_state *state = tevent_req_data(
82 req, struct file_ploadv_state);
83 uint8_t buf[1024];
84 uint8_t *tmp;
85 ssize_t nread;
86 size_t bufsize;
87 int err;
88 bool ok;
90 ok = wait_for_read_recv(subreq, &err);
91 TALLOC_FREE(subreq);
92 if (!ok) {
93 tevent_req_error(req, err);
94 return;
97 nread = sys_read(state->fd, buf, sizeof(buf));
98 if (nread == -1) {
99 tevent_req_error(req, errno);
100 return;
102 if (nread == 0) {
103 tevent_req_done(req);
104 return;
107 bufsize = talloc_get_size(state->buf);
109 if (((bufsize + nread) < bufsize) ||
110 ((bufsize + nread + 1) < bufsize)) {
111 /* overflow */
112 tevent_req_error(req, EMSGSIZE);
113 return;
116 if ((state->maxsize != 0) && ((bufsize + nread) > state->maxsize)) {
117 tevent_req_error(req, EMSGSIZE);
118 return;
121 tmp = talloc_realloc(state, state->buf, uint8_t, bufsize + nread + 1);
122 if (tevent_req_nomem(tmp, req)) {
123 return;
125 state->buf = tmp;
127 memcpy(state->buf + bufsize, buf, nread);
128 state->buf[bufsize+nread] = '\0';
130 subreq = wait_for_read_send(state, state->ev, state->fd, false);
131 if (tevent_req_nomem(subreq, req)) {
132 return;
134 tevent_req_set_callback(subreq, file_ploadv_readable, req);
137 int file_ploadv_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
138 uint8_t **buf)
140 struct file_ploadv_state *state = tevent_req_data(
141 req, struct file_ploadv_state);
142 int err;
144 if (tevent_req_is_unix_error(req, &err)) {
145 return err;
147 *buf = talloc_move(mem_ctx, &state->buf);
149 tevent_req_received(req);
151 return 0;
156 Load a pipe into memory and return an array of pointers to lines in the data
157 must be freed with TALLOC_FREE.
160 char **file_lines_ploadv(TALLOC_CTX *mem_ctx,
161 char * const argl[],
162 int *numlines)
164 char *p = NULL;
165 size_t size;
167 p = file_ploadv(argl, &size);
168 if (!p) {
169 return NULL;
172 return file_lines_parse(p, size, numlines, mem_ctx);