Example #1
0
// Move or copy the file, reutnr weather it has been moved ot copied
void moveOrCopy(const fs::path &source, const fs::path &destination) {
	// First try a simple rename.
	boost::system::error_code ec;
	fs::rename(source, destination, ec);
	if(!ec) {
		return;
	}
#if 0
	fs::copy_file(source, destination, fs::copy_option::fail_if_exists);
#else
	struct stat st;
	if(::lstat(source.string().c_str(), &st) != 0) {
		throw SyscallException(errno, "lstat", source.string());
	}
	// Load source into memory
	MMap md(source.string());
	md.map();
	// Open temorary file near destination (we assume that
	// rename(tmpFile, destination) will succeed.
	const std::string tmpFile = destination.string() + ".tmp";
	FILE *f = fopen(tmpFile.c_str(), "w");
	if(f == NULL) {
		throw SyscallException(errno, "fopen", tmpFile);
	}
	try { // try to assure fclose is called!
		if(chmod(tmpFile.c_str(), st.st_mode & 0777) != 0) {
			std::cerr << SyscallException(errno, "chmod", tmpFile).what() << std::endl;
		}
		// copy the data
		if(fwrite(md.map(), md.len(), 1, f) != 1) {
			throw SyscallException(errno, "fwrite", source.string(), tmpFile);
		}
		fclose(f);
		f = NULL; // Make sure the file is not "closed" again in catch
		fs::rename(tmpFile, destination);
	} catch(...) {
		// Make sure f is closed and
		if(f != NULL) {
			fclose(f);
		}
		// remove the temporary file. Note: we do not check if the file
		// still exists, if it doesn't this will silently fail (no exception!).
		fs::remove(tmpFile);
		throw;
	}
#endif
	fs::remove(source);
}
Example #2
0
void TorrentFile::init(const std::string & path, bool files_should_exists, uint32_t & files_exists)
{
	if (path == "" || path[0] != '/')
		throw Exception(Exception::ERR_CODE_UNDEF);

	std::string i_path(path);
	if (i_path[i_path.length() - 1] != '/')
		i_path.append("/");

	uint32_t files_count = m_torrent->get_files_count();

	if (files_count > 1)
	{
		m_torrent->get_dirtree().make_dir_tree(i_path);
		i_path.append(m_torrent->get_name() + "/");
	}
	files_exists = 0;
	for(uint32_t i = 0; i < files_count; i++)
	{
		info::file_stat * fi = m_torrent->get_file_info(i);
		file f;
		f.length = fi->length;
		f.name = i_path + fi->path;
		f.download = true;
		f.priority = DOWNLOAD_PRIORITY_NORMAL;
		f.downloaded = 0;
		try
		{
			if (m_fm->File_exists(f.name, f.length))
				files_exists++;
			m_fm->File_add(f.name, f.length, false, shared_from_this(), f.file_);
		}
		catch(Exception & e)
		{
			ReleaseFiles();
			files_exists = 0;
			throw Exception(Exception::ERR_CODE_UNDEF);
		}
		catch (SyscallException & e)
		{
			ReleaseFiles();
			files_exists = 0;
			throw SyscallException(e);
		}
		catch(...)
		{
			ReleaseFiles();
			files_exists = 0;
			throw Exception(Exception::ERR_CODE_UNDEF);
		}
		m_files.push_back(f);
	}
}
Example #3
0
SignalHandler<SIGNUM>::~SignalHandler()
{
  if (signal(SIGNUM, SIG_DFL) == SIG_ERR)
    throw SyscallException("Error stopping handling signal : ");
}
Example #4
0
SignalHandler<SIGNUM>::SignalHandler()
{
  s_handler = this;
  if (signal(SIGNUM, &(handler<SIGNUM>)) == SIG_ERR)
    throw SyscallException("Error handling signal : ");
}
Example #5
0
static void	handler(int signum __attribute__ ((unused)))
{
  if (signal(SIGNUM, SIG_DFL) == SIG_ERR)
    throw SyscallException("Error stopping handling signal when this signal was caught : ");
  SignalHandler<SIGNUM>::s_handler->onCatch();
}