mirror of https://github.com/oxen-io/lokinet
Merge branch 'master' of https://github.com/loki-project/loki-network
commit
4b3b09736b
File diff suppressed because it is too large
Load Diff
@ -1,157 +0,0 @@
|
||||
#ifdef _MSC_VER
|
||||
/* Declarations for getopt.
|
||||
Copyright (C) 1989,90,91,92,93,94,96,97,98 Free Software Foundation, Inc.
|
||||
This file is part of the GNU C Library.
|
||||
|
||||
The GNU C Library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public License as
|
||||
published by the Free Software Foundation; either version 2 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
The GNU C Library 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
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with the GNU C Library; see the file COPYING.LIB. If not,
|
||||
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
Boston, MA 02111-1307, USA. */
|
||||
|
||||
#ifndef _GETOPT_H
|
||||
|
||||
#ifndef __need_getopt
|
||||
# define _GETOPT_H 1
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* For communication from `getopt' to the caller.
|
||||
When `getopt' finds an option that takes an argument,
|
||||
the argument value is returned here.
|
||||
Also, when `ordering' is RETURN_IN_ORDER,
|
||||
each non-option ARGV-element is returned here. */
|
||||
|
||||
extern char *optarg;
|
||||
|
||||
/* Index in ARGV of the next element to be scanned.
|
||||
This is used for communication to and from the caller
|
||||
and for communication between successive calls to `getopt'.
|
||||
|
||||
On entry to `getopt', zero means this is the first call; initialize.
|
||||
|
||||
When `getopt' returns -1, this is the index of the first of the
|
||||
non-option elements that the caller should itself scan.
|
||||
|
||||
Otherwise, `optind' communicates from one call to the next
|
||||
how much of ARGV has been scanned so far. */
|
||||
|
||||
extern int optind;
|
||||
|
||||
/* Callers store zero here to inhibit the error message `getopt' prints
|
||||
for unrecognized options. */
|
||||
|
||||
extern int opterr;
|
||||
|
||||
/* Set to an option character which was unrecognized. */
|
||||
|
||||
extern int optopt;
|
||||
|
||||
#ifndef __need_getopt
|
||||
/* Describe the long-named options requested by the application.
|
||||
The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
|
||||
of `struct option' terminated by an element containing a name which is
|
||||
zero.
|
||||
|
||||
The field `has_arg' is:
|
||||
no_argument (or 0) if the option does not take an argument,
|
||||
required_argument (or 1) if the option requires an argument,
|
||||
optional_argument (or 2) if the option takes an optional argument.
|
||||
|
||||
If the field `flag' is not NULL, it points to a variable that is set
|
||||
to the value given in the field `val' when the option is found, but
|
||||
left unchanged if the option is not found.
|
||||
|
||||
To have a long-named option do something other than set an `int' to
|
||||
a compiled-in constant, such as set a value from `optarg', set the
|
||||
option's `flag' field to zero and its `val' field to a nonzero
|
||||
value (the equivalent single-letter option character, if there is
|
||||
one). For long options that have a zero `flag' field, `getopt'
|
||||
returns the contents of the `val' field. */
|
||||
|
||||
struct option
|
||||
{
|
||||
# if defined __STDC__ && __STDC__
|
||||
const char *name;
|
||||
# else
|
||||
char *name;
|
||||
# endif
|
||||
/* has_arg can't be an enum because some compilers complain about
|
||||
type mismatches in all the code that assumes it is an int. */
|
||||
int has_arg;
|
||||
int *flag;
|
||||
int val;
|
||||
};
|
||||
|
||||
/* Names for the values of the `has_arg' field of `struct option'. */
|
||||
|
||||
# define no_argument 0
|
||||
# define required_argument 1
|
||||
# define optional_argument 2
|
||||
#endif /* need getopt */
|
||||
|
||||
|
||||
/* Get definitions and prototypes for functions to process the
|
||||
arguments in ARGV (ARGC of them, minus the program name) for
|
||||
options given in OPTS.
|
||||
|
||||
Return the option character from OPTS just read. Return -1 when
|
||||
there are no more options. For unrecognized options, or options
|
||||
missing arguments, `optopt' is set to the option letter, and '?' is
|
||||
returned.
|
||||
|
||||
The OPTS string is a list of characters which are recognized option
|
||||
letters, optionally followed by colons, specifying that that letter
|
||||
takes an argument, to be placed in `optarg'.
|
||||
|
||||
If a letter in OPTS is followed by two colons, its argument is
|
||||
optional. This behavior is specific to the GNU `getopt'.
|
||||
|
||||
The argument `--' causes premature termination of argument
|
||||
scanning, explicitly telling `getopt' that there are no more
|
||||
options.
|
||||
|
||||
If OPTS begins with `--', then non-option arguments are treated as
|
||||
arguments to the option '\0'. This behavior is specific to the GNU
|
||||
`getopt'. */
|
||||
|
||||
/* Many other libraries have conflicting prototypes for getopt, with
|
||||
differences in the consts, in stdlib.h. To avoid compilation
|
||||
errors, only prototype getopt for the GNU C library. */
|
||||
extern int getopt (int __argc, char *const *__argv, const char *__shortopts);
|
||||
|
||||
# ifndef __need_getopt
|
||||
extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts,
|
||||
const struct option *__longopts, int *__longind);
|
||||
extern int getopt_long_only (int __argc, char *const *__argv,
|
||||
const char *__shortopts,
|
||||
const struct option *__longopts, int *__longind);
|
||||
|
||||
/* Internal only. Users should not call this directly. */
|
||||
extern int _getopt_internal (int __argc, char *const *__argv,
|
||||
const char *__shortopts,
|
||||
const struct option *__longopts, int *__longind,
|
||||
int __long_only);
|
||||
# endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Make sure we later can get all the definitions and declarations. */
|
||||
#undef __need_getopt
|
||||
|
||||
#endif /* getopt.h */
|
||||
#endif /* _MSC_VER */
|
@ -1,28 +1,31 @@
|
||||
#include <llarp/time.h>
|
||||
#include <chrono>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
typedef std::chrono::system_clock clock_t;
|
||||
|
||||
template < typename Res, typename IntType >
|
||||
static IntType
|
||||
time_since_epoch()
|
||||
{
|
||||
return std::chrono::duration_cast< Res >(
|
||||
llarp::clock_t::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
} // namespace llarp
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
// these _should_ be 32-bit safe...
|
||||
llarp_time_t
|
||||
llarp_time_now_ms()
|
||||
{
|
||||
return llarp::time_since_epoch< std::chrono::milliseconds, llarp_time_t >();
|
||||
struct timeval tv;
|
||||
struct timezone z;
|
||||
z.tz_minuteswest = 0;
|
||||
time_t t = time(nullptr);
|
||||
z.tz_dsttime = gmtime(&t)->tm_isdst;
|
||||
gettimeofday(&tv, &z);
|
||||
llarp_time_t timeNow =
|
||||
(llarp_time_t)(tv.tv_sec) * 1000 + (llarp_time_t)(tv.tv_usec) / 1000;
|
||||
return timeNow;
|
||||
}
|
||||
|
||||
llarp_seconds_t
|
||||
llarp_time_now_sec()
|
||||
{
|
||||
return llarp::time_since_epoch< std::chrono::seconds, llarp_seconds_t >();
|
||||
struct timeval tv;
|
||||
struct timezone z;
|
||||
z.tz_minuteswest = 0;
|
||||
time_t t = time(nullptr);
|
||||
z.tz_dsttime = gmtime(&t)->tm_isdst;
|
||||
gettimeofday(&tv, &z);
|
||||
llarp_time_t timeNow = tv.tv_sec;
|
||||
return timeNow;
|
||||
}
|
||||
|
@ -1,138 +1,144 @@
|
||||
/* Copyright (c) 2014, Pollard Banknote Limited
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "cleanpath.h"
|
||||
#include <algorithm>
|
||||
|
||||
#if defined(_WIN32) && !defined(NOMINMAX)
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
|
||||
namespace cpp17
|
||||
{
|
||||
namespace filesystem
|
||||
{
|
||||
/// @todo Probably belongs in path.cpp
|
||||
std::string cleanpath(const std::string& s)
|
||||
{
|
||||
std::string t;
|
||||
|
||||
if ( s.empty() )
|
||||
{
|
||||
|
||||
// error
|
||||
return t;
|
||||
}
|
||||
|
||||
const std::size_t n = s.length();
|
||||
|
||||
for ( std::size_t i = 0; i < n;)
|
||||
{
|
||||
if ( s[i] == '/' )
|
||||
{
|
||||
// directory separator
|
||||
t.push_back('/');
|
||||
i = s.find_first_not_of('/', i);
|
||||
}
|
||||
else
|
||||
{
|
||||
// path component
|
||||
const std::size_t j = std::min(s.find('/', i), n);
|
||||
|
||||
if ( s.compare(i, j - i, ".", 1) == 0 )
|
||||
{
|
||||
// handle dot
|
||||
if ( j < n )
|
||||
{
|
||||
i = s.find_first_not_of('/', j);
|
||||
}
|
||||
else
|
||||
{
|
||||
i = n;
|
||||
}
|
||||
}
|
||||
else if ( s.compare(i, j - i, "..", 2) == 0 )
|
||||
{
|
||||
// handle dot-dot
|
||||
const std::size_t l = t.length();
|
||||
|
||||
if ( l == 0 )
|
||||
{
|
||||
// no previous component (ex., "../src")
|
||||
t.assign("..", 2);
|
||||
i = j;
|
||||
}
|
||||
else
|
||||
{
|
||||
// remove previously copied component (unless root)
|
||||
if ( l >= 2 )
|
||||
{
|
||||
const std::size_t k = t.find_last_of('/', l - 2);
|
||||
|
||||
if ( k == std::string::npos )
|
||||
{
|
||||
t.clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
t.resize(k + 1);
|
||||
}
|
||||
}
|
||||
|
||||
if ( j < n )
|
||||
{
|
||||
i = s.find_first_not_of('/', j);
|
||||
}
|
||||
else
|
||||
{
|
||||
i = n;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// append path component
|
||||
t.append(s, i, j - i);
|
||||
i = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( t.empty() )
|
||||
{
|
||||
return ".";
|
||||
}
|
||||
|
||||
// drop trailing slashes
|
||||
const std::size_t i = t.find_last_not_of('/');
|
||||
|
||||
if ( i != std::string::npos )
|
||||
{
|
||||
t.resize(i + 1);
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
namespace filesystem
|
||||
{
|
||||
/// @todo Probably belongs in path.cpp
|
||||
std::string
|
||||
cleanpath(const std::string& s)
|
||||
{
|
||||
std::string t;
|
||||
|
||||
if(s.empty())
|
||||
{
|
||||
// error
|
||||
return t;
|
||||
}
|
||||
|
||||
const std::size_t n = s.length();
|
||||
|
||||
for(std::size_t i = 0; i < n;)
|
||||
{
|
||||
if(s[i] == '/')
|
||||
{
|
||||
// directory separator
|
||||
t.push_back('/');
|
||||
i = s.find_first_not_of('/', i);
|
||||
}
|
||||
else
|
||||
{
|
||||
// path component
|
||||
const std::size_t j = std::min(s.find('/', i), n);
|
||||
|
||||
if(s.compare(i, j - i, ".", 1) == 0)
|
||||
{
|
||||
// handle dot
|
||||
if(j < n)
|
||||
{
|
||||
i = s.find_first_not_of('/', j);
|
||||
}
|
||||
else
|
||||
{
|
||||
i = n;
|
||||
}
|
||||
}
|
||||
else if(s.compare(i, j - i, "..", 2) == 0)
|
||||
{
|
||||
// handle dot-dot
|
||||
const std::size_t l = t.length();
|
||||
|
||||
if(l == 0)
|
||||
{
|
||||
// no previous component (ex., "../src")
|
||||
t.assign("..", 2);
|
||||
i = j;
|
||||
}
|
||||
else
|
||||
{
|
||||
// remove previously copied component (unless root)
|
||||
if(l >= 2)
|
||||
{
|
||||
const std::size_t k = t.find_last_of('/', l - 2);
|
||||
|
||||
if(k == std::string::npos)
|
||||
{
|
||||
t.clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
t.resize(k + 1);
|
||||
}
|
||||
}
|
||||
|
||||
if(j < n)
|
||||
{
|
||||
i = s.find_first_not_of('/', j);
|
||||
}
|
||||
else
|
||||
{
|
||||
i = n;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// append path component
|
||||
t.append(s, i, j - i);
|
||||
i = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(t.empty())
|
||||
{
|
||||
return ".";
|
||||
}
|
||||
|
||||
// drop trailing slashes
|
||||
const std::size_t i = t.find_last_not_of('/');
|
||||
|
||||
if(i != std::string::npos)
|
||||
{
|
||||
t.resize(i + 1);
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
} // namespace filesystem
|
||||
} // namespace cpp17
|
||||
|
@ -1,81 +1,87 @@
|
||||
/* Copyright (c) 2016, Pollard Banknote Limited
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "current_path.h"
|
||||
|
||||
#include <cerrno>
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#include <direct.h>
|
||||
#endif
|
||||
|
||||
namespace cpp17
|
||||
{
|
||||
namespace filesystem
|
||||
{
|
||||
path current_path()
|
||||
{
|
||||
char buf[4096];
|
||||
namespace filesystem
|
||||
{
|
||||
path
|
||||
current_path()
|
||||
{
|
||||
char buf[4096];
|
||||
|
||||
if ( ::getcwd( buf, sizeof( buf ) ) )
|
||||
{
|
||||
return buf;
|
||||
}
|
||||
if(::getcwd(buf, sizeof(buf)))
|
||||
{
|
||||
return buf;
|
||||
}
|
||||
|
||||
// Dynamically allocate larger buffers until cwd fits
|
||||
std::size_t size = 2 * sizeof( buf );
|
||||
// Dynamically allocate larger buffers until cwd fits
|
||||
std::size_t size = 2 * sizeof(buf);
|
||||
|
||||
while ( true )
|
||||
{
|
||||
char* q = new char[size];
|
||||
while(true)
|
||||
{
|
||||
char* q = new char[size];
|
||||
|
||||
if ( ::getcwd(q, size) )
|
||||
{
|
||||
path p = q;
|
||||
delete[] q;
|
||||
if(::getcwd(q, size))
|
||||
{
|
||||
path p = q;
|
||||
delete[] q;
|
||||
|
||||
return p;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( errno == ERANGE )
|
||||
{
|
||||
delete[] q;
|
||||
size *= 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
delete[] q;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(errno == ERANGE)
|
||||
{
|
||||
delete[] q;
|
||||
size *= 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
delete[] q;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return path();
|
||||
}
|
||||
return path();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} // namespace filesystem
|
||||
} // namespace cpp17
|
||||
|
@ -1,281 +1,404 @@
|
||||
/* Copyright (c) 2015, Pollard Banknote Limited
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation and/or
|
||||
other materials provided with the distribution.
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors
|
||||
may be used to endorse or promote products derived from this software without
|
||||
specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "filestatus.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "path.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
::cpp17::filesystem::file_status from_mode_t(mode_t m)
|
||||
{
|
||||
::cpp17::filesystem::perms p = static_cast< ::cpp17::filesystem::perms >( m & 0xFFF );
|
||||
|
||||
::cpp17::filesystem::file_type t = file_type::unknown;
|
||||
|
||||
if ( S_ISREG(m) )
|
||||
{
|
||||
t = file_type::regular;
|
||||
}
|
||||
else if ( S_ISDIR(m) )
|
||||
{
|
||||
t = file_type::directory;
|
||||
}
|
||||
else if ( S_ISCHR(m) )
|
||||
{
|
||||
t = file_type::character;
|
||||
}
|
||||
else if ( S_ISBLK(m) )
|
||||
{
|
||||
t = file_type::block;
|
||||
}
|
||||
else if ( S_ISFIFO(m) )
|
||||
{
|
||||
t = file_type::fifo;
|
||||
}
|
||||
#ifndef _WIN32 // these only work on cygnus or msys2!
|
||||
else if(S_ISLNK(m))
|
||||
{
|
||||
t = file_type::symlink;
|
||||
}
|
||||
else if ( S_ISSOCK(m) )
|
||||
{
|
||||
t = file_type::socket;
|
||||
}
|
||||
#endif
|
||||
return ::cpp17::filesystem::file_status(t, p);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace cpp17
|
||||
{
|
||||
namespace filesystem
|
||||
{
|
||||
file_status::file_status(const file_status& s)
|
||||
: t(s.t), p(s.p)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
file_status::file_status(
|
||||
file_type t_,
|
||||
perms p_
|
||||
)
|
||||
: t(t_), p(p_)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
file_status& file_status::operator=(const file_status& s)
|
||||
{
|
||||
t = s.t;
|
||||
p = s.p;
|
||||
#ifdef _MSC_VER
|
||||
#include <Windows.h>
|
||||
#include <stdint.h>
|
||||
#include <io.h>
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
file_type file_status::type() const
|
||||
{
|
||||
return t;
|
||||
}
|
||||
typedef unsigned short mode_t;
|
||||
typedef uint32_t id_t; /* Internal uids/gids are 32-bits */
|
||||
|
||||
void file_status::type(file_type t_)
|
||||
{
|
||||
t = t_;
|
||||
}
|
||||
|
||||
perms file_status::permissions() const
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
void file_status::permissions(perms p_)
|
||||
{
|
||||
p = p_;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(
|
||||
std::ostream& os,
|
||||
const file_status& fs
|
||||
)
|
||||
{
|
||||
os << fs.type() << "; " << fs.permissions();
|
||||
typedef SSIZE_T ssize_t;
|
||||
#ifndef _OFF_T_DEFINED
|
||||
typedef DWORD64 off_t;
|
||||
#endif
|
||||
typedef uint32_t uid_t; /* [???] user IDs */
|
||||
|
||||
return os;
|
||||
}
|
||||
#ifndef S_IFIFO
|
||||
#define S_IFIFO 0010000 /* [XSI] named pipe (fifo) */
|
||||
#endif
|
||||
|
||||
file_status status(const path& path_)
|
||||
{
|
||||
if ( !path_.empty() )
|
||||
{
|
||||
struct stat st;
|
||||
#ifndef S_IFBLK
|
||||
#define S_IFBLK 0060000 /* [XSI] block special */
|
||||
#endif
|
||||
|
||||
if ( ::stat(path_.c_str(), &st) == 0 )
|
||||
{
|
||||
return from_mode_t(st.st_mode);
|
||||
}
|
||||
}
|
||||
#ifndef S_IFLNK
|
||||
#define S_IFLNK 0120000 /* [XSI] symbolic link */
|
||||
#endif
|
||||
|
||||
return file_status();
|
||||
}
|
||||
#ifndef S_IFSOCK
|
||||
#define S_IFSOCK 0140000 /* [XSI] socket */
|
||||
#endif
|
||||
|
||||
file_status symlink_status(const path& path_)
|
||||
{
|
||||
if ( !path_.empty() )
|
||||
{
|
||||
struct stat st;
|
||||
#ifndef _WIN32
|
||||
if(::lstat(path_.c_str(), &st) == 0)
|
||||
#else
|
||||
if(::stat(path_.c_str(), &st) == 0)
|
||||
#ifndef S_ISBLK
|
||||
#define S_ISBLK(m) (((m)&S_IFMT) == S_IFBLK) /* block special */
|
||||
#endif
|
||||
{
|
||||
return from_mode_t(st.st_mode);
|
||||
}
|
||||
}
|
||||
return file_status();
|
||||
}
|
||||
|
||||
bool status_known(file_status s)
|
||||
{
|
||||
return s.type() != file_type::none;
|
||||
}
|
||||
|
||||
bool exists(file_status s)
|
||||
{
|
||||
return status_known(s) && s.type() != file_type::not_found;
|
||||
}
|
||||
#ifndef S_ISCHR
|
||||
#define S_ISCHR(m) (((m)&S_IFMT) == S_IFCHR) /* char special */
|
||||
#endif
|
||||
|
||||
bool exists(const path& p, std::error_code& ec)
|
||||
{
|
||||
return exists( status(p) );
|
||||
}
|
||||
#ifndef S_ISDIR
|
||||
#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR) /* directory */
|
||||
#endif
|
||||
|
||||
bool is_block_file(file_status s)
|
||||
{
|
||||
return s.type() == file_type::block;
|
||||
}
|
||||
#ifndef S_ISFIFO
|
||||
#define S_ISFIFO(m) (((m)&S_IFMT) == S_IFIFO) /* fifo or socket */
|
||||
#endif
|
||||
|
||||
bool is_block_file(const path& p)
|
||||
{
|
||||
return is_block_file( status(p) );
|
||||
}
|
||||
#ifndef S_ISREG
|
||||
#define S_ISREG(m) (((m)&S_IFMT) == S_IFREG) /* regular file */
|
||||
#endif
|
||||
|
||||
bool is_character_file(file_status s)
|
||||
{
|
||||
return s.type() == file_type::character;
|
||||
}
|
||||
#ifndef S_ISLNK
|
||||
#define S_ISLNK(m) (((m)&S_IFMT) == S_IFLNK) /* symbolic link */
|
||||
#endif
|
||||
|
||||
bool is_character_file(const path& p)
|
||||
{
|
||||
return is_character_file( status(p) );
|
||||
}
|
||||
#ifndef S_ISSOCK
|
||||
#define S_ISSOCK(m) (((m)&S_IFMT) == S_IFSOCK) /* socket */
|
||||
#endif
|
||||
|
||||
bool is_fifo(file_status s)
|
||||
{
|
||||
return s.type() == file_type::fifo;
|
||||
}
|
||||
#ifndef makedev
|
||||
#define makedev(x, y) ((dev_t)(((x) << 24) | (y)))
|
||||
#endif
|
||||
|
||||
bool is_fifo(const path& p)
|
||||
{
|
||||
return is_fifo( status(p) );
|
||||
}
|
||||
#ifndef DT_UNKNOWN
|
||||
#define DT_UNKNOWN 0
|
||||
#endif
|
||||
|
||||
bool is_other(file_status s)
|
||||
{
|
||||
return exists(s) && !is_regular_file(s) && !is_directory(s) && !is_symlink(s);
|
||||
}
|
||||
#ifndef DT_FIFO
|
||||
#define DT_FIFO 1
|
||||
#endif
|
||||
|
||||
bool is_other(const path& p)
|
||||
{
|
||||
return is_other( status(p) );
|
||||
}
|
||||
#ifndef DT_CHR
|
||||
#define DT_CHR 2
|
||||
#endif
|
||||
|
||||
bool is_regular_file(file_status s)
|
||||
{
|
||||
return s.type() == file_type::regular;
|
||||
}
|
||||
#ifndef DT_DIR
|
||||
#define DT_DIR 4
|
||||
#endif
|
||||
|
||||
bool is_regular_file(const path& p)
|
||||
{
|
||||
return is_regular_file( status(p) );
|
||||
}
|
||||
#ifndef DT_BLK
|
||||
#define DT_BLK 6
|
||||
#endif
|
||||
|
||||
bool is_socket(file_status s)
|
||||
{
|
||||
return s.type() == file_type::socket;
|
||||
}
|
||||
#ifndef DT_REG
|
||||
#define DT_REG 8
|
||||
#endif
|
||||
|
||||
bool is_socket(const path& p)
|
||||
{
|
||||
return is_socket( status(p) );
|
||||
}
|
||||
#ifndef DT_LNK
|
||||
#define DT_LNK 10
|
||||
#endif
|
||||
|
||||
bool is_symlink(file_status s)
|
||||
{
|
||||
return s.type() == file_type::symlink;
|
||||
}
|
||||
#ifndef DT_SOCK
|
||||
#define DT_SOCK 12
|
||||
#endif
|
||||
|
||||
bool is_symlink(const path& p)
|
||||
{
|
||||
return is_symlink( status(p) );
|
||||
}
|
||||
#ifndef DT_WHT
|
||||
#define DT_WHT 14
|
||||
#endif
|
||||
#endif
|
||||
|
||||
bool is_directory(file_status s)
|
||||
namespace
|
||||
{
|
||||
return s.type() == file_type::directory;
|
||||
}
|
||||
::cpp17::filesystem::file_status
|
||||
from_mode_t(mode_t m)
|
||||
{
|
||||
::cpp17::filesystem::perms p =
|
||||
static_cast<::cpp17::filesystem::perms >(m & 0xFFF);
|
||||
|
||||
::cpp17::filesystem::file_type t = file_type::unknown;
|
||||
|
||||
if(S_ISREG(m))
|
||||
{
|
||||
t = file_type::regular;
|
||||
}
|
||||
else if(S_ISDIR(m))
|
||||
{
|
||||
t = file_type::directory;
|
||||
}
|
||||
else if(S_ISCHR(m))
|
||||
{
|
||||
t = file_type::character;
|
||||
}
|
||||
else if(S_ISBLK(m))
|
||||
{
|
||||
t = file_type::block;
|
||||
}
|
||||
else if(S_ISFIFO(m))
|
||||
{
|
||||
t = file_type::fifo;
|
||||
}
|
||||
#ifndef _WIN32 // these only work on cygnus or msys2!
|
||||
else if(S_ISLNK(m))
|
||||
{
|
||||
t = file_type::symlink;
|
||||
}
|
||||
else if(S_ISSOCK(m))
|
||||
{
|
||||
t = file_type::socket;
|
||||
}
|
||||
#endif
|
||||
return ::cpp17::filesystem::file_status(t, p);
|
||||
}
|
||||
|
||||
bool is_directory(const path& p)
|
||||
{
|
||||
return is_directory( status(p) );
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::size_t file_size(const path& p)
|
||||
namespace cpp17
|
||||
{
|
||||
if ( !p.empty() )
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if ( ::stat(p.c_str(), &st) == 0 )
|
||||
{
|
||||
return st.st_size;
|
||||
}
|
||||
}
|
||||
|
||||
return std::size_t(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace filesystem
|
||||
{
|
||||
file_status::file_status(const file_status& s) : t(s.t), p(s.p)
|
||||
{
|
||||
}
|
||||
|
||||
file_status::file_status(file_type t_, perms p_) : t(t_), p(p_)
|
||||
{
|
||||
}
|
||||
|
||||
file_status&
|
||||
file_status::operator=(const file_status& s)
|
||||
{
|
||||
t = s.t;
|
||||
p = s.p;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
file_type
|
||||
file_status::type() const
|
||||
{
|
||||
return t;
|
||||
}
|
||||
|
||||
void
|
||||
file_status::type(file_type t_)
|
||||
{
|
||||
t = t_;
|
||||
}
|
||||
|
||||
perms
|
||||
file_status::permissions() const
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
void
|
||||
file_status::permissions(perms p_)
|
||||
{
|
||||
p = p_;
|
||||
}
|
||||
|
||||
std::ostream&
|
||||
operator<<(std::ostream& os, const file_status& fs)
|
||||
{
|
||||
os << fs.type() << "; " << fs.permissions();
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
file_status
|
||||
status(const path& path_)
|
||||
{
|
||||
if(!path_.empty())
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if(::stat(path_.c_str(), &st) == 0)
|
||||
{
|
||||
return from_mode_t(st.st_mode);
|
||||
}
|
||||
}
|
||||
|
||||
return file_status();
|
||||
}
|
||||
|
||||
file_status
|
||||
symlink_status(const path& path_)
|
||||
{
|
||||
if(!path_.empty())
|
||||
{
|
||||
struct stat st;
|
||||
#ifndef _WIN32
|
||||
if(::lstat(path_.c_str(), &st) == 0)
|
||||
#else
|
||||
if(::stat(path_.c_str(), &st) == 0)
|
||||
#endif
|
||||
{
|
||||
return from_mode_t(st.st_mode);
|
||||
}
|
||||
}
|
||||
return file_status();
|
||||
}
|
||||
|
||||
bool
|
||||
status_known(file_status s)
|
||||
{
|
||||
return s.type() != file_type::none;
|
||||
}
|
||||
|
||||
bool
|
||||
exists(file_status s)
|
||||
{
|
||||
return status_known(s) && s.type() != file_type::not_found;
|
||||
}
|
||||
|
||||
bool
|
||||
exists(const path& p, std::error_code& ec)
|
||||
{
|
||||
return exists(status(p));
|
||||
}
|
||||
|
||||
bool
|
||||
is_block_file(file_status s)
|
||||
{
|
||||
return s.type() == file_type::block;
|
||||
}
|
||||
|
||||
bool
|
||||
is_block_file(const path& p)
|
||||
{
|
||||
return is_block_file(status(p));
|
||||
}
|
||||
|
||||
bool
|
||||
is_character_file(file_status s)
|
||||
{
|
||||
return s.type() == file_type::character;
|
||||
}
|
||||
|
||||
bool
|
||||
is_character_file(const path& p)
|
||||
{
|
||||
return is_character_file(status(p));
|
||||
}
|
||||
|
||||
bool
|
||||
is_fifo(file_status s)
|
||||
{
|
||||
return s.type() == file_type::fifo;
|
||||
}
|
||||
|
||||
bool
|
||||
is_fifo(const path& p)
|
||||
{
|
||||
return is_fifo(status(p));
|
||||
}
|
||||
|
||||
bool
|
||||
is_other(file_status s)
|
||||
{
|
||||
return exists(s) && !is_regular_file(s) && !is_directory(s)
|
||||
&& !is_symlink(s);
|
||||
}
|
||||
|
||||
bool
|
||||
is_other(const path& p)
|
||||
{
|
||||
return is_other(status(p));
|
||||
}
|
||||
|
||||
bool
|
||||
is_regular_file(file_status s)
|
||||
{
|
||||
return s.type() == file_type::regular;
|
||||
}
|
||||
|
||||
bool
|
||||
is_regular_file(const path& p)
|
||||
{
|
||||
return is_regular_file(status(p));
|
||||
}
|
||||
|
||||
bool
|
||||
is_socket(file_status s)
|
||||
{
|
||||
return s.type() == file_type::socket;
|
||||
}
|
||||
|
||||
bool
|
||||
is_socket(const path& p)
|
||||
{
|
||||
return is_socket(status(p));
|
||||
}
|
||||
|
||||
bool
|
||||
is_symlink(file_status s)
|
||||
{
|
||||
return s.type() == file_type::symlink;
|
||||
}
|
||||
|
||||
bool
|
||||
is_symlink(const path& p)
|
||||
{
|
||||
return is_symlink(status(p));
|
||||
}
|
||||
|
||||
bool
|
||||
is_directory(file_status s)
|
||||
{
|
||||
return s.type() == file_type::directory;
|
||||
}
|
||||
|
||||
bool
|
||||
is_directory(const path& p)
|
||||
{
|
||||
return is_directory(status(p));
|
||||
}
|
||||
|
||||
std::size_t
|
||||
file_size(const path& p)
|
||||
{
|
||||
if(!p.empty())
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
if(::stat(p.c_str(), &st) == 0)
|
||||
{
|
||||
return st.st_size;
|
||||
}
|
||||
}
|
||||
|
||||
return std::size_t(-1);
|
||||
}
|
||||
} // namespace filesystem
|
||||
} // namespace cpp17
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue