예제 #1
0
void __create_symlink(path const & from, path const & to, std::error_code *ec) {

    if (::symlink(from.c_str(), to.c_str()) == -1)
        set_or_throw(ec, "create_symlink", from, to);
    else if (ec)
        ec->clear();
}
예제 #2
0
ConfigParser::ConfigParser(const path & configFile) :
	m_configFilePath(configFile)
{
	ifstream fin(configFile.c_str());
	string configBuffer;
	string buff;

	if (fin.is_open())
	{
		while (fin.good())
		{
			getline(fin, buff);

			// if this isn't just an empty line or a comment
			if (buff[0] != '\0' && buff[0] != '#')
				configBuffer += buff + '\n';
		}
	}
	else
	{
		string errMsg = "Cannot read configuration file \"";
		errMsg += configFile.c_str();
		errMsg += '\"';
		LogManager::getInstancePtr()->log(errMsg, LogManager::L_ERROR);
		throw FSException(errMsg, __FILE__, __LINE__);
	}

	fin.close();

	m_fsyncHomePath = getFsyncHomePath();
	generatePairs(configBuffer);
}
예제 #3
0
//------------------------------------------------------------------------------
void FileCopier::copy (path const& srcpath, path const& dstpath, path const& dsppath) {
   // declare variables
   long unsigned buf_count = 0;
   long unsigned buf_trigger = bufs_per_update;
   FileSize initialBytes = status.bytes;

   // update status
   status.fileTotal = file_size(srcpath);
   status.srcPath = srcpath;
   status.dstPath = dstpath;
   status.dspPath = dsppath;

   // open files
   ifstream src(srcpath.c_str());
   ofstream dst;
   if (!safe_mode) dst.open(dstpath.c_str(), ios_base::out | ios_base::binary);

   printStart(status);
   while (src) {
      if (buf_count++ == buf_trigger) {
         buf_trigger += bufs_per_update;
         FileSize newbytes = bufs_per_update * BUFSIZ;
         status.bytes += newbytes;
         status.fileBytes += newbytes;
         printUpdate(status);
      }

      src.read(buf, BUFSIZ);
      if (!safe_mode) dst.write(buf, src.gcount());
   }

   src.close();
   if (!safe_mode) dst.close();
   status.bytes = initialBytes + status.fileTotal;
}
예제 #4
0
 void rename(const path& from, const path& to, std::error_code& ec) noexcept
 {
   if (::rename(from.c_str(), to.c_str())) {
     ec = {errno, std::system_category()};
   } else {
     ec.clear();
   }
 }
예제 #5
0
void __last_write_time(const path& p, file_time_type new_time,
                       std::error_code *ec)
{
    using namespace std::chrono;
    std::error_code m_ec;

    // We can use the presence of UTIME_OMIT to detect platforms that do not
    // provide utimensat.
#if !defined(UTIME_OMIT)
    // This implementation has a race condition between determining the
    // last access time and attempting to set it to the same value using
    // ::utimes
    struct ::stat st;
    file_status fst = detail::posix_stat(p, st, &m_ec);
    if (m_ec && !status_known(fst)) {
        set_or_throw(m_ec, ec, "last_write_time", p);
        return;
    }
    struct ::timeval tbuf[2];
    tbuf[0].tv_sec = st.st_atime;
    tbuf[0].tv_usec = 0;
    const bool overflowed = !detail::set_times_checked<microseconds>(
        &tbuf[1].tv_sec, &tbuf[1].tv_usec, new_time);

    if (overflowed) {
        set_or_throw(make_error_code(errc::invalid_argument), ec,
                     "last_write_time", p);
        return;
    }
    if (::utimes(p.c_str(), tbuf) == -1) {
        m_ec = detail::capture_errno();
    }
#else
    struct ::timespec tbuf[2];
    tbuf[0].tv_sec = 0;
    tbuf[0].tv_nsec = UTIME_OMIT;

    const bool overflowed = !detail::set_times_checked<nanoseconds>(
        &tbuf[1].tv_sec, &tbuf[1].tv_nsec, new_time);
    if (overflowed) {
        set_or_throw(make_error_code(errc::invalid_argument),
            ec, "last_write_time", p);
        return;
    }
    if (::utimensat(AT_FDCWD, p.c_str(), tbuf, 0) == -1) {
        m_ec = detail::capture_errno();
    }
#endif
    if (m_ec)
        set_or_throw(m_ec, ec, "last_write_time", p);
    else if (ec)
        ec->clear();
}
예제 #6
0
//------------------------------------------------------------------------------
void rename(const path &op, const path &np)
{
    const int r = ::rename(op.c_str(), np.c_str());
    if(r != 0)
    {
        n_throw(system::system_error)
            << ei_msg_c("rename() failed.")
            << ei_path(op)
            << system::ei_error_code(system::error_code(
                errno, system::system_category()));
    }
}
예제 #7
0
 bool equivalent(const path& p1, const path& p2, std::error_code& ec) noexcept
 {
   struct stat st1;
   struct stat st2;
   if (::stat(p1.c_str(), &st1) || ::stat(p2.c_str(), &st2)) {
     ec = {errno, std::system_category()};
     return false;
   } else {
     ec.clear();
     return st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino;
   }
 }
예제 #8
0
void __last_write_time(const path& p, file_time_type new_time,
                       std::error_code *ec)
{
    using namespace std::chrono;
    std::error_code m_ec;

#if !defined(_LIBCXX_USE_UTIMENSAT)
    // This implementation has a race condition between determining the
    // last access time and attempting to set it to the same value using
    // ::utimes
    struct ::stat st;
    file_status fst = detail::posix_stat(p, st, &m_ec);
    if (m_ec && !status_known(fst)) {
        set_or_throw(m_ec, ec, "last_write_time", p);
        return;
    }
    auto atime = detail::extract_atime(st);
    struct ::timeval tbuf[2];
    tbuf[0].tv_sec = atime.tv_sec;
    tbuf[0].tv_usec = duration_cast<microseconds>(nanoseconds(atime.tv_nsec)).count();
    const bool overflowed = !FSTime::set_times_checked<microseconds>(
        &tbuf[1].tv_sec, &tbuf[1].tv_usec, new_time);

    if (overflowed) {
        set_or_throw(make_error_code(errc::invalid_argument), ec,
                     "last_write_time", p);
        return;
    }
    if (::utimes(p.c_str(), tbuf) == -1) {
        m_ec = detail::capture_errno();
    }
#else
    struct ::timespec tbuf[2];
    tbuf[0].tv_sec = 0;
    tbuf[0].tv_nsec = UTIME_OMIT;

    const bool overflowed = !FSTime::set_times_checked<nanoseconds>(
        &tbuf[1].tv_sec, &tbuf[1].tv_nsec, new_time);
    if (overflowed) {
        set_or_throw(make_error_code(errc::invalid_argument),
            ec, "last_write_time", p);
        return;
    }
    if (::utimensat(AT_FDCWD, p.c_str(), tbuf, 0) == -1) {
        m_ec = detail::capture_errno();
    }
#endif
    if (m_ec)
        set_or_throw(m_ec, ec, "last_write_time", p);
    else if (ec)
        ec->clear();
}
예제 #9
0
bool filesys::create_directory(const path & p)
{
    /*
    */
#	if OS_WINDOWS
    return CreateDirectory(p.c_str(), 0) != 0;
#	elif OS_LINUX
    int32 stat = mkdir(p.c_str(), 777);
    return stat != 0;
#   else
#	endif
    return false;
}
예제 #10
0
filesys::DirectoryIterator::DirectoryIterator(const path & p, const path& filter /* = "*.*"*/) :
    hdl(nullptr),
    i(0), n(0),
    current(""), basePath("")
{
    /*
    WIN32_FIND_DATA ffd;
    path p2 = filesys::concat(p, filter);
    basePath = p;
    hdl = FindFirstFile(p2.c_str(), &ffd);
    if (hdl == INVALID_HANDLE_VALUE)
    	hdl = nullptr;
    current = filesys::concat(basePath,path(ffd.cFileName));
    if (path(ffd.cFileName) == "." || path(ffd.cFileName) == "..")
    	operator ++();
    */
#	if OS_WINDOWS || OS_LINUX
    basePath = p;
    i = 0;
    struct dirent **nameList = nullptr;
    n = scandir(p.c_str(),&nameList,filter_dirEntry, alphasort);
    if(n <= 0) {
        hdl = nullptr;
        n = 0;
        return;
    }
    hdl = (void*)nameList;
    current = filesys::concat(basePath,filesys::path(nameList[i]->d_name));
    free(nameList[i++]);
#   else
#	endif
}
예제 #11
0
HashTable<string,std::vector<string> > read_configuration_file(const path cfg_path)
{
    assert(is_regular_file(cfg_path));
    auto hash_table=HashTable<std::string,std::vector<std::string>>();
    std::ifstream cfg_file(cfg_path.c_str());
    std::string line;
    std::vector<std::string> parts;
    while (  std::getline(cfg_file,line) )
    {
        split( parts,line,boost::is_any_of("=") );
        switch (parts.size())
        {
         case 0 : 
             throw std::string("Malformed configuration file : "+line); 
             break;
         case 1 :       // probably empty line
             break;
         case 2 :
             std::string key=parts[0];
             std::string value=parts[1];
            if (!hash_table.isKey(key))
            {
                hash_table[key]=std::vector<std::string>();
            }
            hash_table[key].push_back(value);
            break;
        }
    }
    return hash_table;
}
예제 #12
0
 OPENPSTD_SHARED_EXPORT void PSTDFileAccess::Open(path filename)
 {
     boost::unique_lock<boost::recursive_mutex> m_lock(*this->mutex);
     this->realPath = path(filename.c_str());
     this->d = PSTDFile::Open(this->realPath);
     this->Change();
 }
예제 #13
0
        /**
         *  Finds a particular fasta header in a fasta file and returns the associated sequence
         **/
        static int getEntryFromFasta(const path& fasta_path, const string& header, string& sequence)
        {
            // Setup stream to sequence file and check all is well
            std::ifstream inputStream (fasta_path.c_str());

            if (!inputStream.is_open())
            {
                std::cerr << "ERROR: Could not open the sequence file: " << fasta_path << endl;
                return 0;
            }

            string id;
            string seq;

            // Read a record
            while(inputStream.good() && readRecord(inputStream, id, seq) == 0)
            {
                stringstream ssHeader;
                ssHeader << id;

                if (header.compare(ssHeader.str()) == 0)
                {
                    inputStream.close();
                    sequence = seq;

                    return 0;
                }
                seq.clear();
            }

            inputStream.close();
            return -1;
        }
예제 #14
0
        /**
         *  Finds the nth entry from the fasta file and returns the associated sequence
         **/
        static int getEntryFromFasta(const path& fasta_path, uint32_t n, string& header, string& sequence)
        {
            // Setup stream to sequence file and check all is well
            std::ifstream inputStream (fasta_path.c_str());

            if (!inputStream.is_open())
            {
                std::cerr << "ERROR: Could not open the sequence file: " << fasta_path << endl;
                return 0;
            }


            std::string id;
            std::string seq;
            uint32_t i = 1;

            // Read a record
            while(inputStream.good() && readRecord(inputStream, id, seq) == 0)
            {
                if (i == n)
                {
                    inputStream.close();

                    header.swap(id);
                    sequence = seq;
                    return 0;
                }

                seq.clear();
                i++;
            }

            inputStream.close();
            return -1;
        }
예제 #15
0
path absolute(const path& filename)
{
	if ( !filename.empty() )
	{
		#if (( defined( _POSIX_VERSION ) && _POSIX_VERSION >= 200809l ) || defined( __GLIBC__ ))
		// Preferred - POSIX-2008 and glibc will allocate the path buffer
		char* res = ::realpath(filename.c_str(), NULL);

		if ( res )
		{
			path s = res;
			::free(res);

			return s;
		}

		#else
		#ifdef _GNU_SOURCE
		// Maybe we can rely on the GNU extension
		char* res = ::canonicalize_file_name( filename.c_str() );

		if ( res )
		{
			std::string s = res;
			::free(res);

			return s;
		}

		#elif ((( defined( _POSIX_VERSION ) && _POSIX_VERSION >= 200112L ) || ( defined( _XOPEN_VERSION ) && _XOPEN_VERSION >= 500 )) && defined( PATH_MAX ))
		/// @todo PATH_MAX may be huge or -1, according to man pages for realpath
		char  resolved[PATH_MAX + 1];
		char* res = ::realpath(filename.c_str(), resolved);

		if ( res )
		{
			return resolved;
		}

		#else
		#error "No way to get absolute file path!"
		#endif // if 1
		#endif // if ( defined( _POSIX_VERSION ) && _POSIX_VERSION >= 200809l )
	}

	return path();
}
예제 #16
0
 void resize_file(const path& p, uintmax_t size, std::error_code& ec) noexcept
 {
   if (::truncate(p.c_str(), static_cast<off_t>(size))) {
     ec = {errno, std::system_category()};
   } else {
     ec.clear();
   }
 }
예제 #17
0
bool __remove(const path& p, std::error_code *ec) {
    if (ec) ec->clear();
    if (::remove(p.c_str()) == -1) {
        set_or_throw(ec, "remove", p);
        return false;
    }
    return true;
}
예제 #18
0
 void current_path(const path& p, std::error_code& ec) noexcept
 {
   if (chdir(p.c_str()) == 0) {
     ec = {errno, std::system_category()};
   } else {
     ec.clear();
   }
 }
예제 #19
0
파일: fs.cpp 프로젝트: rcombs/Aegisub
std::string ShortName(path const& p) {
    std::wstring out(MAX_PATH + 1, 0);
    DWORD len = GetShortPathName(p.c_str(), &out[0], out.size());
    if (!len)
        return p.string();
    out.resize(len);
    return ConvertLocal(out);
}
예제 #20
0
bool filesys::exists(const path & p)
{
#	if OS_WINDOWS || OS_LINUX
    struct stat buffer;
    return stat(p.c_str(), &buffer) == 0;
#	else
    return false;
#	endif
}
예제 #21
0
파일: editor.cpp 프로젝트: CruzR/stk-editor
// ----------------------------------------------------------------------------
path Editor::toRelative(path p)
{
    stringc path = p.c_str();
    s32 ix = path.findLast('/');
    if (ix == -1) return p;
    stringc ret = path.subString(ix+1, path.size() - ix);
    return ret;

} // toRelative
예제 #22
0
/// Removes a file.
///
/// \param file The file to remove.
///
/// \throw fs::system_error If the call to unlink(2) fails.
void
fs::unlink(const path& file)
{
    if (::unlink(file.c_str()) == -1) {
        const int original_errno = errno;
        throw fs::system_error(F("Removal of %s failed") % file,
                               original_errno);
    }
}
예제 #23
0
bool __create_directory(const path& p, std::error_code *ec)
{
    if (ec) ec->clear();
    if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)
        return true;
    if (errno != EEXIST || !is_directory(p))
        set_or_throw(ec, "create_directory", p);
    return false;
}
예제 #24
0
 struct stat lstat(path p, std::error_code& ec)
 {
   struct stat st;
   if (::lstat(p.c_str(), &st) != 0) {
     ec = {errno, std::system_category()};
   } else {
     ec.clear();
   }
   return st;
 }
예제 #25
0
 bool create_directory(const path& p, std::error_code& ec) noexcept
 {
   auto error = mkdir(p.c_str(), static_cast<int>(perms::all));
   if (error && !is_directory(p)) {
     ec = {errno, std::system_category()};
   } else {
     ec.clear();
   }
   return !error;
 }
BackgroundImageContents::BackgroundImageContents(path filename, GError** error)
{
	XOJ_INIT_TYPE(BackgroundImageContents);

	this->filename = filename;
	this->ref = 1;
	this->pageId = -1;
	this->attach = false;
	this->pixbuf = gdk_pixbuf_new_from_file(filename.c_str(), error);
}
예제 #27
0
 path canonical(const path& p, std::error_code& ec)
 {
   char tmp[PATH_MAX];
   if (realpath(p.c_str(), tmp) == nullptr) {
     ec = {errno, std::system_category()};
   } else {
     ec.clear();
   }
   return path(tmp);
 }
예제 #28
0
	void directory_iterator::open_dir(const path &p, error_code &ec) noexcept {

		DIR *dp = opendir(p.c_str());
		if (!dp)
		{
				ec = error_code(errno, std::system_category());
				return;
		}

		_imp = std::make_shared<imp>(p, dp);
	}
예제 #29
0
 bool remove(const path& p, std::error_code& ec) noexcept
 {
   if (!exists(p, ec)) return false;
   if (::remove(p.c_str())) {
     ec = {errno, std::system_category()};
     return false;
   } else {
     ec.clear();
     return true;
   }
 }
예제 #30
0
void __permissions(const path& p, perms prms, std::error_code *ec)
{

    const bool resolve_symlinks = !bool(perms::symlink_nofollow & prms);
    const bool add_perms = bool(perms::add_perms & prms);
    const bool remove_perms = bool(perms::remove_perms & prms);
    _LIBCPP_ASSERT(!(add_perms && remove_perms),
                   "Both add_perms and remove_perms are set");

    bool set_sym_perms = false;
    prms &= perms::mask;
    if (!resolve_symlinks || (add_perms || remove_perms)) {
        std::error_code m_ec;
        file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec)
                                          : detail::posix_lstat(p, &m_ec);
        set_sym_perms = is_symlink(st);
        if (m_ec) return set_or_throw(m_ec, ec, "permissions", p);
        _LIBCPP_ASSERT(st.permissions() != perms::unknown,
                       "Permissions unexpectedly unknown");
        if (add_perms)
            prms |= st.permissions();
        else if (remove_perms)
           prms = st.permissions() & ~prms;
    }
    const auto real_perms = detail::posix_convert_perms(prms);

# if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
    const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0;
    if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) {
        return set_or_throw(ec, "permissions", p);
    }
# else
    if (set_sym_perms)
        return set_or_throw(make_error_code(errc::operation_not_supported),
                            ec, "permissions", p);
    if (::chmod(p.c_str(), real_perms) == -1) {
        return set_or_throw(ec, "permissions", p);
    }
# endif
    if (ec) ec->clear();
}