[4] | 1 | /**
|
---|
[28] | 2 | * @file cf_file.c
|
---|
| 3 | * @author myusgun <myusgun@gmail.com>
|
---|
[4] | 4 | */
|
---|
| 5 | #include "cf_file.h"
|
---|
[66] | 6 | #include "cf_local.h"
|
---|
[40] | 7 | #include "cf_error.h"
|
---|
[4] | 8 |
|
---|
[41] | 9 | #include <stdio.h>
|
---|
| 10 | #include <errno.h>
|
---|
| 11 | #include <sys/stat.h>
|
---|
| 12 |
|
---|
[50] | 13 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[4] | 14 | # include <io.h>
|
---|
[55] | 15 | # include <direct.h>
|
---|
[14] | 16 |
|
---|
[41] | 17 | # define DELIMITER '\\'
|
---|
| 18 | # define mkdir(a,b) _mkdir (a)
|
---|
| 19 | # define access(a,b) _access (a,b)
|
---|
| 20 |
|
---|
[43] | 21 | # define F_OK 0
|
---|
| 22 | # define W_OK 2
|
---|
| 23 | # define R_OK 4
|
---|
| 24 |
|
---|
[41] | 25 | /*------------------------------*/
|
---|
[14] | 26 | # define S_IWUSR _S_IWRITE
|
---|
| 27 | # define S_IRUSR _S_IREAD
|
---|
| 28 | # define S_IXUSR _S_IEXEC
|
---|
[41] | 29 | /*------------------------------*/
|
---|
[14] | 30 | # define S_IRGRP 0x00000000
|
---|
| 31 | # define S_IWGRP 0x00000000
|
---|
| 32 | # define S_IXGRP 0x00000000
|
---|
[41] | 33 | /*------------------------------*/
|
---|
[14] | 34 | # define S_IROTH 0x00000000
|
---|
| 35 | # define S_IWOTH 0x00000000
|
---|
| 36 | # define S_IXOTH 0x00000000
|
---|
[41] | 37 | /*------------------------------*/
|
---|
| 38 | # define S_IRWXU 0x00000000
|
---|
[50] | 39 | #else // #if defined(_WIN32) || defined(_WIN64)
|
---|
[4] | 40 | # include <unistd.h>
|
---|
[41] | 41 |
|
---|
| 42 | # define DELIMITER '/'
|
---|
[26] | 43 | # define O_BINARY 0x00000000
|
---|
[50] | 44 | #endif // #if defined(_WIN32) || defined(_WIN64)
|
---|
[4] | 45 |
|
---|
[41] | 46 | #define DIRECTORY_MODE S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH
|
---|
| 47 | #define FILE_MODE S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH
|
---|
[15] | 48 |
|
---|
[55] | 49 | #define ASSERT_ARGS(x) \
|
---|
| 50 | if ((x)) \
|
---|
| 51 | return CF_ERROR_FILE_INVALID_ARGS
|
---|
| 52 |
|
---|
[35] | 53 | /**
|
---|
| 54 | * 파일 열기
|
---|
| 55 | *
|
---|
| 56 | * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
|
---|
| 57 | *
|
---|
| 58 | * @param path 파일 경로
|
---|
| 59 | * @param flag 파일 열기 플래그
|
---|
| 60 | *
|
---|
| 61 | * @see CF_FILE_FLAG
|
---|
| 62 | */
|
---|
[4] | 63 | int
|
---|
[16] | 64 | CF_File_Open (const char * path,
|
---|
| 65 | const CF_FILE_FLAG flag)
|
---|
[4] | 66 | {
|
---|
[55] | 67 | int result = 0;
|
---|
[4] | 68 |
|
---|
[55] | 69 | ASSERT_ARGS (path == NULL);
|
---|
| 70 |
|
---|
| 71 | result = open (path, (int)(flag|O_BINARY));
|
---|
| 72 |
|
---|
[4] | 73 | if (result < 0)
|
---|
| 74 | return CF_ERROR_FILE_OPEN;
|
---|
| 75 |
|
---|
| 76 | return result;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[35] | 79 | /**
|
---|
| 80 | * 파일 생성
|
---|
| 81 | *
|
---|
| 82 | * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
|
---|
| 83 | *
|
---|
[41] | 84 | * @param path 파일 경로
|
---|
[35] | 85 | */
|
---|
[4] | 86 | int
|
---|
| 87 | CF_File_Create (const char * path)
|
---|
| 88 | {
|
---|
[55] | 89 | int result = 0;
|
---|
| 90 |
|
---|
| 91 | ASSERT_ARGS (path == NULL);
|
---|
[4] | 92 |
|
---|
[55] | 93 | result = open (path, CF_FILE_CR|CF_FILE_WO|CF_FILE_TR, FILE_MODE);
|
---|
| 94 |
|
---|
[4] | 95 | if (result < 0)
|
---|
| 96 | return CF_ERROR_FILE_CREATE;
|
---|
| 97 |
|
---|
| 98 | return result;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[35] | 101 | /**
|
---|
| 102 | * 파일 닫기
|
---|
| 103 | *
|
---|
| 104 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 105 | *
|
---|
| 106 | * @param fd 파일 디스크립터
|
---|
| 107 | */
|
---|
[4] | 108 | int
|
---|
| 109 | CF_File_Close (const int fd)
|
---|
| 110 | {
|
---|
| 111 | int result = 0;
|
---|
| 112 |
|
---|
[55] | 113 | ASSERT_ARGS (fd < 0);
|
---|
[4] | 114 |
|
---|
| 115 | result = close (fd);
|
---|
| 116 |
|
---|
| 117 | if (result < 0)
|
---|
| 118 | return CF_ERROR_FILE_CLOSE;
|
---|
| 119 |
|
---|
| 120 | return CF_OK;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[35] | 123 | /**
|
---|
| 124 | * 파일 읽기
|
---|
| 125 | *
|
---|
| 126 | * @return 성공 시, 읽은 바이트 수; 실패 시, 오류 코드
|
---|
| 127 | *
|
---|
| 128 | * @param fd 파일 디스크립터
|
---|
| 129 | * @param buf 데이터를 저장할 메모리
|
---|
| 130 | * @param len 데이터를 저장할 메모리의 크기
|
---|
| 131 | */
|
---|
[4] | 132 | int
|
---|
| 133 | CF_File_Read (const int fd,
|
---|
| 134 | void * buf,
|
---|
| 135 | const size_t len)
|
---|
| 136 | {
|
---|
[55] | 137 | int result = 0;
|
---|
| 138 |
|
---|
| 139 | ASSERT_ARGS (fd < 0);
|
---|
| 140 | ASSERT_ARGS (buf == NULL);
|
---|
[4] | 141 |
|
---|
[55] | 142 | result = (int) read (fd, buf, len);
|
---|
| 143 |
|
---|
[4] | 144 | if (result < 0)
|
---|
| 145 | return CF_ERROR_FILE_READ;
|
---|
| 146 |
|
---|
[26] | 147 | return result;
|
---|
[4] | 148 | }
|
---|
| 149 |
|
---|
[35] | 150 | /**
|
---|
| 151 | * 파일 쓰기
|
---|
| 152 | *
|
---|
| 153 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 154 | *
|
---|
| 155 | * @param fd 파일 디스크립터
|
---|
| 156 | * @param buf 데이터가 저장된 메모리
|
---|
| 157 | * @param len 쓸 데이터의 길이
|
---|
| 158 | */
|
---|
[4] | 159 | int
|
---|
| 160 | CF_File_Write (const int fd,
|
---|
| 161 | const void * buf,
|
---|
| 162 | const size_t len)
|
---|
| 163 | {
|
---|
[55] | 164 | int result = 0;
|
---|
| 165 |
|
---|
| 166 | ASSERT_ARGS (fd < 0);
|
---|
| 167 | ASSERT_ARGS (buf == NULL);
|
---|
[4] | 168 |
|
---|
[58] | 169 | result = (int) write (fd, buf, len);
|
---|
[55] | 170 |
|
---|
[4] | 171 | if (result != len)
|
---|
| 172 | return CF_ERROR_FILE_WRITE;
|
---|
| 173 |
|
---|
| 174 | return CF_OK;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[35] | 177 | /**
|
---|
| 178 | * 파일 크기 얻기
|
---|
| 179 | *
|
---|
| 180 | * @return 성공 시, 파일 크기; 실패 시, 오류 코드
|
---|
| 181 | *
|
---|
[41] | 182 | * @param fd 파일 디스크립터
|
---|
[35] | 183 | */
|
---|
[4] | 184 | int
|
---|
| 185 | CF_File_GetSize (const int fd)
|
---|
| 186 | {
|
---|
[55] | 187 | int result = 0;
|
---|
[4] | 188 |
|
---|
[55] | 189 | ASSERT_ARGS (fd < 0);
|
---|
| 190 |
|
---|
[58] | 191 | result = (int) lseek (fd, 0, SEEK_END);
|
---|
[55] | 192 |
|
---|
[4] | 193 | if (result < 0 || lseek (fd, 0, SEEK_SET) < 0)
|
---|
| 194 | return CF_ERROR_FILE_GET_SIZE;
|
---|
| 195 |
|
---|
| 196 | return result;
|
---|
| 197 | }
|
---|
[41] | 198 |
|
---|
| 199 | /**
|
---|
| 200 | * 디렉터리 생성
|
---|
| 201 | *
|
---|
| 202 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 203 | *
|
---|
| 204 | * @param path 생성할 디렉터리 경로
|
---|
| 205 | */
|
---|
| 206 | int
|
---|
| 207 | CF_File_MakeDirectory (const char * path)
|
---|
| 208 | {
|
---|
| 209 | int result = 0;
|
---|
| 210 | char fullPath[1024] = {0x00,};
|
---|
| 211 | char stepPath[256] = {0x00,};
|
---|
| 212 |
|
---|
| 213 | char * f = fullPath;
|
---|
| 214 | char * d = stepPath;
|
---|
| 215 |
|
---|
[55] | 216 | ASSERT_ARGS (path == NULL);
|
---|
| 217 |
|
---|
[46] | 218 | snprintf (fullPath, sizeof (fullPath) - 1, "%s%c", path, DELIMITER);
|
---|
[41] | 219 |
|
---|
| 220 | for (*d++ = *f++ ; *f ; *d++ = *f++)
|
---|
| 221 | {
|
---|
| 222 | if (*f != DELIMITER)
|
---|
| 223 | continue;
|
---|
| 224 |
|
---|
| 225 | if (access (stepPath, F_OK) == 0)
|
---|
| 226 | continue;
|
---|
| 227 |
|
---|
| 228 | result = mkdir (stepPath, DIRECTORY_MODE);
|
---|
| 229 | if (result && errno != EEXIST)
|
---|
| 230 | return CF_ERROR_FILE_MAKE_DIRECTORY;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | return CF_OK;
|
---|
| 234 | }
|
---|