Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5c8a6cd

Browse files
committed
fix recovery & sync bug to ssu_rsync.c
1 parent e0b4af3 commit 5c8a6cd

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

‎ssu_crond.service‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎ssu_rsync.c‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ int err_fd; // 표준 에러
2525
file_node change_list[BUFFER_SIZE]; // 변경 목록
2626
char **saved_argv;
2727
int saved_argc;
28+
bool src_is_dir = false;
2829

2930

3031
/**
@@ -56,8 +57,8 @@ int main(int argc, char *argv[])
5657
}
5758

5859
getcwd(pwd, MAX_BUFFER_SIZE);
59-
signal(SIGUSR1, swap_handler);
60-
signal(SIGUSR2, swap_handler);
60+
signal(SIGUSR1, io_handler);
61+
signal(SIGUSR2, io_handler);
6162

6263
copy_argument(argc, argv);
6364

@@ -82,6 +83,12 @@ int main(int argc, char *argv[])
8283
realpath(argv[i], src_path); // 절대 경로로 변환
8384
#ifdef DEBUG
8485
printf("ssu_rsync(): src_path = %s\n", src_path);
86+
#endif
87+
lstat(src_path, &statbuf);
88+
if (S_ISDIR(statbuf.st_mode))
89+
src_is_dir = true;
90+
#ifdef DEBUG
91+
fprintf(stderr, "ssu_rsync(): dst_path doesn't directory\n");
8592
#endif
8693
is_src = true;
8794
continue;
@@ -150,20 +157,21 @@ int main(int argc, char *argv[])
150157
exit(1);
151158
}
152159

153-
sprintf(swap_path, "%s.swp", get_file_name(dst_path)); // swap 파일 경로 생성
154-
sprintf(command, "tar -cvf %s %s", swap_path, get_file_name(dst_path)); // 명령어 생성
160+
strncpy(swap_path, dst_path, strlen(dst_path) - strlen(get_file_name(dst_path)));
161+
#ifdef DEBUG
162+
printf("ssu_rsync(): cd %s\n", swap_path);
163+
#endif
164+
chdir(swap_path);
165+
sprintf(command, "tar -cvf %s.swp %s", get_file_name(dst_path), get_file_name(dst_path)); // 명령어 생성
155166
#ifdef DEBUG
156-
printf("ssu_rsync(): swap_path = %s\n", swap_path);
157167
printf("ssu_rsync(): command = %s\n", command);
158168
#endif
159169
kill(getpid(), SIGUSR1); // 표준 입출력 닫음
160170
system(command); // 명령어 실행(압축)
161171
kill(getpid(), SIGUSR2); // 표준 입출력 열기
162-
172+
chdir(pwd); // 실행 경로로 복귀
163173
signal(SIGINT, recovery); // SIGINT 시그널 처리
164-
165174
syncronize(src_path, dst_path); // 동기화
166-
167175
remove(swap_path); // swap 파일 삭제
168176

169177
gettimeofday(&end_t, NULL); // 측정 종료
@@ -190,7 +198,7 @@ void copy_argument(int argc, char *argv[]) // 명령행 인자 백업
190198
* @brief 표준 입출력 전환
191199
* @param signo 시그널
192200
*/
193-
void swap_handler(int signo) // 표준 입출력 전환
201+
void io_handler(int signo) // 표준 입출력 전환
194202
{
195203
switch (signo) {
196204
case SIGUSR1:
@@ -497,6 +505,12 @@ void renewal(int count) // 파일 동기화
497505
struct utimbuf attr;
498506
size_t length;
499507

508+
sprintf(path, "%.*s/%s", (int)strlen(dst_path), dst_path, get_file_name(src_path));
509+
if (src_is_dir && access(path, F_OK) < 0) {
510+
lstat(src_path, &statbuf);
511+
mkdir(path, statbuf.st_mode);
512+
}
513+
500514
for (int i = 0; i < count; i++) {
501515

502516
switch (change_list[i].status) {
@@ -545,7 +559,6 @@ void renewal(int count) // 파일 동기화
545559
break;
546560
}
547561
}
548-
549562
write_log(count);
550563
}
551564

@@ -596,7 +609,10 @@ void write_log(int count) // 로그 파일 작성
596609
break;
597610
case CREATE:
598611
case MODIFY:
599-
fprintf(fp, " %s %dbytes\n", change_list[i].name + strlen(src_path) + 1, change_list[i].size);
612+
if (src_is_dir)
613+
fprintf(fp, " %s %dbytes\n", change_list[i].name + strlen(src_path) + 1, change_list[i].size);
614+
else
615+
fprintf(fp, " %s %dbytes\n", change_list[i].name + strlen(src_path) - strlen(get_file_name(src_path)), change_list[i].size);
600616
break;
601617
}
602618
}
@@ -625,13 +641,20 @@ void free_list(file_node *head) // 모니터링 파일 목록 메모리 할당
625641
void recovery(int signo) // SIGINT 시그널 처리
626642
{
627643
char command[MAX_BUFFER_SIZE];
644+
char path[MAX_BUFFER_SIZE];
628645

629646
if(signo == SIGINT) { // SIGINT 시그널 획득 시
630647
#ifdef DEBUG
631648
printf("recovery(): SIGINT signal is arrived\n");
632649
#endif
633650
if(is_complete) // 동기화가 완료되었을 경우
634651
return;
652+
653+
strncpy(path, dst_path, strlen(dst_path) - strlen(get_file_name(dst_path)));
654+
#ifdef DEBUG
655+
printf("recovery(): cd %s\n", path);
656+
#endif
657+
chdir(path);
635658

636659
sprintf(command, "tar -xvf %s.swp", get_file_name(dst_path)); // 복원 명령어 생성(압축 해제)
637660
#ifdef DEBUG
@@ -642,6 +665,7 @@ void recovery(int signo) // SIGINT 시그널 처리
642665
system(command); // 복원 명령어 실행
643666
kill(getpid(), SIGUSR2);
644667
remove(command + 9); // swap 파일 삭제
668+
645669
}
646670
exit(1);
647671
}

‎ssu_rsync.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@
2828
*/
2929
#define RSYNC_LOG "ssu_rsync_log"
3030

31-
typedef struct ssu_fileNode{ // 모니터링 파일 목록 구조체
31+
typedef struct ssu_fileNode{ // 파일 목록 구조체
3232
char name[BUFFER_SIZE]; // 파일 이름
3333
struct stat attr; // 파일 상태 정보
3434
struct dirent **namelist; // 디렉토리 경우 하위 파일 목록
3535
struct ssu_fileNode *next; // 하위 디렉토리 파일 포인터
3636
struct ssu_fileNode *child; // 같은 레벨의 다음 파일 포인터
3737
int size; // 파일 크기
38-
int status; // 모니터링 확인 상태
38+
int status; // 확인 상태
3939
} file_node;
4040

4141
void copy_argument(int argc, char *argv[]); // 명령행 인자 백업
42-
void swap_handler(int signo); // 표준입출력 전환
42+
void io_handler(int signo); // 표준입출력 전환
4343
void syncronize(char *src_path, char *dst_path); // 동기화 함수
4444
file_node *make_node(void); // 노드 생성
4545
file_node *make_list(char *path); // 디렉토리 파일 목록 트리화
@@ -48,7 +48,7 @@ void compare_list(file_node *src_list, file_node *dst_list); // 파일 목록
4848
bool compare_file(file_node *src_file, file_node *dst_file); // 파일 정보 비교
4949
int write_change_list(file_node *head, int idx, int status, bool is_first); // 변경사항 목록 작성
5050
void write_log(int count); // 로그 파일 작성
51-
void free_list(file_node *head); // 모니터링 파일 목록 메모리 할당 해제
51+
void free_list(file_node *head); // 파일 목록 메모리 할당 해제
5252
void renewal(int count); // 파일 동기화
5353
void recovery(int signo); // SIGINT 시그널 처리
5454
void remove_directory(const char *path); // 디렉토리 삭제

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /