1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
// Copyright 2014 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.
#include <cerrno>
#include <cstring>
#include <iostream>
#include <string>
#include <sstream>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
using std::endl;
//============================================================================
// :: Helpers
namespace
{
//--------------------------------------------------------------------------
// Helper to create an empty file with the given path.
void touch(const std::string& path, const mode_t mode)
{
std::cout
<< "Touching file: " << path << " with mode=" << std::oct << mode
<< std::dec << endl;
const int fd = ::open(path.c_str(), O_CREAT | O_WRONLY, mode);
if (fd == -1) {
const int error = errno;
std::cout
<< "Failed to touch file using open: " << path << "; errno=" << error
<< ";" << std::strerror(error) << endl;
}
else {
::close(fd);
}
}
//--------------------------------------------------------------------------
// Stats the given path and prints the mode. Returns true if the path
// exists; false otherwise.
bool exists(const std::string& path)
{
struct ::stat path_stat;
if (::lstat(path.c_str(), &path_stat) != 0) {
const int error = errno;
if (error == ENOENT) {
// Only bother logging if something other than the path not existing
// went wrong.
std::cout
<< "Failed to lstat path: " << path << "; errno=" << error << "; "
<< std::strerror(error) << endl;
}
return false;
}
std::cout
<< std::oct << "Mode for path=" << path << ": " << path_stat.st_mode
<< std::dec << endl;
return true;
}
}
//============================================================================
// :: Entry Point
int main()
{
touch("file1", 0667);
if (not exists("file1")) {
std::cout << "Failed to create path: file1" << endl;
return 1;
}
if (exists("file1/dir")) {
std::cout << "Path should not exists: file1/dir" << endl;
return 1;
}
touch("file2", 0676);
if (not exists("file2")) {
std::cout << "Failed to create path: file2" << endl;
return 1;
}
if (exists("file2/dir")) {
std::cout << "Path should not exists: file2/dir" << endl;
return 1;
}
touch("file3", 0766);
if (not exists("file3")) {
std::cout << "Failed to create path: file3" << endl;
return 1;
}
if (exists("file3/dir")) {
std::cout << "Path should not exists: file3/dir" << endl;
return 1;
}
std::cout << "ok." << endl;
return 0;
}
|