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