|
34 | 34 | #include "FileUtils.h" |
35 | 35 |
|
36 | 36 | #include "Basics/operating-system.h" |
| 37 | +#include "Basics/process-utils.h" |
37 | 38 |
|
38 | 39 | #ifdef TRI_HAVE_DIRENT_H |
39 | 40 | #include <dirent.h> |
@@ -739,80 +740,51 @@ void makePathAbsolute(std::string& path) { |
739 | 740 | } |
740 | 741 | } |
741 | 742 |
|
742 | | -static void throwProgramError(std::string const& filename) { |
743 | | - auto res = TRI_set_errno(TRI_ERROR_SYS_ERROR); |
744 | | - |
745 | | - LOG_TOPIC("a557b", TRACE, arangodb::Logger::FIXME) |
746 | | - << StringUtils::concatT("open failed for file '", filename, "': ", TRI_last_error()); |
747 | | - |
748 | | - THROW_ARANGO_EXCEPTION(res); |
749 | | -} |
750 | | - |
751 | 743 | std::string slurpProgram(std::string const& program) { |
752 | | -#ifdef _WIN32 |
753 | | - icu::UnicodeString uprog(program.c_str(), static_cast<int32_t>(program.length())); |
754 | | - FILE* fp = _wpopen(reinterpret_cast<wchar_t const*>(uprog.getTerminatedBuffer()), L"r"); |
755 | | -#else |
756 | | - FILE* fp = popen(program.c_str(), "r"); |
757 | | -#endif |
758 | | - |
759 | | - constexpr size_t chunkSize = 8192; |
760 | | - StringBuffer buffer(chunkSize, false); |
761 | | - |
762 | | - if (fp) { |
763 | | - int c; |
764 | | - |
765 | | - while ((c = getc(fp)) != EOF) { |
766 | | - buffer.appendChar((char)c); |
767 | | - } |
768 | | - |
769 | | -#ifdef _WIN32 |
770 | | - int res = _pclose(fp); |
771 | | -#else |
772 | | - int res = pclose(fp); |
773 | | -#endif |
| 744 | + ExternalProcess const* process; |
| 745 | + ExternalId external; |
| 746 | + ExternalProcessStatus res; |
| 747 | + std::string output; |
| 748 | + std::vector<std::string> moreArgs; |
| 749 | + std::vector<std::string> additionalEnv; |
| 750 | + char buf[1024]; |
| 751 | + |
| 752 | + moreArgs.push_back(std::string("version")); |
| 753 | + |
| 754 | + TRI_CreateExternalProcess(program.c_str(), |
| 755 | + moreArgs, |
| 756 | + additionalEnv, |
| 757 | + true, |
| 758 | + &external); |
| 759 | + if (external._pid == TRI_INVALID_PROCESS_ID) { |
| 760 | + auto res = TRI_set_errno(TRI_ERROR_SYS_ERROR); |
774 | 761 |
|
775 | | - if (res != 0) { |
776 | | - throwProgramError(program); |
777 | | - } |
778 | | - } else { |
779 | | - throwProgramError(program); |
| 762 | + LOG_TOPIC("a557b", TRACE, arangodb::Logger::FIXME) |
| 763 | + << StringUtils::concatT("open failed for file '", |
| 764 | + program, "': ", |
| 765 | + TRI_last_error()); |
| 766 | + THROW_ARANGO_EXCEPTION(res); |
780 | 767 | } |
| 768 | + process = TRI_LookupSpawnedProcess(external._pid); |
| 769 | + if (process == nullptr) { |
| 770 | + auto res = TRI_set_errno(TRI_ERROR_SYS_ERROR); |
781 | 771 |
|
782 | | - return std::string(buffer.data(), buffer.length()); |
783 | | -} |
784 | | - |
785 | | -int slurpProgramWithExitcode(std::string const& program, std::string& output) { |
786 | | -#ifdef _WIN32 |
787 | | - icu::UnicodeString uprog(program.c_str(), static_cast<int32_t>(program.length())); |
788 | | - FILE* fp = _wpopen(reinterpret_cast<wchar_t const*>(uprog.getTerminatedBuffer()), L"r"); |
789 | | -#else |
790 | | - FILE* fp = popen(program.c_str(), "r"); |
791 | | -#endif |
792 | | - |
793 | | - constexpr size_t chunkSize = 8192; |
794 | | - StringBuffer buffer(chunkSize, false); |
795 | | - |
796 | | - if (fp) { |
797 | | - int c; |
798 | | - |
799 | | - while ((c = getc(fp)) != EOF) { |
800 | | - buffer.appendChar((char)c); |
| 772 | + LOG_TOPIC("a557c", TRACE, arangodb::Logger::FIXME) |
| 773 | + << StringUtils::concatT("process gone? '", |
| 774 | + program, "': ", |
| 775 | + TRI_last_error()); |
| 776 | + THROW_ARANGO_EXCEPTION(res); |
| 777 | + } |
| 778 | + while (res = TRI_CheckExternalProcess(external, false, 0), |
| 779 | + (res._status == TRI_EXT_RUNNING)) { |
| 780 | + auto nRead = TRI_ReadPipe(process, buf, sizeof(buf) - 1); |
| 781 | + if (nRead > 0) { |
| 782 | + output.append(buf, nRead); |
801 | 783 | } |
802 | | - |
803 | | -#ifdef _WIN32 |
804 | | - int res = _pclose(fp); |
805 | | -#else |
806 | | - int res = pclose(fp); |
807 | | -#endif |
808 | | - |
809 | | - output = std::string(buffer.data(), buffer.length()); |
810 | | - return res; |
811 | 784 | } |
| 785 | + return output; |
| 786 | +} |
812 | 787 |
|
813 | | - throwProgramError(program); |
814 | | - return 1; // Just to please the compiler. |
815 | | -}; |
816 | 788 |
|
817 | 789 | } // namespace FileUtils |
818 | 790 | } // namespace basics |
|
0 commit comments