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

/support/grdiff.c

http://archfs.googlecode.com/
C | 350 lines | 287 code | 55 blank | 8 comment | 123 complexity | 9b5acce86349aecb1880d2b73a2b69c0 MD5 | raw file
  1. #include "grdiff.h"
  2. // prototypes:
  3. int unzip(char *path);
  4. int unzip_revs(char *path){
  5. DIR *dir = NULL;
  6. int rev_count = 0;
  7. struct dirent *entry = NULL;
  8. char *mirror = NULL;
  9. char *extension = NULL;
  10. int descriptor = 0;
  11. #ifdef DEBUG_DEEP
  12. printf("[Function: unzip_revs] Unzipping revisions in %s directory;\n", path);
  13. #endif
  14. if ((dir = opendir(path)) == NULL)
  15. return -1;
  16. for (entry = readdir(dir); entry != NULL; entry = readdir(dir)){
  17. if (gstrsub(entry->d_name, "mirror_metadata.") == 0){
  18. if (gmstrcpy(&mirror, path, "/", entry->d_name, 0) == -1)
  19. continue;
  20. extension = gpthext(entry->d_name);
  21. if (strcmp(extension, "gz") == 0){
  22. gstrdel(extension);
  23. if (unzip(mirror) == -1)
  24. continue;
  25. }
  26. else{
  27. gstrdel(extension);
  28. if (gmstrcpy(&mirror, tmp_file, "/", entry->d_name, 0) == -1)
  29. continue;
  30. if ((descriptor = open(mirror, O_WRONLY | O_CREAT)) == -1)
  31. continue;
  32. if (close(descriptor) == -1)
  33. continue;
  34. };
  35. rev_count++;
  36. };
  37. };
  38. closedir(dir);
  39. return rev_count;
  40. };
  41. int count_revs(char *path){
  42. DIR *dir = NULL;
  43. int rev_count = 0;
  44. struct dirent *entry = NULL;
  45. #ifdef DEBUG_DEEP
  46. printf("[Function: count_revs] Counting revisions in %s directory;\n", path);
  47. #endif
  48. if ((dir = opendir(path)) == NULL)
  49. return -1;
  50. for (entry = readdir(dir); entry != NULL; entry = readdir(dir))
  51. if (gstrsub(entry->d_name, "mirror_metadata.") == 0)
  52. rev_count++;
  53. closedir(dir);
  54. #ifdef DEBUG_DEEP
  55. printf("[Function: count_revs] Found %d revisions;\n", rev_count);
  56. #endif
  57. return rev_count;
  58. };
  59. // FIXME: what about seasonal time?
  60. time_t get_revs_date(char *mirror){
  61. #define FACTORS_COUNT 8
  62. #define FACTORS_SHORT_COUNT 6
  63. int i = 0;
  64. int factors[FACTORS_COUNT];
  65. char *current = mirror + strlen(MIRROR_PREFIX);
  66. char *next = 0;
  67. struct tm rev_time;
  68. if (mirror[GMT_TYPE_POSITION] == 'Z')
  69. for (i = 0; i < FACTORS_SHORT_COUNT; i++){
  70. factors[i] = strtol(current, &next, 10);
  71. current = next + 1;
  72. }
  73. else
  74. for (i = 0; i < FACTORS_COUNT; i++){
  75. factors[i] = strtol(current, &next, 10);
  76. current = next + 1;
  77. };
  78. rev_time.tm_year = factors[0] - 1900;
  79. rev_time.tm_mon = factors[1] - 1;
  80. rev_time.tm_mday = factors[2];
  81. rev_time.tm_hour = factors[3];
  82. rev_time.tm_min = factors[4];
  83. rev_time.tm_sec = factors[5];
  84. /* TODO: consult, whether seasonal time change should be considered */
  85. rev_time.tm_isdst = -1;
  86. time_t gmt_rev_time = mktime(&rev_time);
  87. return gmt_rev_time;
  88. };
  89. char* get_revs_dir(char *mirror){
  90. time_t rev_date = get_revs_date(mirror);
  91. struct tm *rev_tm = gmtime(&rev_date);
  92. char *result = gstralloc(strlen(ARCHFS_DIR_FORMAT_LENGTH));
  93. sprintf(result, ARCHFS_DIR_FORMAT, rev_tm->tm_year + 1900,
  94. rev_tm->tm_mon + 1, rev_tm->tm_mday, rev_tm->tm_hour,
  95. rev_tm->tm_min, rev_tm->tm_sec);
  96. return result;
  97. };
  98. int read_stats(struct stats *stats, FILE *file){
  99. char *line = NULL;
  100. size_t length = 0;
  101. int result = 0;
  102. int name_set = 0;
  103. int link_set = 0;
  104. int type_set = 0;
  105. int size_set = 0;
  106. int time_set = 0;
  107. memset(stats, 0, sizeof(struct stats));
  108. while ((result = gstrline(&line, &length, file)) != -1){
  109. if (gstrsub(line, "File ") == 0){
  110. memset(stats, 0, sizeof(struct stats));
  111. line[result - 1] = 0;
  112. if (strcmp(line, "File .") == 0)
  113. continue;
  114. // gstrcpy(stats->internal, line + strlen("File "));
  115. gstrcpy(&stats->internal, line + 5);
  116. name_set = 1;
  117. link_set = 0;
  118. type_set = 0;
  119. size_set = 0;
  120. time_set = 0;
  121. }
  122. if (gstrsub(line, " Size") == 0){
  123. // stats->size = atoi(line + strlen(" Size ");
  124. stats->size = atoi(line + 7);
  125. size_set = 1;
  126. };
  127. if (strcmp(line, " Type reg\n") == 0){
  128. stats->type = S_IFREG;
  129. type_set = 1;
  130. };
  131. if (strcmp(line, " Type dir\n") == 0){
  132. stats->type = S_IFDIR;
  133. type_set = 1;
  134. };
  135. if (strcmp(line, " Type sym\n") == 0){
  136. stats->type = S_IFLNK;
  137. type_set = 1;
  138. };
  139. if (strcmp(line, " Type None\n") == 0){
  140. stats->type = -1;
  141. if (name_set == 1)
  142. return 0;
  143. };
  144. if (gstrsub(line, " SymData ") == 0){
  145. line[result - 1] = 0;
  146. // gstrcpy(stats->link, line + strlen(" SymData "));
  147. gstrcpy(&stats->link, line + 10);
  148. link_set = 1;
  149. };
  150. if (gstrsub(line, " ModTime ") == 0){
  151. // stats->ctime = atoi(line + strlen(" ModTime "));
  152. stats->ctime = atoi(line + 10);
  153. stats->atime = stats->ctime;
  154. time_set = 1;
  155. }
  156. if ((stats->type == S_IFLNK) &&
  157. (name_set == 1) && (link_set == 1) && (type_set == 1))
  158. return 0;
  159. if ((stats->type == S_IFDIR) &&
  160. (name_set == 1) && (type_set == 1) && (time_set == 1))
  161. return 0;
  162. if ((stats->type != S_IFLNK) &&
  163. (name_set == 1) && (size_set == 1) && (type_set == 1) && (time_set == 1))
  164. return 0;
  165. };
  166. #ifdef DEBUG_DEEP
  167. printf("[Function: read_stats] Finished reading file %s with name %s, type %s, size %s, time %s, link %s;\n",
  168. stats->internal, name_set == 1 ? "set" : "not set", type_set == 1 ? "set" : "not set",
  169. size_set == 1 ? "set" : "not set", time_set == 1 ? "set" : "not set", link_set == 1 ? "set" : "not set");
  170. #endif
  171. return -1;
  172. };
  173. int get_revisions(int count, char **revs){
  174. DIR *dir = NULL;
  175. struct dirent *entry;
  176. int i = 0;
  177. #ifdef DEBUG_DEEP
  178. printf("[Function: get_revisions] Received place for %d revisions;\n", count);
  179. #endif
  180. if ((dir = opendir(tmp_file)) == NULL)
  181. return -1;
  182. for (entry = readdir(dir); (i < count) && (entry != NULL); entry = readdir(dir)){
  183. if (gstrsub(entry->d_name, "mirror_metadata.") == 0){
  184. gstrcpy(&revs[i], entry->d_name);
  185. i++;
  186. };
  187. };
  188. closedir(dir);
  189. if (i != count)
  190. return -1;
  191. gstrsort(revs, count);
  192. #ifdef DEBUG_DEEP
  193. printf("[Function: get_revisions] Retrieved and sorted %d revisions;\n", count);
  194. #endif
  195. return 0;
  196. }
  197. struct node *snapshot_tree = NULL;
  198. int read_snapshot(struct node *tree){
  199. return 0;
  200. };
  201. int snapshot_copy(char *revision){
  202. char *path = NULL;
  203. char *snapshot = NULL;
  204. int snapshot_desc = 0;
  205. int revision_desc = 0;
  206. char buffer[1024];
  207. size_t result = 0;
  208. gmstrcpy(&path, tmp_file, "/", revision, 0);
  209. gmstrcpy(&snapshot, tmp_file, "/", CURRENT_SNAPSHOT, 0);
  210. #ifdef DEBUG_DEEP
  211. printf("[Function: snapshot_copy] Copying from %s to %s\n", path, snapshot);
  212. #endif
  213. unlink(snapshot);
  214. if ((snapshot_desc = open(snapshot, O_WRONLY | O_CREAT, S_IRWXU)) == -1)
  215. return -1;
  216. if ((revision_desc = open(path, O_RDONLY)) == -1){
  217. close(snapshot_desc);
  218. return -1;
  219. };
  220. while ((result = read(revision_desc, buffer, 1024)) != 0)
  221. write(snapshot_desc, buffer, result);
  222. close(snapshot_desc);
  223. close(revision_desc);
  224. return 0;
  225. };
  226. int snapshot_append(char *file){
  227. char *snapshot = NULL;
  228. int snapshot_desc = 0;
  229. char *revision = NULL;
  230. int revision_desc = 0;
  231. char buffer[1024];
  232. size_t result = 0;
  233. gmstrcpy(&revision, tmp_file, "/", file, 0);
  234. gmstrcpy(&snapshot, tmp_file, "/", CURRENT_SNAPSHOT, 0);
  235. #ifdef DEBUG_DEEP
  236. printf("[Function: snapshot_append] Appending from %s to %s\n", revision, snapshot);
  237. #endif
  238. if ((snapshot_desc = open(snapshot, O_WRONLY | O_APPEND)) == -1)
  239. return -1;
  240. if ((revision_desc = open(revision, O_RDONLY)) == -1){
  241. close(snapshot_desc);
  242. return -1;
  243. };
  244. buffer[0] = '\n';
  245. write(snapshot_desc, buffer, 1);
  246. while ((result = read(revision_desc, buffer, 1024)) != 0)
  247. write(snapshot_desc, buffer, result);
  248. close(snapshot_desc);
  249. close(revision_desc);
  250. return 0;
  251. };
  252. // private:
  253. int unzip(char *path){
  254. #define unzip_error { \
  255. gstrdel(temp); \
  256. gstrdel(target); \
  257. gstrdel(buffer); \
  258. if (file != NULL) \
  259. fclose(file); \
  260. if (archive != NULL) \
  261. gzclose(archive); \
  262. return -1; \
  263. }
  264. char *temp = NULL;
  265. char *target = NULL;
  266. char *buffer = NULL;
  267. int buffer_length = 1000;
  268. FILE *file = NULL;
  269. gzFile archive = NULL;
  270. if (gstrcpy(&temp, path) == -1)
  271. unzip_error;
  272. if (gpthcld(&temp, temp) == -1)
  273. unzip_error;
  274. if (gpthugz(&temp) == -1)
  275. unzip_error;
  276. if (gstrcpy(&target, tmp_file) == -1)
  277. unzip_error;
  278. if (gstrcat(&target, "/") == -1)
  279. unzip_error;
  280. if (gstrcat(&target, temp) == -1)
  281. unzip_error;
  282. if ((file = fopen(target, "w")) == NULL)
  283. unzip_error;
  284. if ((archive = gzopen(path, "rb")) == NULL)
  285. unzip_error;
  286. if ((buffer = gstralloc(buffer_length)) == NULL)
  287. unzip_error;
  288. while (gzgets(archive, buffer, buffer_length) != Z_NULL)
  289. fprintf(file, "%s", buffer);
  290. fclose(file);
  291. gzclose(archive);
  292. gstrdel(temp);
  293. gstrdel(target);
  294. return 0;
  295. };