-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HDFS-15843. Make write cross-platform (#2710)
- Loading branch information
1 parent
73394fa
commit 47620f8
Showing
11 changed files
with
210 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/x-platform/c_api.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "syscall.h" | ||
|
||
extern "C" int x_platform_syscall_write_to_stdout(const char* msg) { | ||
return XPlatform::Syscall::WriteToStdout(msg) ? 1 : 0; | ||
} |
28 changes: 28 additions & 0 deletions
28
...p-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/x-platform/c_api.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef NATIVE_LIBHDFSPP_LIB_CROSS_PLATFORM_C_API_H | ||
#define NATIVE_LIBHDFSPP_LIB_CROSS_PLATFORM_C_API_H | ||
|
||
/** | ||
* C APIs for accessing XPlatform | ||
*/ | ||
|
||
int x_platform_syscall_write_to_stdout(const char* msg); | ||
|
||
#endif // NATIVE_LIBHDFSPP_LIB_CROSS_PLATFORM_C_API_H |
56 changes: 56 additions & 0 deletions
56
...hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/x-platform/syscall.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef NATIVE_LIBHDFSPP_LIB_CROSS_PLATFORM_SYSCALL | ||
#define NATIVE_LIBHDFSPP_LIB_CROSS_PLATFORM_SYSCALL | ||
|
||
#include <string> | ||
|
||
/** | ||
* The {@link XPlatform} namespace contains components that | ||
* aid in writing cross-platform code. | ||
*/ | ||
namespace XPlatform { | ||
class Syscall { | ||
public: | ||
/** | ||
* Writes the given string to the application's | ||
* standard output stream. | ||
* | ||
* @param message The string to write to stdout. | ||
* @returns A boolean indicating whether the write | ||
* was successful. | ||
*/ | ||
static bool WriteToStdout(const std::string& message); | ||
|
||
/** | ||
* Writes the given char pointer to the application's | ||
* standard output stream. | ||
* | ||
* @param message The char pointer to write to stdout. | ||
* @returns A boolean indicating whether the write | ||
* was successful. | ||
*/ | ||
static int WriteToStdout(const char* message); | ||
|
||
private: | ||
static bool WriteToStdoutImpl(const char* message); | ||
}; | ||
} // namespace XPlatform | ||
|
||
#endif |
37 changes: 37 additions & 0 deletions
37
...oject/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/x-platform/syscall_linux.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <unistd.h> | ||
|
||
#include <cstring> | ||
|
||
#include "syscall.h" | ||
|
||
bool XPlatform::Syscall::WriteToStdout(const std::string& message) { | ||
return WriteToStdoutImpl(message.c_str()); | ||
} | ||
|
||
int XPlatform::Syscall::WriteToStdout(const char* message) { | ||
return WriteToStdoutImpl(message) ? 1 : 0; | ||
} | ||
|
||
bool XPlatform::Syscall::WriteToStdoutImpl(const char* message) { | ||
const auto message_len = strlen(message); | ||
const auto result = write(1, message, message_len); | ||
return result == static_cast<ssize_t>(message_len); | ||
} |
42 changes: 42 additions & 0 deletions
42
...ect/hadoop-hdfs-native-client/src/main/native/libhdfspp/lib/x-platform/syscall_windows.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <Windows.h> | ||
|
||
#include "syscall.h" | ||
|
||
bool XPlatform::Syscall::WriteToStdout(const std::string& message) { | ||
return WriteToStdoutImpl(message.c_str()); | ||
} | ||
|
||
int XPlatform::Syscall::WriteToStdout(const char* message) { | ||
return WriteToStdoutImpl(message) ? 1 : 0; | ||
} | ||
|
||
bool XPlatform::Syscall::WriteToStdoutImpl(const char* message) { | ||
auto* const stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); | ||
if (stdout_handle == INVALID_HANDLE_VALUE || stdout_handle == nullptr) { | ||
return false; | ||
} | ||
|
||
unsigned long bytes_written = 0; | ||
const auto message_len = lstrlen(message); | ||
const auto result = | ||
WriteFile(stdout_handle, message, message_len, &bytes_written, nullptr); | ||
return result && static_cast<unsigned long>(message_len) == bytes_written; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters