@@ -215,12 +215,12 @@ void syncronize(char *src_path, char *dst_path) // 동기화 함수
215
215
compare_file (src_list , dst_list -> child );
216
216
217
217
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 ); // 생성 혹은 수정된 파일 확인
219
219
else
220
- change_count = write_change_list (src_list , change_count , CREATE , true ); // 생성 혹은 수정된 파일 확인
220
+ change_count = write_change_list (src_list , change_count , CREATE ); // 생성 혹은 수정된 파일 확인
221
221
222
222
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 ); // 삭제 혹은 수정된 파일 확인
224
224
225
225
if (option_t )
226
226
renewal_tar (change_count );
@@ -370,9 +370,13 @@ bool compare_file(file_node *src_file, file_node *dst_file) // 파일 정보 비
370
370
while (now != NULL ) {
371
371
372
372
#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 );
374
377
#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 ))) { // 파일 이름이 같은 경우
376
380
377
381
#ifdef DEBUG
378
382
printf ("compare_file(): file found\n" );
@@ -421,7 +425,7 @@ bool compare_file(file_node *src_file, file_node *dst_file) // 파일 정보 비
421
425
* @param status 변경 사항 타입 번호
422
426
* @param is_first 첫번째 레벨 확인 변수
423
427
*/
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 ) // 변경사항 목록 작성
425
429
{
426
430
file_node * now ;
427
431
@@ -430,17 +434,21 @@ int write_change_list(file_node *head, int idx, int status, bool is_first) //
430
434
while (now != NULL ) {
431
435
432
436
switch (now -> status ) {
437
+
433
438
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 ;
441
445
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
+
444
452
}
445
453
change_list [idx ++ ].size = now -> size ;
446
454
#ifdef DEBUG
@@ -458,9 +466,8 @@ int write_change_list(file_node *head, int idx, int status, bool is_first) //
458
466
break ;
459
467
}
460
468
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 );
464
471
465
472
now = now -> next ;
466
473
}
@@ -481,13 +488,6 @@ void renewal(int count) // 파일 동기화
481
488
struct utimbuf attr ;
482
489
size_t length ;
483
490
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
-
491
491
for (int i = 0 ; i < count ; i ++ ) {
492
492
493
493
switch (change_list [i ].status ) {
@@ -506,7 +506,7 @@ void renewal(int count) // 파일 동기화
506
506
memset (path , 0 , MAX_BUFFER_SIZE );
507
507
508
508
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 ); // 동기화 파일 경로 생성
510
510
#ifdef DEBUG
511
511
printf ("renewal: path = %s\n" , path );
512
512
#endif
@@ -560,12 +560,16 @@ void renewal_tar(int count)
560
560
}
561
561
562
562
// 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 파일 이름 생성
565
564
#ifdef DEBUG
566
565
printf ("renewal_tar(): cd %s\n" , path );
567
566
#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
+ }
569
573
570
574
// 2. 압축 실행
571
575
for (int i = 0 ; i < count ; i ++ ) {
@@ -585,9 +589,9 @@ void renewal_tar(int count)
585
589
// 압축 파일에 파일 추가
586
590
if (src_is_dir )
587
591
#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 );
589
593
#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 );
591
595
#endif
592
596
else
593
597
#ifdef DEBUG
@@ -603,15 +607,22 @@ void renewal_tar(int count)
603
607
}
604
608
}
605
609
606
- chdir (path );
607
610
lstat (file_name , & statbuf ); // 상태 정보 획득
608
611
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
610
618
rename (file_name , path ); // 압축 파일 이동
619
+
620
+ // DST 내부로 이동
611
621
#ifdef DEBUG
612
622
printf ("renewal_tar(): cd %s\n" , dst_path );
613
623
#endif
614
624
chdir (dst_path );
625
+
615
626
#ifdef DEBUG
616
627
sprintf (command , "tar -xvf %s" , file_name );
617
628
printf ("renewal_tar(): command = %s\n" , command );
0 commit comments