syscall.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef NATIVE_LIBHDFSPP_LIB_CROSS_PLATFORM_SYSCALL
  19. #define NATIVE_LIBHDFSPP_LIB_CROSS_PLATFORM_SYSCALL
  20. #include <string>
  21. #include <vector>
  22. /**
  23. * The {@link XPlatform} namespace contains components that
  24. * aid in writing cross-platform code.
  25. */
  26. namespace XPlatform {
  27. class Syscall {
  28. public:
  29. /**
  30. * Writes the given string to the application's
  31. * standard output stream.
  32. *
  33. * @param message The string to write to stdout.
  34. * @returns A boolean indicating whether the write
  35. * was successful.
  36. */
  37. static bool WriteToStdout(const std::string& message);
  38. /**
  39. * Writes the given char pointer to the application's
  40. * standard output stream.
  41. *
  42. * @param message The char pointer to write to stdout.
  43. * @returns A boolean indicating whether the write
  44. * was successful.
  45. */
  46. static int WriteToStdout(const char* message);
  47. /**
  48. * Checks whether the {@link str} argument matches the {@link pattern}
  49. * argument, which is a shell wildcard pattern.
  50. *
  51. * @param pattern The wildcard pattern to use.
  52. * @param str The string to match.
  53. * @returns A boolean indicating whether the given {@link str}
  54. * matches {@link pattern}.
  55. */
  56. static bool FnMatch(const std::string& pattern, const std::string& str);
  57. /**
  58. * Clears the given {@link buffer} upto {@link sz_bytes} by
  59. * filling them with zeros. This method is immune to compiler
  60. * optimizations and guarantees that the first {@link sz_bytes} of
  61. * {@link buffer} is cleared. The {@link buffer} must be at least
  62. * as big as {@link sz_bytes}, the behaviour is undefined otherwise.
  63. *
  64. * @param buffer the pointer to the buffer to clear.
  65. * @param sz_bytes the count of the bytes to clear.
  66. */
  67. static void ClearBufferSafely(void* buffer, size_t sz_bytes);
  68. /**
  69. * Performs a case insensitive equality comparison for the two
  70. * given strings {@link a} and {@link b}.
  71. *
  72. * @param a the first string parameter to compare.
  73. * @param b the second string parameter to compare.
  74. * @returns A boolean indicating whether to two strings are the
  75. * same irrespective of their case. Returns true if they match,
  76. * else false.
  77. */
  78. static bool StringCompareIgnoreCase(const std::string& a,
  79. const std::string& b);
  80. /**
  81. * Creates and opens a temporary file with a given {@link pattern}.
  82. * The {@link pattern} must end with a minimum of 6 'X' characters.
  83. * This function will first modify the last 6 'X' characters with
  84. * random character values, which serve as the temporary file name.
  85. * Subsequently opens the file and returns the file descriptor for
  86. * the same. The behaviour of this function is the same as that of
  87. * POSIX mkstemp function. The file must be later closed by the
  88. * application and is not handled by this function.
  89. *
  90. * @param pattern the pattern to be used for the temporary filename.
  91. * @returns an integer representing the file descriptor for the
  92. * opened temporary file. Returns -1 in the case of error and sets
  93. * the global errno with the appropriate error code.
  94. */
  95. static int CreateAndOpenTempFile(std::vector<char>& pattern);
  96. /**
  97. * Closes the file corresponding to given {@link file_descriptor}.
  98. *
  99. * @param file_descriptor the file descriptor of the file to close.
  100. * @returns a boolean indicating the status of the call to this
  101. * function. true if it's a success, false in the case of an error.
  102. * The global errno is set if the call to this function was not
  103. * successful.
  104. */
  105. static bool CloseFile(int file_descriptor);
  106. private:
  107. static bool WriteToStdoutImpl(const char* message);
  108. };
  109. } // namespace XPlatform
  110. #endif