posix/ostream: protect win32 against missing stat members

This commit is contained in:
Danny Robson 2019-05-05 09:30:00 +10:00
parent 82aef3e207
commit c3c36a662b

View File

@ -1,8 +1,12 @@
#include "ostream.hpp" #include "ostream.hpp"
#include "../platform.hpp"
#include <ostream> #include <ostream>
#include <iomanip> #include <iomanip>
#include <sys/stat.h>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
struct type_printer { struct type_printer {
@ -18,9 +22,11 @@ struct type_printer {
std::ostream& operator<< (std::ostream &os, type_printer const &val) std::ostream& operator<< (std::ostream &os, type_printer const &val)
{ {
os << "[ "; os << "[ ";
#ifndef PLATFORM_WIN32
if (val.data.st_mode & S_ISUID) os << "SUID, "; if (val.data.st_mode & S_ISUID) os << "SUID, ";
if (val.data.st_mode & S_ISGID) os << "SGID, "; if (val.data.st_mode & S_ISGID) os << "SGID, ";
if (val.data.st_mode & S_ISVTX) os << "STICKY, "; if (val.data.st_mode & S_ISVTX) os << "STICKY, ";
#endif
os << "{ user: " << (val.data.st_mode & S_IRUSR ? 'r' : '_') os << "{ user: " << (val.data.st_mode & S_IRUSR ? 'r' : '_')
<< (val.data.st_mode & S_IWUSR ? 'w' : '_') << (val.data.st_mode & S_IWUSR ? 'w' : '_')
@ -53,13 +59,15 @@ struct mode_printer {
std::ostream& operator<< (std::ostream &os, mode_printer const &val) std::ostream& operator<< (std::ostream &os, mode_printer const &val)
{ {
return S_ISREG (val.data.st_mode) ? os << "REGULAR" return S_ISREG (val.data.st_mode) ? os << "REGULAR"
: S_ISDIR (val.data.st_mode) ? os << "DIRECTORY" : S_ISDIR (val.data.st_mode) ? os << "DIRECTORY"
: S_ISCHR (val.data.st_mode) ? os << "CHARACTER" : S_ISCHR (val.data.st_mode) ? os << "CHARACTER"
: S_ISBLK (val.data.st_mode) ? os << "BLOCK" : S_ISBLK (val.data.st_mode) ? os << "BLOCK"
: S_ISFIFO (val.data.st_mode) ? os << "FIFO" : S_ISFIFO (val.data.st_mode) ? os << "FIFO"
: S_ISLNK (val.data.st_mode) ? os << "SYMLINK" #ifndef PLATFORM_WIN32
: S_ISSOCK (val.data.st_mode) ? os << "SOCKET" : S_ISLNK (val.data.st_mode) ? os << "SYMLINK"
: (throw std::invalid_argument ("Unhandled mode_t"), os << "_error"); : S_ISSOCK (val.data.st_mode) ? os << "SOCKET"
#endif
: (throw std::invalid_argument ("Unhandled mode_t"), os << "_error");
} }
@ -73,6 +81,8 @@ std::ostream& operator<< (std::ostream &os, struct stat const &val)
<< ", uid: " << val.st_uid << ", uid: " << val.st_uid
<< ", gid: " << val.st_gid << ", gid: " << val.st_gid
<< ", size: " << val.st_size << ", size: " << val.st_size
#ifndef PLATFORM_WIN32
<< ", blocksize: " << val.st_blksize << ", blocksize: " << val.st_blksize
#endif
<< " }"; << " }";
} }