PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/csync_util.c

https://gitlab.com/rpdev/csync
C | 349 lines | 259 code | 57 blank | 33 comment | 42 complexity | b4ac60d7f446ef3f956533cabaa2650e MD5 | raw file
  1. /*
  2. * libcsync -- a library to sync a directory with another
  3. *
  4. * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
  5. * Copyright (c) 2012-2013 by Klaas Freitag <freitag@owncloud.com>wie
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #ifndef _GNU_SOURCE
  23. #define _GNU_SOURCE
  24. #endif
  25. #include <errno.h>
  26. #include <limits.h>
  27. #include <stdio.h>
  28. #include "c_jhash.h"
  29. #include "csync_util.h"
  30. #include "vio/csync_vio.h"
  31. #define CSYNC_LOG_CATEGORY_NAME "csync.util"
  32. #include "csync_log.h"
  33. typedef struct {
  34. const char *instr_str;
  35. enum csync_instructions_e instr_code;
  36. } _instr_code_struct;
  37. static const _instr_code_struct _instr[] =
  38. {
  39. { "INSTRUCTION_NONE", CSYNC_INSTRUCTION_NONE },
  40. { "INSTRUCTION_EVAL", CSYNC_INSTRUCTION_EVAL },
  41. { "INSTRUCTION_REMOVE", CSYNC_INSTRUCTION_REMOVE },
  42. { "INSTRUCTION_RENAME", CSYNC_INSTRUCTION_RENAME },
  43. { "INSTRUCTION_NEW", CSYNC_INSTRUCTION_NEW },
  44. { "INSTRUCTION_CONFLICT", CSYNC_INSTRUCTION_CONFLICT },
  45. { "INSTRUCTION_IGNORE", CSYNC_INSTRUCTION_IGNORE },
  46. { "INSTRUCTION_SYNC", CSYNC_INSTRUCTION_SYNC },
  47. { "INSTRUCTION_STAT_ERR", CSYNC_INSTRUCTION_STAT_ERROR },
  48. { "INSTRUCTION_ERROR", CSYNC_INSTRUCTION_ERROR },
  49. { "INSTRUCTION_DELETED", CSYNC_INSTRUCTION_DELETED },
  50. { "INSTRUCTION_UPDATED", CSYNC_INSTRUCTION_UPDATED },
  51. { NULL, CSYNC_INSTRUCTION_ERROR }
  52. };
  53. struct csync_memstat_s {
  54. int size;
  55. int resident;
  56. int shared;
  57. int trs;
  58. int drs;
  59. int lrs;
  60. int dt;
  61. };
  62. const char *csync_instruction_str(enum csync_instructions_e instr)
  63. {
  64. int idx = 0;
  65. while (_instr[idx].instr_str != NULL) {
  66. if (_instr[idx].instr_code == instr) {
  67. return _instr[idx].instr_str;
  68. }
  69. idx++;
  70. }
  71. return "ERROR!";
  72. }
  73. void csync_memstat_check(void) {
  74. int s = 0;
  75. struct csync_memstat_s m;
  76. FILE* fp;
  77. /* get process memory stats */
  78. fp = fopen("/proc/self/statm","r");
  79. if (fp == NULL) {
  80. return;
  81. }
  82. s = fscanf(fp, "%d%d%d%d%d%d%d", &m.size, &m.resident, &m.shared, &m.trs,
  83. &m.drs, &m.lrs, &m.dt);
  84. fclose(fp);
  85. if (s == EOF) {
  86. return;
  87. }
  88. CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "Memory: %dK total size, %dK resident, %dK shared",
  89. m.size * 4, m.resident * 4, m.shared * 4);
  90. }
  91. static int _merge_file_trees_visitor(void *obj, void *data) {
  92. csync_file_stat_t *fs = NULL;
  93. csync_vio_file_stat_t *vst = NULL;
  94. CSYNC *ctx = NULL;
  95. c_rbtree_t *tree = NULL;
  96. c_rbnode_t *node = NULL;
  97. char errbuf[256] = {0};
  98. char *uri = NULL;
  99. int rc = -1;
  100. fs = (csync_file_stat_t *) obj;
  101. ctx = (CSYNC *) data;
  102. /* search for UPDATED file */
  103. if (fs->instruction != CSYNC_INSTRUCTION_UPDATED) {
  104. rc = 0;
  105. goto out;
  106. }
  107. switch (ctx->current) {
  108. case LOCAL_REPLICA:
  109. tree = ctx->local.tree;
  110. break;
  111. case REMOTE_REPLICA:
  112. tree = ctx->remote.tree;
  113. break;
  114. default:
  115. break;
  116. }
  117. /* check if the file is new or has been synced */
  118. node = c_rbtree_find(tree, &fs->phash);
  119. if (node == NULL) {
  120. csync_file_stat_t *new = NULL;
  121. new = c_malloc(sizeof(csync_file_stat_t) + fs->pathlen + 1);
  122. if (new == NULL) {
  123. strerror_r(errno, errbuf, sizeof(errbuf));
  124. CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR,
  125. "file: %s, merge malloc, error: %s",
  126. fs->path,
  127. errbuf);
  128. rc = -1;
  129. goto out;
  130. }
  131. new = memcpy(new, fs, sizeof(csync_file_stat_t) + fs->pathlen + 1);
  132. if (c_rbtree_insert(tree, new) < 0) {
  133. strerror_r(errno, errbuf, sizeof(errbuf));
  134. SAFE_FREE(new);
  135. CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR,
  136. "file: %s, rb tree insert, error: %s",
  137. fs->path,
  138. errbuf);
  139. rc = -1;
  140. goto out;
  141. }
  142. node = c_rbtree_find(tree, &fs->phash);
  143. if (node == NULL) {
  144. rc = -1;
  145. CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Unable to find node");
  146. goto out;
  147. }
  148. }
  149. fs = c_rbtree_node_data(node);
  150. switch (ctx->current) {
  151. case LOCAL_REPLICA:
  152. if (asprintf(&uri, "%s/%s", ctx->local.uri, fs->path) < 0) {
  153. rc = -1;
  154. strerror_r(errno, errbuf, sizeof(errbuf));
  155. CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "file uri alloc failed: %s",
  156. errbuf);
  157. goto out;
  158. }
  159. break;
  160. case REMOTE_REPLICA:
  161. if (asprintf(&uri, "%s/%s", ctx->remote.uri, fs->path) < 0) {
  162. rc = -1;
  163. strerror_r(errno, errbuf, sizeof(errbuf));
  164. CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "file uri alloc failed: %s",
  165. errbuf);
  166. goto out;
  167. }
  168. break;
  169. default:
  170. break;
  171. }
  172. /* get file stat of the file on the replica */
  173. vst = csync_vio_file_stat_new();
  174. if (csync_vio_stat(ctx, uri, vst) < 0) {
  175. rc = -1;
  176. strerror_r(errno, errbuf, sizeof(errbuf));
  177. CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR,
  178. "file: %s, updating stat failed, error: %s",
  179. uri,
  180. errbuf);
  181. goto out;
  182. }
  183. /* update file stat */
  184. fs->inode = vst->inode;
  185. fs->modtime = vst->mtime;
  186. CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "file: %s, instruction: UPDATED", uri);
  187. fs->instruction = CSYNC_INSTRUCTION_NONE;
  188. rc = 0;
  189. out:
  190. csync_vio_file_stat_destroy(vst);
  191. SAFE_FREE(uri);
  192. if (rc != 0) {
  193. fs->instruction = CSYNC_INSTRUCTION_ERROR;
  194. }
  195. return rc;
  196. }
  197. /*
  198. * merge the local tree with the new files from remote and update the
  199. * inode numbers
  200. */
  201. int csync_merge_file_trees(CSYNC *ctx) {
  202. int rc = -1;
  203. /* walk over remote tree, stat on local system */
  204. ctx->current = LOCAL_REPLICA;
  205. ctx->replica = ctx->local.type;
  206. rc = c_rbtree_walk(ctx->remote.tree, ctx, _merge_file_trees_visitor);
  207. if (rc < 0) {
  208. goto out;
  209. }
  210. #if 0
  211. /* We don't have to merge the remote tree atm. */
  212. /* walk over local tree, stat on remote system */
  213. ctx->current = REMOTE_REPLICA;
  214. ctx->replica = ctx->remote.type;
  215. rc = c_rbtree_walk(ctx->local.tree, ctx, _merge_file_trees_visitor);
  216. if (rc < 0) {
  217. goto out;
  218. }
  219. #endif
  220. out:
  221. return rc;
  222. }
  223. int csync_unix_extensions(CSYNC *ctx) {
  224. int rc = -1;
  225. char *uri = NULL;
  226. csync_vio_handle_t *fp = NULL;
  227. ctx->options.unix_extensions = 0;
  228. rc = asprintf(&uri, "%s/csync_unix_extension*test.ctmp", ctx->remote.uri);
  229. if (rc < 0) {
  230. goto out;
  231. }
  232. ctx->replica = ctx->remote.type;
  233. fp = csync_vio_creat(ctx, uri, 0644);
  234. if (fp == NULL) {
  235. rc = 0;
  236. CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO,
  237. "Disabled unix filesystem synchronization");
  238. goto out;
  239. }
  240. csync_vio_close(ctx, fp);
  241. ctx->options.unix_extensions = 1;
  242. CSYNC_LOG(CSYNC_LOG_PRIORITY_INFO, "Enabled unix filesystem synchronization");
  243. rc = 1;
  244. out:
  245. csync_vio_unlink(ctx, uri);
  246. SAFE_FREE(uri);
  247. return rc;
  248. }
  249. csync_vio_file_stat_t *csync_vio_convert_file_stat(csync_file_stat_t *st) {
  250. csync_vio_file_stat_t *vfs = NULL;
  251. if (st == NULL) {
  252. return NULL;
  253. }
  254. vfs = csync_vio_file_stat_new();
  255. if (vfs == NULL) {
  256. return NULL;
  257. }
  258. vfs->acl = NULL;
  259. if (st->pathlen > 0) {
  260. vfs->name = c_strdup(st->path);
  261. }
  262. vfs->uid = st->uid;
  263. vfs->gid = st->gid;
  264. vfs->atime = 0;
  265. vfs->mtime = st->modtime;
  266. vfs->ctime = 0;
  267. vfs->size = st->size;
  268. vfs->blksize = 0; /* Depricated. */
  269. vfs->blkcount = 0;
  270. vfs->mode = st->mode;
  271. vfs->device = 0;
  272. vfs->inode = st->inode;
  273. vfs->nlink = st->nlink;
  274. /* fields. */
  275. vfs->fields = CSYNC_VIO_FILE_STAT_FIELDS_TYPE
  276. + CSYNC_VIO_FILE_STAT_FIELDS_PERMISSIONS
  277. + CSYNC_VIO_FILE_STAT_FIELDS_INODE
  278. + CSYNC_VIO_FILE_STAT_FIELDS_LINK_COUNT
  279. + CSYNC_VIO_FILE_STAT_FIELDS_SIZE
  280. + CSYNC_VIO_FILE_STAT_FIELDS_MTIME
  281. + CSYNC_VIO_FILE_STAT_FIELDS_UID
  282. + CSYNC_VIO_FILE_STAT_FIELDS_GID;
  283. if (st->type == CSYNC_FTW_TYPE_DIR)
  284. vfs->type = CSYNC_VIO_FILE_TYPE_DIRECTORY;
  285. else if (st->type == CSYNC_FTW_TYPE_FILE)
  286. vfs->type = CSYNC_VIO_FILE_TYPE_REGULAR;
  287. else if (st->type == CSYNC_FTW_TYPE_SLINK)
  288. vfs->type = CSYNC_VIO_FILE_TYPE_SYMBOLIC_LINK;
  289. else
  290. vfs->type = CSYNC_VIO_FILE_TYPE_UNKNOWN;
  291. return vfs;
  292. }