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 0ab42fc

Browse files
committed
implement compare & find diff ssu_rsync
1 parent 3dbe27e commit 0ab42fc

File tree

26 files changed

+1128
-36
lines changed

26 files changed

+1128
-36
lines changed

‎ssu_crond.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void *reservation_execute(void *arg) // 예약 명령 실행 스레드
127127
#endif
128128
sleep(execute_t - now_t); // 명령어 실행 대기
129129
#ifdef DEBUG
130-
printf("reservation_execute(): %s, running at %s\n", (char*)arg, asctime(&now_tm));
130+
printf("reservation_execute(): %s, running at %s", (char*)arg, asctime(&now_tm));
131131
#endif
132132
system(command); // 명령어 실행
133133
write_log(RUN, (char*)arg);

‎ssu_crontab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void prompt(void) // 프롬프트 메인
100100
case REMOVE:
101101

102102
if (command.argc < 2) {
103-
print_usage();
103+
fprintf(stderr, "prompt: COMMAND_NUMBER doesn't exist\n");
104104
break;
105105
}
106106

‎ssu_rsync.c

Lines changed: 177 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
*/
66
#include "ssu_rsync.h"
77

8+
// 옵션
9+
bool option_r = false;
10+
bool option_t = false;
11+
bool option_m = false;
12+
813
/**
914
* @brief ssu_rsync 메인 함수
1015
* @param argc 인자 개수
@@ -16,71 +21,106 @@ int main(int argc, char *argv[])
1621
struct timeval begin_t, end_t;
1722

1823
// 파일 경로
19-
char src_path[MAX_BUFFER_SIZE];
20-
char dst_path[MAX_BUFFER_SIZE];
21-
22-
// 옵션
23-
bool option_r = false;
24-
bool option_t = false;
25-
bool option_m = false;
24+
char src_path[MAX_BUFFER_SIZE] = { 0 };
25+
char dst_path[MAX_BUFFER_SIZE] = { 0 };
2626

2727
// 유효 검사
28+
char opt;
29+
struct stat statbuf;
2830
bool is_invalid = false;
2931
bool is_src = false;
3032
bool is_dst = false;
3133

32-
3334
gettimeofday(&begin_t, NULL); // 측정 시작
3435

3536
if (argc < 3) { // 인자 개수가 부족할 경우
3637
fprintf(stderr, "ssu_rsync(): Usage: %s [OPTION] <SOURCE> <DESTINATION>\n", argv[0]);
3738
exit(1);
3839
}
3940

40-
for (int i = 0; i < argc; i++) {
41-
42-
// 옵션 파싱
43-
if (argv[i][0] == '-') {
44-
if (!strcmp(argv[i], "-r"))
45-
option_r = true;
46-
else if (!strcmp(argv[i], "-t"))
47-
option_t = true;
48-
else if (!strcmp(argv[i], "-m"))
49-
option_m = true;
50-
else {
51-
is_invalid = true;
52-
break;
53-
}
41+
for (int i = 1; i < argc; i++) {
42+
#ifdef DEBUG
43+
printf("ssu_rsync(): argv[%d] = %s\n", i, argv[i]);
44+
#endif
45+
// 옵션 생략
46+
if (argv[i][0] == '-')
5447
continue;
55-
}
5648

57-
// 목적 경로 파싱
49+
// 타겟 경로 파싱
5850
if (!is_src) {
5951
if (access(argv[i], F_OK) < 0) {
52+
#ifdef DEBUG
6053
fprintf(stderr, "ssu_rsync(): access error for %s\n", argv[i]);
54+
#endif
6155
is_invalid = true;
62-
exit(1);
63-
} else
64-
realpath(argv[i], src_path); // 절대 경로로 변환
65-
56+
break;
57+
}
58+
59+
realpath(argv[i], src_path); // 절대 경로로 변환
60+
#ifdef DEBUG
61+
printf("ssu_rsync(): src_path = %s\n", src_path);
62+
#endif
6663
is_src = true;
6764
continue;
6865
}
6966

70-
// 위치 경로 파싱
67+
// 동기화 경로 파싱
7168
if (!is_dst) {
7269
if (access(argv[i], F_OK) < 0) {
70+
#ifdef DEBUG
7371
fprintf(stderr, "ssu_rsync(): access error for %s\n", argv[i]);
72+
#endif
7473
is_invalid = true;
75-
exit(1);
76-
} else
77-
realpath(argv[i], dst_path); // 절대 경로로 변환
74+
break;
75+
}
7876

77+
realpath(argv[i], dst_path); // 절대 경로로 변환
78+
#ifdef DEBUG
79+
printf("ssu_rsync(): dst_path = %s\n", dst_path);
80+
#endif
81+
lstat(dst_path, &statbuf);
82+
if (!S_ISDIR(statbuf.st_mode)) { // 동기화 경로가 디렉토리가 아닐 경우
83+
#ifdef DEBUG
84+
fprintf(stderr, "ssu_rsync(): dst_path doesn't directory\n");
85+
#endif
86+
is_invalid = true;
87+
break;
88+
}
7989
is_dst = true;
8090
continue;
8191
}
8292
}
8393

94+
// 옵션 파싱
95+
while ((opt = getopt(argc, argv, "rtm")) != -1) {
96+
switch (opt) {
97+
case 'r':
98+
#ifdef DEBUG
99+
printf("ssu_rsync(): R option found\n");
100+
#endif
101+
option_r = true;
102+
break;
103+
case 't':
104+
#ifdef DEBUG
105+
printf("ssu_rsync(): T option found\n");
106+
#endif
107+
option_t = true;
108+
break;
109+
case 'm':
110+
#ifdef DEBUG
111+
printf("ssu_rsync(): M option found\n");
112+
#endif
113+
option_m = true;
114+
break;
115+
default:
116+
#ifdef DEBUG
117+
printf("ssu_rsync(): invalid option found\n");
118+
#endif
119+
is_invalid = true;
120+
break;
121+
}
122+
}
123+
84124
// 파싱 중 에러 발견
85125
if (is_invalid)
86126
exit(1);
@@ -91,12 +131,34 @@ int main(int argc, char *argv[])
91131
fprintf(stderr, "ssu_rsync(): <DESTINATION> doesn't exist\n");
92132
exit(1);
93133
}
94-
134+
135+
syncronized(src_path, dst_path);
136+
95137
gettimeofday(&end_t, NULL); // 측정 종료
96138
ssu_runtime(&begin_t, &end_t); // 실행 시간 출력
97139
exit(0);
98140
}
99141

142+
/**
143+
* @brief 동기화 함수
144+
* @param src_path 타겟 경로
145+
* @param dst_path 동기화 경로
146+
*/
147+
void syncronized(char *src_path, char *dst_path) // 동기화 함수
148+
{
149+
file_node *src_list; // 타겟 경로 파일 목록
150+
file_node *dst_list; // 동기화 경로 파일 목록
151+
152+
src_list = make_list(src_path);
153+
dst_list = make_list(dst_path);
154+
155+
compare_list(src_list, dst_list->child); // 파일 목록 트리 비교
156+
157+
free_list(src_list);
158+
free_list(dst_list);
159+
160+
}
161+
100162
/**
101163
* @brief 노드 생성
102164
* @return 새로운 노드
@@ -191,6 +253,88 @@ int count_size(file_node *head) // 디렉토리 크기 반환
191253
return size;
192254
}
193255

256+
/**
257+
* @brief 파일 목록 트리 비교
258+
* @param src_list 타겟 파일 목록
259+
* @param dst_list 동기화 디렉토리 파일 목록
260+
*/
261+
void compare_list(file_node *src_list, file_node *dst_list) // 파일 목록 트리 비교
262+
{
263+
file_node *now;
264+
265+
if (src_list == NULL || dst_list == NULL) // 둘중 하나라도 비교 대상이 존재하지 않을 경우
266+
return;
267+
268+
now = src_list;
269+
270+
while (now != NULL) { // 타겟 파일 탐색
271+
272+
compare_file(now, dst_list);
273+
274+
if (option_r) // R 옵션이 존재하는 경우
275+
if (now->child != NULL)
276+
compare_list(now->child, dst_list);
277+
278+
now = now->next;
279+
}
280+
}
281+
282+
/**
283+
* @brief 파일 정보 비교
284+
* @param src_file 타겟 파일 노드
285+
* @param dst_file 동기화 디렉토리 파일 노드
286+
*/
287+
int compare_file(file_node *src_file, file_node *dst_file) // 파일 정보 비교
288+
{
289+
file_node *now;
290+
291+
now = dst_file;
292+
293+
while (now != NULL) {
294+
295+
#ifdef DEBUG
296+
printf("compare_file(): src_file->name = %s, now->name = %s\n", src_file->name, now->name);
297+
#endif
298+
if (!strcmp(src_file->name, now->name)) { // 해당 이름을 가진 파일이 기존에 이미 존재할 경우
299+
300+
#ifdef DEBUG
301+
printf("compare_file(): founded\n");
302+
#endif
303+
src_file->status = CHCKED;
304+
305+
if (src_file->attr.st_mtime != now->attr.st_mtime) { // 해당 파일이 수정되었을 경우
306+
#ifdef DEBUG
307+
printf("compare_file(): mtime different\n");
308+
#endif
309+
src_file->status = MODIFY; // 타겟 파일의 상태 변경
310+
}
311+
312+
if (src_file->size != now->size) { // 해당 파일의 크기가 변경되었을 경우
313+
314+
#ifdef DEBUG
315+
printf("compare_file(): size different\n");
316+
#endif
317+
src_file->status = MODIFY;
318+
}
319+
320+
now->status = CHCKED;
321+
#ifdef DEBUG
322+
printf("compare_file(): src_file->status = %d, now->status = %d\n", src_file->status, now->status);
323+
#endif
324+
return true;
325+
}
326+
327+
if(option_r)
328+
if(now->child != NULL) // 디렉토리 안에 파일이 존재할 경우
329+
if(compare_file(src_file, now->child))
330+
break;
331+
332+
now = now->next;
333+
}
334+
335+
return false;
336+
}
337+
194338
/**
195339
* @brief 모니터링 파일 목록 메모리 할당 해제
196340
* @param head 트리의 루트 노드

‎ssu_rsync.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@
1010
#include <sys/stat.h>
1111
#include "common.h"
1212

13-
// 모니터링 상태
13+
/**
14+
* @brief diff 상태
15+
*/
1416
#define UNCHCK -1
1517
#define CHCKED 0
1618
#define CREATE 2
1719
#define MODIFY 3
1820

21+
/**
22+
* @brief 파일 이름
23+
*/
24+
#define RSYNC_LOG "ssu_rsync_log"
25+
1926
typedef struct ssu_fileNode{ // 모니터링 파일 목록 구조체
2027
char name[BUFFER_SIZE]; // 파일 이름
2128
struct stat attr; // 파일 상태 정보
@@ -26,8 +33,11 @@ typedef struct ssu_fileNode{ // 모니터링 파일 목록 구조체
2633
int status; // 모니터링 확인 상태
2734
} file_node;
2835

36+
void syncronized(char *src_path, char *dst_path); // 동기화 함수
2937
file_node *make_node(void); // 노드 생성
3038
file_node *make_list(char *path); // 디렉토리 파일 목록 트리화
3139
int count_size(file_node *head); // 디렉토리 크기 반환
40+
void compare_list(file_node *src_list, file_node *dst_list); // 파일 목록 트리 비교
41+
int compare_file(file_node *src_file, file_node *dst_file); // 파일 정보 비교
3242
void free_list(file_node *head); // 모니터링 파일 목록 메모리 할당 해제
3343
#endif // SSU_RSYNC_H

‎test/11.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <sys/stat.h>
5+
6+
void print_file_type(struct stat *statbuf)
7+
{
8+
char *str;
9+
10+
if (S_ISREG(statbuf->st_mode))
11+
str = "regular";
12+
else if (S_ISDIR(statbuf->st_mode))
13+
str = "directory";
14+
else if (S_ISCHR(statbuf->st_mode))
15+
str = "character special";
16+
else if (S_ISBLK(statbuf->st_mode))
17+
str = "block";
18+
else if (S_ISFIFO(statbuf->st_mode))
19+
str = "FIFO";
20+
else if (S_ISLNK(statbuf->st_mode))
21+
str = "symbolic link";
22+
else if (S_ISSOCK(statbuf->st_mode))
23+
str = "socket";
24+
else
25+
str = "unknown mode";
26+
27+
printf("%s\n", str);
28+
}
29+
30+
int main(int argc, char *argv[])
31+
{
32+
struct stat statbuf;
33+
int i;
34+
35+
for(i = 1; i < argc; i++) {
36+
lstat(argv[i], &statbuf);
37+
print_file_type(&statbuf);
38+
}
39+
exit(0);
40+
}

0 commit comments

Comments
(0)

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