From 80c152d6898c1b8727ac14d32437b274153a7089 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 30 Jun 2017 13:30:52 -0700 Subject: Move the readlink(2) wrapper to qcore_unix.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This deduplicates the code between QFileSystemEngine and QLockFile. Change-Id: I1eba2b016de74620bfc8fffd14cd005d5fd9beaa Reviewed-by: Jędrzej Nowacki --- src/corelib/kernel/qcore_unix.cpp | 33 +++++++++++++++++++++++++++++++++ src/corelib/kernel/qcore_unix_p.h | 2 ++ 2 files changed, 35 insertions(+) (limited to 'src/corelib/kernel') diff --git a/src/corelib/kernel/qcore_unix.cpp b/src/corelib/kernel/qcore_unix.cpp index 686143f8c7..3b0da136ca 100644 --- a/src/corelib/kernel/qcore_unix.cpp +++ b/src/corelib/kernel/qcore_unix.cpp @@ -50,6 +50,37 @@ QT_BEGIN_NAMESPACE +QByteArray qt_readlink(const char *path) +{ +#ifndef PATH_MAX + // suitably large value that won't consume too much memory +# define PATH_MAX 1024*1024 +#endif + + QByteArray buf(256, Qt::Uninitialized); + + ssize_t len = ::readlink(path, buf.data(), buf.size()); + while (len == buf.size()) { + // readlink(2) will fill our buffer and not necessarily terminate with NUL; + if (buf.size() >= PATH_MAX) { + errno = ENAMETOOLONG; + return QByteArray(); + } + + // double the size and try again + buf.resize(buf.size() * 2); + len = ::readlink(path, buf.data(), buf.size()); + } + + if (len == -1) + return QByteArray(); + + buf.resize(len); + return buf; +} + +#ifndef QT_BOOTSTRAPPED + #if QT_CONFIG(poll_pollts) # define ppoll pollts #endif @@ -121,4 +152,6 @@ int qt_safe_poll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout } } +#endif // QT_BOOTSTRAPPED + QT_END_NAMESPACE diff --git a/src/corelib/kernel/qcore_unix_p.h b/src/corelib/kernel/qcore_unix_p.h index ea5d98cbf3..9d2c4f6c31 100644 --- a/src/corelib/kernel/qcore_unix_p.h +++ b/src/corelib/kernel/qcore_unix_p.h @@ -55,6 +55,7 @@ #include #include "qplatformdefs.h" #include "qatomic.h" +#include "qbytearray.h" #ifndef Q_OS_UNIX # error "qcore_unix_p.h included on a non-Unix system" @@ -339,6 +340,7 @@ static inline pid_t qt_safe_waitpid(pid_t pid, int *status, int options) // in qelapsedtimer_mac.cpp or qtimestamp_unix.cpp timespec qt_gettime() Q_DECL_NOTHROW; void qt_nanosleep(timespec amount); +QByteArray qt_readlink(const char *path); /* non-static */ inline bool qt_haveLinuxProcfs() -- cgit v1.2.3