sqliterk_os.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * WCDB available.
  4. *
  5. * Copyright (C) 2017 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "sqliterk_os.h"
  21. #include "sqliterk_util.h"
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. struct sqliterk_file {
  29. char *path;
  30. int fd;
  31. int error; // errno will be set when system error occur
  32. };
  33. int sqliterkOSReadOnlyOpen(const char *path, sqliterk_file **file)
  34. {
  35. if (!path || !path[0] || !file) {
  36. return SQLITERK_MISUSE;
  37. }
  38. int rc = SQLITERK_OK;
  39. sqliterk_file *theFile = sqliterkOSMalloc(sizeof(sqliterk_file));
  40. if (!theFile) {
  41. rc = SQLITERK_NOMEM;
  42. sqliterkOSError(rc, "Not enough memory, required %zu bytes.",
  43. sizeof(sqliterk_file));
  44. goto sqliterkOSReadOnlyOpen_Failed;
  45. }
  46. size_t len = sizeof(char) * (strlen(path) + 1);
  47. theFile->path = sqliterkOSMalloc(len);
  48. if (!theFile->path) {
  49. rc = SQLITERK_NOMEM;
  50. sqliterkOSError(rc, "Not enough memory, required %zu bytes.", len);
  51. goto sqliterkOSReadOnlyOpen_Failed;
  52. }
  53. strncpy(theFile->path, path, len);
  54. // Open the file in read-only mode, since we do not intend to modify it
  55. theFile->fd = open(theFile->path, O_RDONLY);
  56. if (theFile->fd < 0) {
  57. rc = SQLITERK_CANTOPEN;
  58. sqliterkOSError(rc, "Cannot open '%s' for reading: %s", theFile->path,
  59. strerror(errno));
  60. goto sqliterkOSReadOnlyOpen_Failed;
  61. }
  62. *file = theFile;
  63. return SQLITERK_OK;
  64. sqliterkOSReadOnlyOpen_Failed:
  65. if (theFile) {
  66. sqliterkOSClose(theFile);
  67. }
  68. *file = NULL;
  69. return rc;
  70. }
  71. int sqliterkOSClose(sqliterk_file *file)
  72. {
  73. if (!file) {
  74. return SQLITERK_MISUSE;
  75. }
  76. if (file->path) {
  77. sqliterkOSFree((char *) file->path);
  78. file->path = NULL;
  79. }
  80. if (file->fd >= 0) {
  81. close(file->fd);
  82. file->fd = -1;
  83. }
  84. file->error = 0;
  85. sqliterkOSFree(file);
  86. return SQLITERK_OK;
  87. }
  88. int sqliterkOSRead(sqliterk_file *file,
  89. off_t offset,
  90. unsigned char *data,
  91. size_t *size)
  92. {
  93. if (!file || file->fd < 0) {
  94. return SQLITERK_MISUSE;
  95. }
  96. off_t newOffset = lseek(file->fd, offset, SEEK_SET);
  97. if (newOffset == -1) {
  98. file->error = errno;
  99. return SQLITERK_IOERR;
  100. }
  101. size_t left = *size;
  102. size_t cnt = 0;
  103. ssize_t got = 0;
  104. do {
  105. got = read(file->fd, data, left);
  106. if (got < 0) {
  107. if (errno == EINTR) {
  108. got = 1;
  109. continue;
  110. }
  111. file->error = errno;
  112. return SQLITERK_IOERR;
  113. } else if (got > 0) {
  114. left -= got;
  115. cnt += got;
  116. data = data + got;
  117. }
  118. } while (got > 0 && left > 0);
  119. *size = cnt;
  120. if (left > 0) {
  121. return SQLITERK_SHORT_READ;
  122. }
  123. return SQLITERK_OK;
  124. }
  125. int sqliterkOSFileSize(sqliterk_file *file, size_t *filesize)
  126. {
  127. if (!file || file->fd < 0) {
  128. return SQLITERK_MISUSE;
  129. }
  130. struct stat statbuf;
  131. if (fstat(file->fd, &statbuf) != 0) {
  132. file->error = errno;
  133. return SQLITERK_IOERR;
  134. }
  135. *filesize = (size_t) statbuf.st_size;
  136. return SQLITERK_OK;
  137. }
  138. const char *sqliterkOSGetFilePath(sqliterk_file *file)
  139. {
  140. return file->path;
  141. }
  142. void *sqliterkOSMalloc(size_t size)
  143. {
  144. return calloc(size, sizeof(char));
  145. }
  146. void sqliterkOSFree(void *p)
  147. {
  148. free(p);
  149. }
  150. static void
  151. sqliterkDefaultLog(sqliterk_loglevel level, int result, const char *msg)
  152. {
  153. fprintf(stderr, "[%s] %s\n", sqliterkGetResultCodeDescription(result), msg);
  154. }
  155. #define SQLITERK_CONFIG_MAXLOG 4096
  156. static sqliterk_os s_os = {sqliterkDefaultLog};
  157. int sqliterkOSLog(sqliterk_loglevel loglevel,
  158. int result,
  159. const char *format,
  160. ...)
  161. {
  162. char buf[SQLITERK_CONFIG_MAXLOG];
  163. va_list ap;
  164. va_start(ap, format);
  165. vsnprintf(buf, sizeof(buf), format, ap);
  166. va_end(ap);
  167. if (s_os.xLog) {
  168. s_os.xLog(loglevel, result, buf);
  169. }
  170. return result;
  171. }
  172. int sqliterkOSRegister(sqliterk_os os)
  173. {
  174. s_os = os;
  175. return SQLITERK_OK;
  176. }