00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "includes.h"
00022
00023 struct sync_context {
00024 TALLOC_CTX *mem_ctx;
00025 struct cli_state *cli;
00026 char *remote_path;
00027 char *local_path;
00028 pstring mask;
00029 uint16 attribute;
00030 };
00031
00032 static void gpo_sync_func(const char *mnt,
00033 file_info *info,
00034 const char *mask,
00035 void *state);
00036
00037 NTSTATUS gpo_copy_file(TALLOC_CTX *mem_ctx,
00038 struct cli_state *cli,
00039 const char *nt_path,
00040 const char *unix_path)
00041 {
00042 NTSTATUS result;
00043 int fnum;
00044 int fd = 0;
00045 char *data = NULL;
00046 static int io_bufsize = 64512;
00047 int read_size = io_bufsize;
00048 off_t start = 0;
00049 off_t nread = 0;
00050
00051 if ((fnum = cli_open(cli, nt_path, O_RDONLY, DENY_NONE)) == -1) {
00052 result = NT_STATUS_NO_SUCH_FILE;
00053 goto out;
00054 }
00055
00056 if ((fd = sys_open(unix_path, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
00057 result = map_nt_error_from_unix(errno);
00058 goto out;
00059 }
00060
00061 if ((data = (char *)SMB_MALLOC(read_size)) == NULL) {
00062 result = NT_STATUS_NO_MEMORY;
00063 goto out;
00064 }
00065
00066 while (1) {
00067
00068 int n = cli_read(cli, fnum, data, nread + start, read_size);
00069
00070 if (n <= 0)
00071 break;
00072
00073 if (write(fd, data, n) != n) {
00074 break;
00075 }
00076
00077 nread += n;
00078 }
00079
00080 result = NT_STATUS_OK;
00081
00082 out:
00083 SAFE_FREE(data);
00084 if (fnum) {
00085 cli_close(cli, fnum);
00086 }
00087 if (fd) {
00088 close(fd);
00089 }
00090
00091 return result;
00092 }
00093
00094
00095
00096
00097
00098 static NTSTATUS gpo_copy_dir(const char *unix_path)
00099 {
00100 if ((mkdir(unix_path, 0644)) < 0 && errno != EEXIST) {
00101 return NT_STATUS_ACCESS_DENIED;
00102 }
00103
00104 return NT_STATUS_OK;
00105 }
00106
00107
00108
00109
00110
00111 static BOOL gpo_sync_files(struct sync_context *ctx)
00112 {
00113 DEBUG(3,("calling cli_list with mask: %s\n", ctx->mask));
00114
00115 if (cli_list(ctx->cli, ctx->mask, ctx->attribute, gpo_sync_func, ctx) == -1) {
00116 DEBUG(1,("listing [%s] failed with error: %s\n",
00117 ctx->mask, cli_errstr(ctx->cli)));
00118 return False;
00119 }
00120
00121 return True;
00122 }
00123
00124
00125
00126
00127
00128 static void gpo_sync_func(const char *mnt,
00129 file_info *info,
00130 const char *mask,
00131 void *state)
00132 {
00133 NTSTATUS result;
00134 struct sync_context *ctx;
00135 fstring nt_filename, unix_filename;
00136 fstring nt_dir, unix_dir;
00137 char *old_nt_dir, *old_unix_dir;
00138
00139 ctx = (struct sync_context *)state;
00140
00141 if (strequal(info->name, ".") || strequal(info->name, "..")) {
00142 return;
00143 }
00144
00145 DEBUG(5,("gpo_sync_func: got mask: [%s], name: [%s]\n",
00146 mask, info->name));
00147
00148 if (info->mode & aDIR) {
00149
00150 DEBUG(3,("got dir: [%s]\n", info->name));
00151
00152 fstrcpy(nt_dir, ctx->remote_path);
00153 fstrcat(nt_dir, "\\");
00154 fstrcat(nt_dir, info->name);
00155
00156 fstrcpy(unix_dir, ctx->local_path);
00157 fstrcat(unix_dir, "/");
00158 fstrcat(unix_dir, info->name);
00159
00160 result = gpo_copy_dir(unix_dir);
00161 if (!NT_STATUS_IS_OK(result)) {
00162 DEBUG(1,("failed to copy dir: %s\n", nt_errstr(result)));
00163 }
00164
00165 old_nt_dir = ctx->remote_path;
00166 ctx->remote_path = nt_dir;
00167
00168 old_unix_dir = ctx->local_path;
00169 ctx->local_path = talloc_strdup(ctx->mem_ctx, unix_dir);
00170
00171 pstrcpy(ctx->mask, nt_dir);
00172 pstrcat(ctx->mask, "\\*");
00173
00174 if (!gpo_sync_files(ctx)) {
00175 DEBUG(0,("could not sync files\n"));
00176 }
00177
00178 ctx->remote_path = old_nt_dir;
00179 ctx->local_path = old_unix_dir;
00180 return;
00181 }
00182
00183 DEBUG(3,("got file: [%s]\n", info->name));
00184
00185 fstrcpy(nt_filename, ctx->remote_path);
00186 fstrcat(nt_filename, "\\");
00187 fstrcat(nt_filename, info->name);
00188
00189 fstrcpy(unix_filename, ctx->local_path);
00190 fstrcat(unix_filename, "/");
00191 fstrcat(unix_filename, info->name);
00192
00193 result = gpo_copy_file(ctx->mem_ctx, ctx->cli, nt_filename, unix_filename);
00194 if (!NT_STATUS_IS_OK(result)) {
00195 DEBUG(1,("failed to copy file: %s\n", nt_errstr(result)));
00196 }
00197 }
00198
00199
00200
00201
00202
00203
00204 NTSTATUS gpo_sync_directories(TALLOC_CTX *mem_ctx,
00205 struct cli_state *cli,
00206 const char *nt_path,
00207 const char *local_path)
00208 {
00209 struct sync_context ctx;
00210
00211 ctx.mem_ctx = mem_ctx;
00212 ctx.cli = cli;
00213 ctx.remote_path = CONST_DISCARD(char *, nt_path);
00214 ctx.local_path = CONST_DISCARD(char *, local_path);
00215 ctx.attribute = (aSYSTEM | aHIDDEN | aDIR);
00216
00217 pstrcpy(ctx.mask, nt_path);
00218 pstrcat(ctx.mask, "\\*");
00219
00220 if (!gpo_sync_files(&ctx)) {
00221 return NT_STATUS_NO_SUCH_FILE;
00222 }
00223
00224 return NT_STATUS_OK;
00225 }