|
5 | 5 | */
|
6 | 6 | #include "ssu_rsync.h"
|
7 | 7 |
|
8 |
| -int main(void) |
| 8 | +/** |
| 9 | + * @brief ssu_rsync 메인 함수 |
| 10 | + * @param argc 인자 개수 |
| 11 | + * @param argv 인자 문자열 |
| 12 | + */ |
| 13 | +int main(int argc, char *argv[]) |
9 | 14 | {
|
| 15 | + // 실행 시간 측정 |
10 | 16 | struct timeval begin_t, end_t;
|
11 | 17 |
|
12 |
| - gettimeofday(&begin_t, NULL); |
13 |
| - printf("Rsync!\n"); |
14 |
| - gettimeofday(&end_t, NULL); |
15 |
| - ssu_runtime(&begin_t, &end_t); |
16 |
| - return 0; |
| 18 | + // 파일 경로 |
| 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; |
| 26 | + |
| 27 | + // 유효 검사 |
| 28 | + bool is_invalid = false; |
| 29 | + bool is_src = false; |
| 30 | + bool is_dst = false; |
| 31 | + |
| 32 | + |
| 33 | + gettimeofday(&begin_t, NULL); // 측정 시작 |
| 34 | + |
| 35 | + if (argc < 3) { // 인자 개수가 부족할 경우 |
| 36 | + fprintf(stderr, "ssu_rsync(): Usage: %s [OPTION] <SOURCE> <DESTINATION>\n", argv[0]); |
| 37 | + exit(1); |
| 38 | + } |
| 39 | + |
| 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 | + } |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + // 목적 경로 파싱 |
| 58 | + if (!is_src) { |
| 59 | + if (access(argv[i], F_OK) < 0) { |
| 60 | + fprintf(stderr, "ssu_rsync(): access error for %s\n", argv[i]); |
| 61 | + is_invalid = true; |
| 62 | + exit(1); |
| 63 | + } else |
| 64 | + realpath(argv[i], src_path); // 절대 경로로 변환 |
| 65 | + |
| 66 | + is_src = true; |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + // 위치 경로 파싱 |
| 71 | + if (!is_dst) { |
| 72 | + if (access(argv[i], F_OK) < 0) { |
| 73 | + fprintf(stderr, "ssu_rsync(): access error for %s\n", argv[i]); |
| 74 | + is_invalid = true; |
| 75 | + exit(1); |
| 76 | + } else |
| 77 | + realpath(argv[i], dst_path); // 절대 경로로 변환 |
| 78 | + |
| 79 | + is_dst = true; |
| 80 | + continue; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // 파싱 중 에러 발견 |
| 85 | + if (is_invalid) |
| 86 | + exit(1); |
| 87 | + else if (!is_src) { |
| 88 | + fprintf(stderr, "ssu_rsync(): <SOURCE> doesn't exist\n"); |
| 89 | + exit(1); |
| 90 | + } else if (!is_dst) { |
| 91 | + fprintf(stderr, "ssu_rsync(): <DESTINATION> doesn't exist\n"); |
| 92 | + exit(1); |
| 93 | + } |
| 94 | + |
| 95 | + gettimeofday(&end_t, NULL); // 측정 종료 |
| 96 | + ssu_runtime(&begin_t, &end_t); // 실행 시간 출력 |
| 97 | + exit(0); |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * @brief 노드 생성 |
| 102 | + * @return 새로운 노드 |
| 103 | + */ |
| 104 | +file_node *make_node(void) // 노드 생성 |
| 105 | +{ |
| 106 | + file_node *tmp = calloc(true, sizeof(file_node)); |
| 107 | + |
| 108 | + memset(tmp->name, 0, BUFFER_SIZE); |
| 109 | + tmp->next = NULL; |
| 110 | + tmp->child = NULL; |
| 111 | + tmp->namelist = NULL; |
| 112 | + tmp->size = 0; |
| 113 | + tmp->status = UNCHCK; |
| 114 | + |
| 115 | + return tmp; |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * @brief 디렉토리 파일 목록 트리화 |
| 120 | + * @param path 디렉토리 경로 |
| 121 | + * @return 트리의 루트노드 |
| 122 | + */ |
| 123 | +file_node *make_list(char *path) // 디렉토리 파일 목록 트리화 |
| 124 | +{ |
| 125 | + file_node *head, *now; |
| 126 | + // 파일 : 노드 생성(절대경로/이름, 상태정보) |
| 127 | + // 디렉토리: 트리 생성 |
| 128 | + int file_count; |
| 129 | + int is_dirattr = true; |
| 130 | + int i; |
| 131 | + |
| 132 | + // 부모: 현재 경로, 디렉토리 상태정보, 파일 목록 정보 |
| 133 | + // 자식: 절대경로 파일이름, 파일 상태정보, 다음 파일 포인터 및 디렉토리 하위 부모노드 포인터 |
| 134 | + head = make_node(); |
| 135 | + now = head; |
| 136 | + |
| 137 | + strcpy(head->name, path); // 현재 경로 저장 |
| 138 | + stat(head->name, &(head->attr)); // 상태 정보 저장 |
| 139 | + |
| 140 | + file_count = scandir(head->name, &(head->namelist), NULL, alphasort); // 현재 경로의 모든 파일 탐색 및 개수 저장 |
| 141 | + for(i = 0; i < file_count; i++) { |
| 142 | + |
| 143 | + if(!strcmp(head->namelist[i]->d_name, ".") || !strcmp(head->namelist[i]->d_name, "..")) // 현재, 상위 디렉토리 접근 지정자 생략 |
| 144 | + continue; |
| 145 | + |
| 146 | + file_node *new = make_node(); // 새로운 노드 생성 |
| 147 | + |
| 148 | + sprintf(new->name, "%s/%s", path, head->namelist[i]->d_name); // 파일 이름 저장 |
| 149 | + stat(new->name, &(new->attr)); |
| 150 | + |
| 151 | + if(S_ISDIR(new->attr.st_mode)) // 현재 경로의 파일 목록 중 탐색한 파일이 디렉토리일 경우 |
| 152 | + new = make_list(new->name); // 해당 디렉토리 파일 목록 트리화 |
| 153 | + else |
| 154 | + new->size = new->attr.st_size; |
| 155 | + |
| 156 | + if(is_dirattr) { // 현재 노드가 현재 경로의 부모노드일 경우 |
| 157 | + now->child = new; |
| 158 | + now = now->child; |
| 159 | + is_dirattr = false; |
| 160 | + } else { // 아닐 경우 형제로 연결 |
| 161 | + now->next = new; |
| 162 | + now = now->next; |
| 163 | + } |
| 164 | + } |
| 165 | + head->size = count_size(head); |
| 166 | + return head; |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * @brief 디렉토리 크기 반환 |
| 171 | + * @param 디렉토리 노드 |
| 172 | + * @return 디렉토리 크기 |
| 173 | + */ |
| 174 | +int count_size(file_node *head) // 디렉토리 크기 반환 |
| 175 | +{ |
| 176 | + file_node *now; |
| 177 | + int size; |
| 178 | + |
| 179 | + size = false; |
| 180 | + |
| 181 | + if(S_ISDIR(head->attr.st_mode)) |
| 182 | + now = head->child; |
| 183 | + else |
| 184 | + return head->attr.st_size; |
| 185 | + |
| 186 | + while(now != NULL) { |
| 187 | + size += now->size; |
| 188 | + now = now->next; |
| 189 | + } |
| 190 | + |
| 191 | + return size; |
| 192 | +} |
| 193 | + |
| 194 | +/** |
| 195 | + * @brief 모니터링 파일 목록 메모리 할당 해제 |
| 196 | + * @param head 트리의 루트 노드 |
| 197 | + */ |
| 198 | +void free_list(file_node *head) // 모니터링 파일 목록 메모리 할당 해제 |
| 199 | +{ |
| 200 | + // 모든 노드들을 찾아서 메모리 할당을 해제한다. |
| 201 | + if(head->child != NULL) // 자식 탐색 |
| 202 | + free_list(head->child); |
| 203 | + |
| 204 | + if(head->next != NULL) // 형제 탐색 |
| 205 | + free_list(head->next); |
| 206 | + |
| 207 | + free(head->namelist); |
| 208 | + free(head); // 메모리 엑세스 허용 |
17 | 209 | }
|
0 commit comments