platform-nix.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <errno.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <dirent.h>
  20. #include <stdlib.h>
  21. #include <dlfcn.h>
  22. #include <unistd.h>
  23. #include <time.h>
  24. #include "dstr.h"
  25. #include "platform.h"
  26. void *os_dlopen(const char *path)
  27. {
  28. struct dstr dylib_name;
  29. if (!path)
  30. return NULL;
  31. dstr_init_copy(&dylib_name, path);
  32. if (!dstr_find(&dylib_name, ".so"))
  33. dstr_cat(&dylib_name, ".so");
  34. void *res = dlopen(dylib_name.array, RTLD_LAZY);
  35. if (!res)
  36. blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
  37. path, dylib_name.array, dlerror());
  38. dstr_free(&dylib_name);
  39. return res;
  40. }
  41. void *os_dlsym(void *module, const char *func)
  42. {
  43. return dlsym(module, func);
  44. }
  45. void os_dlclose(void *module)
  46. {
  47. dlclose(module);
  48. }
  49. bool os_sleepto_ns(uint64_t time_target)
  50. {
  51. uint64_t current = os_gettime_ns();
  52. if (time_target < current)
  53. return false;
  54. time_target -= current;
  55. struct timespec req, remain;
  56. memset(&req, 0, sizeof(req));
  57. memset(&remain, 0, sizeof(remain));
  58. req.tv_sec = time_target/1000000000;
  59. req.tv_nsec = time_target%1000000000;
  60. while (nanosleep(&req, &remain)) {
  61. req = remain;
  62. memset(&remain, 0, sizeof(remain));
  63. }
  64. return true;
  65. }
  66. void os_sleep_ms(uint32_t duration)
  67. {
  68. usleep(duration*1000);
  69. }
  70. #if !defined(__APPLE__)
  71. uint64_t os_gettime_ns(void)
  72. {
  73. struct timespec ts;
  74. clock_gettime(CLOCK_MONOTONIC, &ts);
  75. return ((uint64_t) ts.tv_sec * 1000000000ULL + (uint64_t) ts.tv_nsec);
  76. }
  77. /* should return $HOME/.[name] */
  78. char *os_get_config_path(const char *name)
  79. {
  80. char *path_ptr = getenv("HOME");
  81. if (path_ptr == NULL)
  82. bcrash("Could not get $HOME\n");
  83. struct dstr path;
  84. dstr_init_copy(&path, path_ptr);
  85. dstr_cat(&path, "/.");
  86. dstr_cat(&path, name);
  87. return path.array;
  88. }
  89. #endif
  90. bool os_file_exists(const char *path)
  91. {
  92. return access(path, F_OK) == 0;
  93. }
  94. struct os_dir {
  95. const char *path;
  96. DIR *dir;
  97. struct dirent *cur_dirent;
  98. struct os_dirent out;
  99. };
  100. os_dir_t os_opendir(const char *path)
  101. {
  102. struct os_dir *dir;
  103. DIR *dir_val;
  104. dir_val = opendir(path);
  105. if (!dir_val)
  106. return NULL;
  107. dir = bzalloc(sizeof(struct os_dir));
  108. dir->dir = dir_val;
  109. dir->path = path;
  110. return dir;
  111. }
  112. struct os_dirent *os_readdir(os_dir_t dir)
  113. {
  114. struct dstr file_path = {0};
  115. struct stat stat_info;
  116. if (!dir) return NULL;
  117. dir->cur_dirent = readdir(dir->dir);
  118. if (!dir->cur_dirent)
  119. return NULL;
  120. strncpy(dir->out.d_name, dir->cur_dirent->d_name, 255);
  121. dstr_copy(&file_path, dir->path);
  122. dstr_cat(&file_path, "/");
  123. dstr_cat(&file_path, dir->out.d_name);
  124. if (stat(file_path.array, &stat_info) == 0)
  125. dir->out.directory = !!S_ISDIR(stat_info.st_mode);
  126. else
  127. blog(LOG_DEBUG, FILE_LINE "stat for %s failed, errno: %d",
  128. file_path.array, errno);
  129. dstr_free(&file_path);
  130. return &dir->out;
  131. }
  132. void os_closedir(os_dir_t dir)
  133. {
  134. if (dir) {
  135. closedir(dir->dir);
  136. bfree(dir);
  137. }
  138. }
  139. int os_unlink(const char *path)
  140. {
  141. return unlink(path);
  142. }
  143. int os_mkdir(const char *path)
  144. {
  145. if (mkdir(path, 0777) == 0)
  146. return MKDIR_SUCCESS;
  147. return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
  148. }