s3:smbd: rename has_ctdb_public_ip to has_cluster_movable_ip
[Samba.git] / libgpo / gpo_filesync.c
blob3cd9470655121353d5f8f3362f1b184dced449ab
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 "system/filesys.h"
22 #include "libsmb/libsmb.h"
23 #include "../libgpo/gpo.h"
24 #include "libgpo/gpo_proto.h"
25 #include "lib/util/string_wrappers.h"
27 struct sync_context {
28 TALLOC_CTX *mem_ctx;
29 struct cli_state *cli;
30 char *remote_path;
31 char *local_path;
32 char *mask;
33 uint16_t attribute;
36 static NTSTATUS gpo_sync_func(const char *mnt,
37 struct file_info *info,
38 const char *mask,
39 void *state);
41 NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
42 struct cli_state *cli,
43 const char *nt_path,
44 const char *unix_path)
46 NTSTATUS result;
47 uint16_t fnum;
48 int fd = -1;
49 char *data = NULL;
50 static int io_bufsize = 64512;
51 int read_size = io_bufsize;
52 off_t nread = 0;
54 result = cli_open(cli, nt_path, O_RDONLY, DENY_NONE, &fnum);
55 if (!NT_STATUS_IS_OK(result)) {
56 goto out;
59 if ((fd = open(unix_path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
60 result = map_nt_error_from_unix(errno);
61 goto out;
64 if ((data = (char *)SMB_MALLOC(read_size)) == NULL) {
65 result = NT_STATUS_NO_MEMORY;
66 goto out;
69 while (1) {
70 size_t n = 0;
72 result = cli_read(cli, fnum, data, nread, read_size, &n);
73 if (!NT_STATUS_IS_OK(result)) {
74 goto out;
77 if (n == 0)
78 break;
80 if (write(fd, data, n) != n) {
81 break;
84 nread += n;
87 result = NT_STATUS_OK;
89 out:
90 SAFE_FREE(data);
91 if (fnum) {
92 cli_close(cli, fnum);
94 if (fd != -1) {
95 close(fd);
98 return result;
101 /****************************************************************
102 copy dir
103 ****************************************************************/
105 static NTSTATUS gpo_copy_dir(const char *unix_path)
107 if ((mkdir(unix_path, 0644)) < 0 && errno != EEXIST) {
108 return map_nt_error_from_unix(errno);
111 return NT_STATUS_OK;
114 /****************************************************************
115 sync files
116 ****************************************************************/
118 static NTSTATUS gpo_sync_files(struct sync_context *ctx)
120 NTSTATUS status;
122 DEBUG(3,("calling cli_list with mask: %s\n", ctx->mask));
124 status = cli_list(ctx->cli, ctx->mask, ctx->attribute, gpo_sync_func,
125 ctx);
126 if (!NT_STATUS_IS_OK(status)) {
127 DEBUG(1, ("listing [%s] failed with error: %s\n",
128 ctx->mask, nt_errstr(status)));
129 return status;
132 return status;
135 /****************************************************************
136 syncronisation call back
137 ****************************************************************/
139 static NTSTATUS gpo_sync_func(const char *mnt,
140 struct file_info *info,
141 const char *mask,
142 void *state)
144 NTSTATUS result;
145 struct sync_context *ctx;
146 fstring nt_filename, unix_filename;
147 fstring nt_dir, unix_dir;
148 char *old_nt_dir, *old_unix_dir;
150 ctx = (struct sync_context *)state;
152 if (strequal(info->name, ".") || strequal(info->name, "..")) {
153 return NT_STATUS_OK;
156 DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n",
157 mask, info->name));
159 if (info->attr & FILE_ATTRIBUTE_DIRECTORY) {
161 DEBUG(3,("got dir: [%s]\n", info->name));
163 fstrcpy(nt_dir, ctx->remote_path);
164 fstrcat(nt_dir, "\\");
165 fstrcat(nt_dir, info->name);
167 fstrcpy(unix_dir, ctx->local_path);
168 fstrcat(unix_dir, "/");
169 fstrcat(unix_dir, info->name);
171 result = gpo_copy_dir(unix_dir);
172 if (!NT_STATUS_IS_OK(result)) {
173 DEBUG(1,("failed to copy dir: %s\n",
174 nt_errstr(result)));
175 return result;
178 old_nt_dir = ctx->remote_path;
179 ctx->remote_path = talloc_strdup(ctx->mem_ctx, nt_dir);
181 old_unix_dir = ctx->local_path;
182 ctx->local_path = talloc_strdup(ctx->mem_ctx, unix_dir);
184 ctx->mask = talloc_asprintf(ctx->mem_ctx,
185 "%s\\*",
186 nt_dir);
187 if (!ctx->local_path || !ctx->mask || !ctx->remote_path) {
188 DEBUG(0,("gpo_sync_func: ENOMEM\n"));
189 return NT_STATUS_NO_MEMORY;
191 result = gpo_sync_files(ctx);
192 if (!NT_STATUS_IS_OK(result)) {
193 DEBUG(0,("could not sync files\n"));
194 return result;
197 ctx->remote_path = old_nt_dir;
198 ctx->local_path = old_unix_dir;
199 return NT_STATUS_OK;
202 DEBUG(3,("got file: [%s]\n", info->name));
204 fstrcpy(nt_filename, ctx->remote_path);
205 fstrcat(nt_filename, "\\");
206 fstrcat(nt_filename, info->name);
208 fstrcpy(unix_filename, ctx->local_path);
209 fstrcat(unix_filename, "/");
210 fstrcat(unix_filename, info->name);
212 result = gpo_copy_file(ctx->mem_ctx, ctx->cli,
213 nt_filename, unix_filename);
214 if (!NT_STATUS_IS_OK(result)) {
215 DEBUG(1,("failed to copy file: %s\n",
216 nt_errstr(result)));
218 return result;
222 /****************************************************************
223 list a remote directory and download recursively
224 ****************************************************************/
226 NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx,
227 struct cli_state *cli,
228 const char *nt_path,
229 const char *local_path)
231 struct sync_context ctx;
233 ctx.mem_ctx = mem_ctx;
234 ctx.cli = cli;
235 ctx.remote_path = discard_const_p(char, nt_path);
236 ctx.local_path = discard_const_p(char, local_path);
237 ctx.attribute = (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY);
239 ctx.mask = talloc_asprintf(mem_ctx,
240 "%s\\*",
241 nt_path);
242 if (!ctx.mask) {
243 return NT_STATUS_NO_MEMORY;
246 return gpo_sync_files(&ctx);