From 085e102ce1234adf1a3ed02eae81bd7a8de88526 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Tue, 21 Dec 2021 07:34:26 +0100 Subject: [PATCH] Added RsFileUtil for os specific file functions Added RsFileUtil::set_fd_nonblock to set a file descriptor to non blocking --- libretroshare/src/CMakeLists.txt | 1 + libretroshare/src/libretroshare.pro | 2 ++ libretroshare/src/tor/TorProcess.cpp | 5 +-- libretroshare/src/util/rsfile.cc | 51 ++++++++++++++++++++++++++++ libretroshare/src/util/rsfile.h | 29 ++++++++++++++++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 libretroshare/src/util/rsfile.cc create mode 100644 libretroshare/src/util/rsfile.h diff --git a/libretroshare/src/CMakeLists.txt b/libretroshare/src/CMakeLists.txt index d3ceffecf..c33799d7c 100644 --- a/libretroshare/src/CMakeLists.txt +++ b/libretroshare/src/CMakeLists.txt @@ -347,6 +347,7 @@ list( util/rsurl.cc util/folderiterator.cc util/rsdir.cc + util/rsfile.cc util/dnsresolver.cc util/extaddrfinder.cc util/rsdebug.cc diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 5ebb64290..69dabca79 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -487,6 +487,7 @@ HEADERS += util/folderiterator.h \ util/rsmemory.h \ util/smallobject.h \ util/rsdir.h \ + util/rsfile.h \ util/argstream.h \ util/rsdiscspace.h \ util/rsnet.h \ @@ -641,6 +642,7 @@ SOURCES += util/folderiterator.cc \ util/rsexpr.cc \ util/smallobject.cc \ util/rsdir.cc \ + util/rsfile.cc \ util/rsdiscspace.cc \ util/rsnet.cc \ util/rsnet_ss.cc \ diff --git a/libretroshare/src/tor/TorProcess.cpp b/libretroshare/src/tor/TorProcess.cpp index 908070fe8..272ddb985 100644 --- a/libretroshare/src/tor/TorProcess.cpp +++ b/libretroshare/src/tor/TorProcess.cpp @@ -35,6 +35,7 @@ #include #include "util/rsdir.h" +#include "util/rsfile.h" #include "pqi/pqifdbin.h" #include "TorProcess.h" @@ -270,8 +271,8 @@ void TorProcess::start() return; // stop the control thread } - unix_fcntl_nonblock(fd[STDOUT_FILENO]); - unix_fcntl_nonblock(fd[STDERR_FILENO]); + RsFileUtil::set_fd_nonblock(fd[STDOUT_FILENO]); + RsFileUtil::set_fd_nonblock(fd[STDERR_FILENO]); mStdOutFD = new RsFdBinInterface(fd[STDOUT_FILENO]); mStdErrFD = new RsFdBinInterface(fd[STDERR_FILENO]); diff --git a/libretroshare/src/util/rsfile.cc b/libretroshare/src/util/rsfile.cc new file mode 100644 index 000000000..fc9f12c21 --- /dev/null +++ b/libretroshare/src/util/rsfile.cc @@ -0,0 +1,51 @@ +/******************************************************************************* + * libretroshare/src/util: rsfile.cc * + * * + * libretroshare: retroshare core library * + * * + * Copyright (C) 2021 Retroshare Team * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public License * + * along with this program. If not, see . * + * * + *******************************************************************************/ + +#include "util/rsfile.h" + +#ifdef WINDOWS_SYS +#include +#include +#include +#else +#include +#endif + +int RsFileUtil::set_fd_nonblock(int fd) +{ + int ret = 0; + +/******************* OS SPECIFIC PART ******************/ +#ifdef WINDOWS_SYS + DWORD mode = PIPE_NOWAIT; + WINBOOL result = SetNamedPipeHandleState((HANDLE) _get_osfhandle(fd), &mode, nullptr, nullptr); + + if (!result) { + ret = -1; + } +#else // ie UNIX + int flags = fcntl(fd, F_GETFL); + ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK); +#endif + + return ret; +} diff --git a/libretroshare/src/util/rsfile.h b/libretroshare/src/util/rsfile.h new file mode 100644 index 000000000..7569b5af5 --- /dev/null +++ b/libretroshare/src/util/rsfile.h @@ -0,0 +1,29 @@ +/******************************************************************************* + * libretroshare/src/util: rsfile.h * + * * + * libretroshare: retroshare core library * + * * + * Copyright (C) 2021 Retroshare Team * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 3 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public License * + * along with this program. If not, see . * + * * + *******************************************************************************/ + +#pragma once + +namespace RsFileUtil { + +int set_fd_nonblock(int fd); + +}