platform_utils.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2016 The Bazel Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef THIRD_PARTY_IJAR_PLATFORM_UTILS_H_
  15. #define THIRD_PARTY_IJAR_PLATFORM_UTILS_H_
  16. #include <stdlib.h>
  17. #include <sys/stat.h>
  18. #include <sys/types.h>
  19. #include <string>
  20. #include "third_party/ijar/common.h"
  21. namespace devtools_ijar {
  22. // Platform-independent stat data.
  23. struct Stat {
  24. // Total size of the file in bytes.
  25. int total_size;
  26. // The Unix file mode from the stat.st_mode field.
  27. mode_t file_mode;
  28. // True if this is a directory.
  29. bool is_directory;
  30. };
  31. // Converts a Stat object to ZIP attributes.
  32. inline u4 stat_to_zipattr(const Stat& file_stat) {
  33. return (((u4)file_stat.file_mode) << 16) |
  34. (file_stat.is_directory != 0 ? 0x10 : 0);
  35. }
  36. // Writes stat data into `result` about the file under `path`.
  37. // Returns true if file is found and can be stat'ed.
  38. // Returns false if the file is not found or cannot be stat'ed.
  39. // Doesn't report any errors because it can also be used to simply check if a
  40. // file exists.
  41. bool stat_file(const char* path, Stat* result);
  42. // Writes `size` bytes from `data` into file under `path`.
  43. // The file is created or overwritten and is set to have `perm` permissions.
  44. // Returns true upon success: file is created and all data is written.
  45. // Returns false upon failure and reports the error to stderr.
  46. bool write_file(const char* path, unsigned int perm, const void* data,
  47. size_t size);
  48. // Reads at most `size` bytes into `buffer` from the file under `path`.
  49. // Returns true upon success: file is opened and all data is read.
  50. // Returns false upon failure and reports the error to stderr.
  51. bool read_file(const char* path, void* buffer, size_t size);
  52. // Returns the current working directory.
  53. // Returns the empty string upon failure and reports the error to stderr.
  54. std::string get_cwd();
  55. // Do a recursive mkdir of all folders of path except the last path
  56. // segment (if path ends with a / then the last path segment is empty).
  57. // All folders are created using "perm" for creation mode, and are writable and
  58. // openable by the current user.
  59. // Returns true if all directories were created and permissions set.
  60. // Returns false upon failure and reports the error to stderr.
  61. bool make_dirs(const char* path, unsigned int perm);
  62. } // namespace devtools_ijar
  63. #endif // THIRD_PARTY_IJAR_PLATFORM_UTILS_H_