s3:libsmb: add tstream_is_cli_np()
[Samba/gebeck_regimport.git] / source3 / libgpo / gpo_filesync.c
blob4e3b93809ef835c2e6dfc6f68bf0d9f3ed9c56b8
1 /*
2 * Unix SMB/CIFS implementation.
3 * Group Policy Object Support
4 * Copyright (C) Guenther Deschner 2006
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "../libgpo/gpo.h"
22 #include "libgpo/gpo_proto.h"
24 struct sync_context {
25 TALLOC_CTX *mem_ctx;
26 struct cli_state *cli;
27 char *remote_path;
28 char *local_path;
29 char *mask;
30 uint16_t attribute;
33 static NTSTATUS gpo_sync_func(const char *mnt,
34 struct file_info *info,
35 const char *mask,
36 void *state);
38 NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
39 struct cli_state *cli,
40 const char *nt_path,
41 const char *unix_path)
43 NTSTATUS result;
44 uint16_t fnum;
45 int fd = -1;
46 char *data = NULL;
47 static int io_bufsize = 64512;
48 int read_size = io_bufsize;
49 off_t nread = 0;
51 result = cli_open(cli, nt_path, O_RDONLY, DENY_NONE, &fnum);
52 if (!NT_STATUS_IS_OK(result)) {
53 goto out;
56 if ((fd = sys_open(unix_path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
57 result = map_nt_error_from_unix(errno);
58 goto out;
61 if ((data = (char *)SMB_MALLOC(read_size)) == NULL) {
62 result = NT_STATUS_NO_MEMORY;
63 goto out;
66 while (1) {
68 int n = cli_read(cli, fnum, data, nread, read_size);
70 if (n <= 0)
71 break;
73 if (write(fd, data, n) != n) {
74 break;
77 nread += n;
80 result = NT_STATUS_OK;
82 out:
83 SAFE_FREE(data);
84 if (fnum) {
85 cli_close(cli, fnum);
87 if (fd != -1) {
88 close(fd);
91 return result;
94 /****************************************************************
95 copy dir
96 ****************************************************************/
98 static NTSTATUS gpo_copy_dir(const char *unix_path)
100 if ((mkdir(unix_path, 0644)) < 0 && errno != EEXIST) {
101 return map_nt_error_from_unix(errno);
104 return NT_STATUS_OK;
107 /****************************************************************
108 sync files
109 ****************************************************************/
111 static NTSTATUS gpo_sync_files(struct sync_context *ctx)
113 NTSTATUS status;
115 DEBUG(3,("calling cli_list with mask: %s\n", ctx->mask));
117 status = cli_list(ctx->cli, ctx->mask, ctx->attribute, gpo_sync_func,
118 ctx);
119 if (!NT_STATUS_IS_OK(status)) {
120 DEBUG(1, ("listing [%s] failed with error: %s\n",
121 ctx->mask, nt_errstr(status)));
122 return status;
125 return status;
128 /****************************************************************
129 syncronisation call back
130 ****************************************************************/
132 static NTSTATUS gpo_sync_func(const char *mnt,
133 struct file_info *info,
134 const char *mask,
135 void *state)
137 NTSTATUS result;
138 struct sync_context *ctx;
139 fstring nt_filename, unix_filename;
140 fstring nt_dir, unix_dir;
141 char *old_nt_dir, *old_unix_dir;
143 ctx = (struct sync_context *)state;
145 if (strequal(info->name, ".") || strequal(info->name, "..")) {
146 return NT_STATUS_OK;
149 DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n",
150 mask, info->name));
152 if (info->mode & aDIR) {
154 DEBUG(3,("got dir: [%s]\n", info->name));
156 fstrcpy(nt_dir, ctx->remote_path);
157 fstrcat(nt_dir, "\\");
158 fstrcat(nt_dir, info->name);
160 fstrcpy(unix_dir, ctx->local_path);
161 fstrcat(unix_dir, "/");
162 fstrcat(unix_dir, info->name);
164 result = gpo_copy_dir(unix_dir);
165 if (!NT_STATUS_IS_OK(result)) {
166 DEBUG(1,("failed to copy dir: %s\n",
167 nt_errstr(result)));
168 return result;
171 old_nt_dir = ctx->remote_path;
172 ctx->remote_path = talloc_strdup(ctx->mem_ctx, nt_dir);
174 old_unix_dir = ctx->local_path;
175 ctx->local_path = talloc_strdup(ctx->mem_ctx, unix_dir);
177 ctx->mask = talloc_asprintf(ctx->mem_ctx,
178 "%s\\*",
179 nt_dir);
180 if (!ctx->local_path || !ctx->mask || !ctx->remote_path) {
181 DEBUG(0,("gpo_sync_func: ENOMEM\n"));
182 return NT_STATUS_NO_MEMORY;
184 result = gpo_sync_files(ctx);
185 if (!NT_STATUS_IS_OK(result)) {
186 DEBUG(0,("could not sync files\n"));
187 return result;
190 ctx->remote_path = old_nt_dir;
191 ctx->local_path = old_unix_dir;
192 return NT_STATUS_OK;
195 DEBUG(3,("got file: [%s]\n", info->name));
197 fstrcpy(nt_filename, ctx->remote_path);
198 fstrcat(nt_filename, "\\");
199 fstrcat(nt_filename, info->name);
201 fstrcpy(unix_filename, ctx->local_path);
202 fstrcat(unix_filename, "/");
203 fstrcat(unix_filename, info->name);
205 result = gpo_copy_file(ctx->mem_ctx, ctx->cli,
206 nt_filename, unix_filename);
207 if (!NT_STATUS_IS_OK(result)) {
208 DEBUG(1,("failed to copy file: %s\n",
209 nt_errstr(result)));
211 return result;
215 /****************************************************************
216 list a remote directory and download recursivly
217 ****************************************************************/
219 NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx,
220 struct cli_state *cli,
221 const char *nt_path,
222 const char *local_path)
224 struct sync_context ctx;
226 ctx.mem_ctx = mem_ctx;
227 ctx.cli = cli;
228 ctx.remote_path = CONST_DISCARD(char *, nt_path);
229 ctx.local_path = CONST_DISCARD(char *, local_path);
230 ctx.attribute = (aSYSTEM | aHIDDEN | aDIR);
232 ctx.mask = talloc_asprintf(mem_ctx,
233 "%s\\*",
234 nt_path);
235 if (!ctx.mask) {
236 return NT_STATUS_NO_MEMORY;
239 return gpo_sync_files(&ctx);