@@ -25,6 +25,7 @@ int err_fd; // 표준 에러
25
25
file_node change_list [BUFFER_SIZE ]; // 변경 목록
26
26
char * * saved_argv ;
27
27
int saved_argc ;
28
+ bool src_is_dir = false;
28
29
29
30
30
31
/**
@@ -56,8 +57,8 @@ int main(int argc, char *argv[])
56
57
}
57
58
58
59
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 );
61
62
62
63
copy_argument (argc , argv );
63
64
@@ -82,6 +83,12 @@ int main(int argc, char *argv[])
82
83
realpath (argv [i ], src_path ); // 절대 경로로 변환
83
84
#ifdef DEBUG
84
85
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" );
85
92
#endif
86
93
is_src = true;
87
94
continue ;
@@ -150,20 +157,21 @@ int main(int argc, char *argv[])
150
157
exit (1 );
151
158
}
152
159
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 )); // 명령어 생성
155
166
#ifdef DEBUG
156
- printf ("ssu_rsync(): swap_path = %s\n" , swap_path );
157
167
printf ("ssu_rsync(): command = %s\n" , command );
158
168
#endif
159
169
kill (getpid (), SIGUSR1 ); // 표준 입출력 닫음
160
170
system (command ); // 명령어 실행(압축)
161
171
kill (getpid (), SIGUSR2 ); // 표준 입출력 열기
162
-
172
+ chdir ( pwd ); // 실행 경로로 복귀
163
173
signal (SIGINT , recovery ); // SIGINT 시그널 처리
164
-
165
174
syncronize (src_path , dst_path ); // 동기화
166
-
167
175
remove (swap_path ); // swap 파일 삭제
168
176
169
177
gettimeofday (& end_t , NULL ); // 측정 종료
@@ -190,7 +198,7 @@ void copy_argument(int argc, char *argv[]) // 명령행 인자 백업
190
198
* @brief 표준 입출력 전환
191
199
* @param signo 시그널
192
200
*/
193
- void swap_handler (int signo ) // 표준 입출력 전환
201
+ void io_handler (int signo ) // 표준 입출력 전환
194
202
{
195
203
switch (signo ) {
196
204
case SIGUSR1 :
@@ -497,6 +505,12 @@ void renewal(int count) // 파일 동기화
497
505
struct utimbuf attr ;
498
506
size_t length ;
499
507
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
+
500
514
for (int i = 0 ; i < count ; i ++ ) {
501
515
502
516
switch (change_list [i ].status ) {
@@ -545,7 +559,6 @@ void renewal(int count) // 파일 동기화
545
559
break ;
546
560
}
547
561
}
548
-
549
562
write_log (count );
550
563
}
551
564
@@ -596,7 +609,10 @@ void write_log(int count) // 로그 파일 작성
596
609
break ;
597
610
case CREATE :
598
611
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 );
600
616
break ;
601
617
}
602
618
}
@@ -625,13 +641,20 @@ void free_list(file_node *head) // 모니터링 파일 목록 메모리 할당
625
641
void recovery (int signo ) // SIGINT 시그널 처리
626
642
{
627
643
char command [MAX_BUFFER_SIZE ];
644
+ char path [MAX_BUFFER_SIZE ];
628
645
629
646
if (signo == SIGINT ) { // SIGINT 시그널 획득 시
630
647
#ifdef DEBUG
631
648
printf ("recovery(): SIGINT signal is arrived\n" );
632
649
#endif
633
650
if (is_complete ) // 동기화가 완료되었을 경우
634
651
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 );
635
658
636
659
sprintf (command , "tar -xvf %s.swp" , get_file_name (dst_path )); // 복원 명령어 생성(압축 해제)
637
660
#ifdef DEBUG
@@ -642,6 +665,7 @@ void recovery(int signo) // SIGINT 시그널 처리
642
665
system (command ); // 복원 명령어 실행
643
666
kill (getpid (), SIGUSR2 );
644
667
remove (command + 9 ); // swap 파일 삭제
668
+
645
669
}
646
670
exit (1 );
647
671
}
0 commit comments