filesys.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // This file is part of BOINC.
  2. // http://boinc.berkeley.edu
  3. // Copyright (C) 2008 University of California
  4. //
  5. // BOINC is free software; you can redistribute it and/or modify it
  6. // under the terms of the GNU Lesser General Public License
  7. // as published by the Free Software Foundation,
  8. // either version 3 of the License, or (at your option) any later version.
  9. //
  10. // BOINC is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. // See the GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef BOINC_FILESYS_H
  18. #define BOINC_FILESYS_H
  19. #if defined(_WIN32) && !defined(__CYGWIN32__)
  20. #include "boinc_win.h"
  21. #else
  22. #include <dirent.h>
  23. #include <grp.h>
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #include <sys/param.h>
  27. #ifdef __cplusplus
  28. #include <string>
  29. #endif
  30. #endif /* !WIN32 */
  31. #ifndef MAXPATHLEN
  32. #define MAXPATHLEN 4096
  33. #endif
  34. // use the following in format codes in snprintf
  35. //
  36. #define DIR_LEN 2048
  37. #define FILE_LEN 256
  38. #define FILE_RETRY_INTERVAL 5
  39. // On Windows, retry for this period of time, since some other program
  40. // (virus scan, defrag, index) may have the file open.
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. extern int boinc_delete_file(const char*);
  45. extern int boinc_touch_file(const char *path);
  46. extern FILE* boinc_fopen(const char* path, const char* mode);
  47. // like fopen(), except:
  48. // retry a few times on failure
  49. // Unix: set close-on-exec flag
  50. extern int boinc_copy(const char* orig, const char* newf);
  51. extern int boinc_rename(const char* old, const char* newf);
  52. extern int boinc_mkdir(const char*);
  53. #ifdef _WIN32
  54. extern int boinc_allocate_file(const char*, double size);
  55. #else
  56. extern int boinc_copy_attributes(const char* orig, const char* newf);
  57. extern int boinc_chown(const char*, gid_t);
  58. #endif
  59. extern int boinc_rmdir(const char*);
  60. extern void boinc_getcwd(char*);
  61. extern void relative_to_absolute(const char* relname, char* path);
  62. extern int boinc_make_dirs(const char*, const char*);
  63. extern char boinc_failed_file[MAXPATHLEN];
  64. extern int is_file(const char* path);
  65. extern int is_dir(const char* path);
  66. extern int is_file_follow_symlinks(const char* path);
  67. extern int is_dir_follow_symlinks(const char* path);
  68. extern int is_symlink(const char* path);
  69. extern int boinc_truncate(const char*, double);
  70. extern int boinc_file_exists(const char* path);
  71. extern int boinc_file_or_symlink_exists(const char* path);
  72. #ifdef _WIN32
  73. extern FILE* boinc_temp_file(
  74. const char* dir, const char* prefix, char* temp_path, double size
  75. );
  76. #else
  77. extern FILE* boinc_temp_file(const char* dir, const char* prefix, char* temp_path);
  78. #endif
  79. extern void boinc_path_to_dir(const char* path, char* dir);
  80. // given a file path, get path of its directory
  81. // (i.e. remove the last / and what follows)
  82. #ifdef __cplusplus
  83. }
  84. #endif
  85. /* C++ specific prototypes/defines follow here */
  86. #ifdef __cplusplus
  87. extern int file_size(const char*, double&);
  88. extern int file_size_alloc(const char*, double&);
  89. extern int dir_size(const char* dirpath, double&, bool recurse=true);
  90. extern int dir_size_alloc(const char* dirpath, double&, bool recurse=true);
  91. extern int clean_out_dir(const char*);
  92. extern int get_filesystem_info(double& total, double& free, char* path=const_cast<char *>("."));
  93. extern bool is_path_absolute(const std::string path);
  94. // TODO TODO TODO
  95. // remove this code - the DirScanner class does the same thing.
  96. // But need to rewrite a couple of places that use it
  97. //
  98. #if defined(_WIN32) && !defined(__CYGWIN32__)
  99. typedef struct _DIR_DESC {
  100. char path[MAXPATHLEN];
  101. bool first;
  102. void* handle;
  103. } DIR_DESC;
  104. typedef DIR_DESC *DIRREF;
  105. #else
  106. typedef DIR *DIRREF;
  107. #endif
  108. extern DIRREF dir_open(const char*);
  109. extern int dir_scan(char*, DIRREF, int);
  110. extern int dir_scan(std::string&, DIRREF);
  111. extern void dir_close(DIRREF);
  112. extern bool is_dir_empty(const char*);
  113. class DirScanner {
  114. #if defined(_WIN32) && !defined(__CYGWIN32__)
  115. std::string dir;
  116. bool first;
  117. void* handle;
  118. #else
  119. DIR* dirp;
  120. #endif
  121. public:
  122. DirScanner(std::string const& path);
  123. ~DirScanner();
  124. bool scan(std::string& name); // return true if file returned
  125. };
  126. struct FILE_LOCK {
  127. #if defined(_WIN32) && !defined(__CYGWIN32__)
  128. HANDLE handle;
  129. #else
  130. int fd;
  131. #endif
  132. bool locked;
  133. FILE_LOCK();
  134. ~FILE_LOCK();
  135. int lock(const char* filename);
  136. int unlock(const char* filename);
  137. };
  138. #endif /* c++ */
  139. #endif /* double-inclusion protection */