Skip to content

Commit 1baf289

Browse files
committed
fix ssu_rsync syncronize, write_change_list
1 parent 78f13db commit 1baf289

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#ifndef COMMON_H
77
#define COMMON_H
88

9-
//#define DEBUG
9+
#define DEBUG
1010

1111
#include <stdio.h>
1212
#include <stdlib.h>

ssu_rsync.c

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,12 @@ void syncronize(char *src_path, char *dst_path) // 동기화 함수
215215
compare_file(src_list, dst_list->child);
216216

217217
if (is_directory)
218-
change_count = write_change_list(src_list->child, change_count, CREATE, true); // 생성 혹은 수정된 파일 확인
218+
change_count = write_change_list(src_list->child, change_count, CREATE); // 생성 혹은 수정된 파일 확인
219219
else
220-
change_count = write_change_list(src_list, change_count, CREATE, true); // 생성 혹은 수정된 파일 확인
220+
change_count = write_change_list(src_list, change_count, CREATE); // 생성 혹은 수정된 파일 확인
221221

222222
if (option_m)
223-
change_count = write_change_list(dst_list->child, change_count, DELETE, true); // 삭제 혹은 수정된 파일 확인
223+
change_count = write_change_list(dst_list->child, change_count, DELETE); // 삭제 혹은 수정된 파일 확인
224224

225225
if (option_t)
226226
renewal_tar(change_count);
@@ -370,9 +370,13 @@ bool compare_file(file_node *src_file, file_node *dst_file) // 파일 정보 비
370370
while (now != NULL) {
371371

372372
#ifdef DEBUG
373-
printf("compare_file(): src_file->name = %s, dst_file->name = %s\n", src_file->name + strlen(src_path) - strlen(get_file_name(src_path)), now->name + strlen(dst_path) + 1);
373+
if (src_is_dir)
374+
printf("compare_file(): src_file->name = %s, dst_file->name = %s\n", src_file->name + strlen(src_path) + 1, now->name + strlen(dst_path) + 1);
375+
else
376+
printf("compare_file(): src_file->name = %s, dst_file->name = %s\n", get_file_name(src_file->name), now->name + strlen(dst_path) + 1);
374377
#endif
375-
if (!strcmp(src_file->name + strlen(src_path) - strlen(get_file_name(src_path)), now->name + strlen(dst_path) + 1)) { // 파일 이름이 같은 경우
378+
if ((src_is_dir && !strcmp(src_file->name + strlen(src_path) + 1, now->name + strlen(dst_path) + 1))
379+
|| (!src_is_dir && !strcmp(get_file_name(src_file->name), now->name + strlen(dst_path) + 1))) { // 파일 이름이 같은 경우
376380

377381
#ifdef DEBUG
378382
printf("compare_file(): file found\n");
@@ -421,7 +425,7 @@ bool compare_file(file_node *src_file, file_node *dst_file) // 파일 정보 비
421425
* @param status 변경 사항 타입 번호
422426
* @param is_first 첫번째 레벨 확인 변수
423427
*/
424-
int write_change_list(file_node *head, int idx, int status, bool is_first) // 변경사항 목록 작성
428+
int write_change_list(file_node *head, int idx, int status) // 변경사항 목록 작성
425429
{
426430
file_node *now;
427431

@@ -430,17 +434,21 @@ int write_change_list(file_node *head, int idx, int status, bool is_first) //
430434
while (now != NULL) {
431435

432436
switch (now->status) {
437+
433438
case UNCHCK:
434-
if (status == CREATE) { // 생성됨
435-
strcpy(change_list[idx].name, now->name);
436-
change_list[idx].status = CREATE;
437-
} else if (status == DELETE) { // 삭제됨
438-
char tmp[MAX_BUFFER_SIZE];
439-
sprintf(tmp, "%.*s/%s", (int)strlen(dst_path), dst_path, get_file_name(src_path));
440-
if(strstr(now->name, tmp) == NULL || !strcmp(now->name, tmp))
439+
440+
switch (status) {
441+
442+
case CREATE:
443+
strcpy(change_list[idx].name, now->name);
444+
change_list[idx].status = CREATE;
441445
break;
442-
strcpy(change_list[idx].name, now->name);
443-
change_list[idx].status = DELETE;
446+
447+
case DELETE:
448+
strcpy(change_list[idx].name, now->name);
449+
change_list[idx].status = DELETE;
450+
break;
451+
444452
}
445453
change_list[idx++].size = now->size;
446454
#ifdef DEBUG
@@ -458,9 +466,8 @@ int write_change_list(file_node *head, int idx, int status, bool is_first) //
458466
break;
459467
}
460468

461-
if(option_r || is_first) // R옵션이 존재하거나 첫번째 레벨일 경우
462-
if (now->child != NULL)
463-
idx = write_change_list(now->child, idx, status, false);
469+
if (option_r && now->child != NULL) // R옵션이 주어진 경우
470+
idx = write_change_list(now->child, idx, status);
464471

465472
now = now->next;
466473
}
@@ -481,13 +488,6 @@ void renewal(int count) // 파일 동기화
481488
struct utimbuf attr;
482489
size_t length;
483490

484-
// 타겟 디렉토리가 존재하지 않을경우 동기화 디렉토리에 생성
485-
sprintf(path, "%.*s/%s", (int)strlen(dst_path), dst_path, get_file_name(src_path));
486-
if (src_is_dir && access(path, F_OK) < 0) {
487-
lstat(src_path, &statbuf);
488-
mkdir(path, statbuf.st_mode);
489-
}
490-
491491
for (int i = 0; i < count; i++) {
492492

493493
switch (change_list[i].status) {
@@ -506,7 +506,7 @@ void renewal(int count) // 파일 동기화
506506
memset(path, 0, MAX_BUFFER_SIZE);
507507

508508
lstat(change_list[i].name, &statbuf);
509-
sprintf(path, "%.*s/%s", (int)strlen(dst_path), dst_path, change_list[i].name + strlen(src_path) - strlen(get_file_name(src_path))); // 동기화 파일 경로 생성
509+
sprintf(path, "%.*s/%s", (int)strlen(dst_path), dst_path, change_list[i].name + strlen(src_path) + 1); // 동기화 파일 경로 생성
510510
#ifdef DEBUG
511511
printf("renewal: path = %s\n", path);
512512
#endif
@@ -560,12 +560,16 @@ void renewal_tar(int count)
560560
}
561561

562562
// 1. 타겟 디렉토리 tar 생성
563-
strncpy(path, src_path, get_file_name(src_path) - src_path - 1);
564-
sprintf(file_name, "%s.tar", get_file_name(src_path));
563+
sprintf(file_name, "%s.tar", get_file_name(dst_path)); // tar 파일 이름 생성
565564
#ifdef DEBUG
566565
printf("renewal_tar(): cd %s\n", path);
567566
#endif
568-
chdir(path);
567+
if (src_is_dir) // SRC가 디렉토리일 경우 SRC디렉토리 내부로 이동
568+
chdir(src_path);
569+
else { // SRC가 파일일 경우 SRC가 존재하는 경로로 이동
570+
strncpy(path, src_path, get_file_name(src_path) - src_path - 1);
571+
chdir(path);
572+
}
569573

570574
// 2. 압축 실행
571575
for(int i = 0; i < count; i++) {
@@ -585,9 +589,9 @@ void renewal_tar(int count)
585589
// 압축 파일에 파일 추가
586590
if (src_is_dir)
587591
#ifdef DEBUG
588-
sprintf(command, "tar -rvf %s %s", file_name, change_list[i].name + strlen(src_path) - strlen(get_file_name(src_path)));
592+
sprintf(command, "tar -rvf %s %s", file_name, change_list[i].name + strlen(src_path) + 1);
589593
#else
590-
sprintf(command, "tar -rf %s %s", file_name, change_list[i].name + strlen(src_path) - strlen(get_file_name(src_path)));
594+
sprintf(command, "tar -rf %s %s", file_name, change_list[i].name + strlen(src_path) + 1);
591595
#endif
592596
else
593597
#ifdef DEBUG
@@ -603,15 +607,22 @@ void renewal_tar(int count)
603607
}
604608
}
605609

606-
chdir(path);
607610
lstat(file_name, &statbuf); // 상태 정보 획득
608611
size = statbuf.st_size; // 압축 파일 크기 획득
609-
sprintf(path, "%.*s/%.*s", (int)strlen(dst_path), dst_path, (int)strlen(file_name), file_name); // 압축 풀 경로 생성
612+
613+
// DST 내부로 tar 파일 이동
614+
sprintf(path, "%s/%.*s", dst_path, (int)strlen(file_name), file_name); // 이동 시킬 경로 생성
615+
#ifdef DEBUG
616+
printf("renewal_tar(): mv %s %s\n", file_name, path);
617+
#endif
610618
rename(file_name, path); // 압축 파일 이동
619+
620+
// DST 내부로 이동
611621
#ifdef DEBUG
612622
printf("renewal_tar(): cd %s\n", dst_path);
613623
#endif
614624
chdir(dst_path);
625+
615626
#ifdef DEBUG
616627
sprintf(command, "tar -xvf %s", file_name);
617628
printf("renewal_tar(): command = %s\n", command);

ssu_rsync.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ file_node *make_list(char *path); // 디렉토리 파일 목록 트리화
4545
int count_size(file_node *head); // 디렉토리 크기 반환
4646
void compare_list(file_node *src_list, file_node *dst_list); // 파일 목록 트리 비교
4747
bool compare_file(file_node *src_file, file_node *dst_file); // 파일 정보 비교
48-
int write_change_list(file_node *head, int idx, int status, bool is_first); // 변경사항 목록 작성
48+
int write_change_list(file_node *head, int idx, int status); // 변경사항 목록 작성
4949
void write_log(int count, unsigned long long totalsize); // 로그 파일 작성
5050
void free_list(file_node *head); // 파일 목록 메모리 할당 해제
5151
void renewal(int count); // 파일 동기화

0 commit comments

Comments
 (0)