Beispiel #1
0
void FileImpl::handleLastErrorImpl(const std::string& path)
{
    switch (errno)
    {
    case EIO:
        throw IOException(path);
    case EPERM:
        throw FileAccessDeniedException("insufficient permissions", path);
    case EACCES:
        throw FileAccessDeniedException(path);
    case ENOENT:
        throw FileNotFoundException(path);
    case ENOTDIR:
        throw OpenFileException("not a directory", path);
    case EISDIR:
        throw OpenFileException("not a file", path);
    case EROFS:
        throw FileReadOnlyException(path);
    case EEXIST:
        throw FileExistsException(path);
    case ENOSPC:
        throw FileException("no space left on device", path);
    case ENOTEMPTY:
        throw FileException("directory not empty", path);
    case ENAMETOOLONG:
        throw PathSyntaxException(path);
    case ENFILE:
    case EMFILE:
        throw FileException("too many open files", path);
    default:
        throw FileException(std::strerror(errno), path);
    }
}
Beispiel #2
0
void BzipOutputStream::dopen(int fd, const char* mode)
{
	assert(0 == m_fp);

	int err;
#ifdef _MSC_VER
	m_cf = _fdopen(fd, mode);
#else
	m_cf = fdopen(fd, mode);
#endif
	if (0 == m_cf)
	{
		std::ostringstream oss;
		oss << "fd=" << fd << ", mode=" << mode;
		throw OpenFileException("<fd>", oss.str().c_str());
	}
	m_fp = BZ2_bzWriteOpen(&err, m_cf
		,  9   // blocksize100k
		,  0   // verbosity
		, 30   // workFactor, default=30
		);
	if (0 == m_fp)
	{
		std::ostringstream oss;
		oss << "fd=" << fd << ", mode=" << mode << ", err=" << strbzerr(err);
		throw OpenFileException("<fd>", oss.str().c_str());
	}
}
Beispiel #3
0
void BzipInputStream::dopen(int fd, const char* mode)
{
	assert(0 == m_fp);

	int err;
#ifdef _MSC_VER
	m_cf = _fdopen(fd, mode);
#else
	m_cf = fdopen(fd, mode);
#endif
	if (0 == m_cf)
	{
		std::ostringstream oss;
		oss << "fd=" << fd << ", mode=" << mode;
		throw OpenFileException("<fd>", oss.str().c_str());
	}
	m_fp = BZ2_bzReadOpen(&err, m_cf
		, 0    // verbosity, 0 will not print msg
		, 0    // small
		, NULL // unused
		, 0    // nUnused
		);
	if (0 == m_fp)
	{
		std::ostringstream oss;
		oss << "fd=" << fd << ", mode=" << mode << ", err=" << strbzerr(err);
		throw OpenFileException("<fd>", oss.str().c_str());
	}
}
Beispiel #4
0
void FileStream::ThrowOpenFileException(const char* fpath, const char* mode)
{
	std::string errMsg = strerror(errno);
	string_appender<> oss;
	oss << "mode=" << mode << ", errMsg: " << errMsg;
	throw OpenFileException(fpath, oss.str().c_str());
}
void LogFileImpl::createFile()
{
	std::wstring upath;
	FileImpl::convertPath(_path, upath);
	
	_hFile = CreateFileW(upath.c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (_hFile == INVALID_HANDLE_VALUE) throw OpenFileException(_path);
	SetFilePointer(_hFile, 0, 0, FILE_END);
	// There seems to be a strange "optimization" in the Windows NTFS
	// filesystem that causes it to reuse directory entries of deleted
	// files. Example:
	// 1. create a file named "test.dat"
	//    note the file's creation date
	// 2. delete the file "test.dat"
	// 3. wait a few seconds
	// 4. create a file named "test.dat"
	//    the new file will have the same creation
	//    date as the old one.
	// We work around this bug by taking the file's
	// modification date as a reference when the
	// file is empty.
	if (sizeImpl() == 0)
		_creationDate = File(_path).getLastModified();
	else
		_creationDate = File(_path).created();
}
void HTTPServerResponseImpl::sendFile(const std::string& path, const std::string& mediaType)
{
	poco_assert (!_pStream);

	File f(path);
	Timestamp dateTime    = f.getLastModified();
	File::FileSize length = f.getSize();
	set("Last-Modified", DateTimeFormatter::format(dateTime, DateTimeFormat::HTTP_FORMAT));
#if defined(POCO_HAVE_INT64)
	setContentLength64(length);
#else
	setContentLength(static_cast<int>(length));
#endif
	setContentType(mediaType);
	setChunkedTransferEncoding(false);

	Poco::FileInputStream istr(path);
	if (istr.good())
	{
		_pStream = new HTTPHeaderOutputStream(_session);
		write(*_pStream);
		if (_pRequest && _pRequest->getMethod() != HTTPRequest::HTTP_HEAD)
		{
			StreamCopier::copyStream(istr, *_pStream);
		}
	}
	else throw OpenFileException(path);
}
void listFiles(const Path& parent, std::list<Path> & list) throw (OpenFileException)
{
    INFO("Adding: %s", parent.fullPath.c_str());
    list.push_back(parent);

    if (!parent.isDir)
        return;

    INFO("Listing files for: %s", parent.fullPath.c_str());
    DIR* directory = 0;
    struct dirent* entry = 0;

    if ((directory = opendir(parent.fullPath.c_str())) == NULL)
    {
        WARN("%s", "Cant open the directory.");
        throw OpenFileException(parent.fullPath.c_str(), CAN_NOT_OPEN_INPUT_FILE);
    }

    while ((entry = readdir(directory)))
    {
        bool isNotCurrentOrParentDirectory = strncmp(entry->d_name, "..", 2) != 0 &&
                strncmp(entry->d_name, ".", 1) != 0;

        if (isNotCurrentOrParentDirectory)
        {
            bool isDir = entry->d_type == DT_DIR;
            Path path(parent, entry->d_name, isDir);
            listFiles(path, list);
        }
    }

    closedir(directory);
}
Beispiel #8
0
FilePartSource::FilePartSource(const std::string& path):
	_istr(path)
{
	Path p(path);
	_filename = p.getFileName();
	if (!_istr.good())
		throw OpenFileException(path);
}
Beispiel #9
0
LogFileImpl::LogFileImpl(const std::string& path): _path(path)
{
	_file = fopen(path.c_str(), "a", "ctx=rec,bin", "shr=get", "fop=dfw", "alq=500", "deq=500");
	if (!_file) throw OpenFileException(path);
	if (size() == 0)
		_creationDate = File(path).getLastModified();
	else
		_creationDate = File(path).created();
}
Beispiel #10
0
FilePartSource::FilePartSource(const std::string& path, const std::string& filename, const std::string& mediaType):
	PartSource(mediaType),
	_filename(filename),
	_istr(path)
{
	Path p(path);
	if (!_istr.good())
		throw OpenFileException(path);
}
Beispiel #11
0
WinFileStream::WinFileStream(
		LPCSTR szFile,
		DWORD dwDesiredAccess,
		DWORD dwShareMode,
		LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
	if (!open(szFile, dwDesiredAccess, dwShareMode, lpSecurityAttributes))
	{
		throw OpenFileException(szFile, ErrorText(GetLastError()).c_str());
	}
}
Beispiel #12
0
std::istream* FileStreamFactory::open(const Path& path)
{
	File file(path);
	if (!file.exists()) throw FileNotFoundException(path.toString());
	
	FileInputStream* istr = new FileInputStream(path.toString(), std::ios::binary);
	if (!istr->good())
	{
		delete istr;
		throw OpenFileException(path.toString());
	}	
	return istr;
}
Beispiel #13
0
void FileImpl::handleLastErrorImpl(const std::string& path)
{
	switch (errno)
	{
	case EIO:
		throw IOException(path, errno);
	case EPERM:
		throw FileAccessDeniedException("insufficient permissions", path, errno);
	case EACCES:
		throw FileAccessDeniedException(path, errno);
	case ENOENT:
		throw FileNotFoundException(path, errno);
	case ENOTDIR:
		throw OpenFileException("not a directory", path, errno);
	case EISDIR:
		throw OpenFileException("not a file", path, errno);
	case EROFS:
		throw FileReadOnlyException(path, errno);
	case EEXIST:
		throw FileExistsException(path, errno);
	case ENOSPC:
		throw FileException("no space left on device", path, errno);
	case EDQUOT:
		throw FileException("disk quota exceeded", path, errno);
#if !defined(_AIX)
	case ENOTEMPTY:
		throw DirectoryNotEmptyException(path, errno);
#endif
	case ENAMETOOLONG:
		throw PathSyntaxException(path, errno);
	case ENFILE:
	case EMFILE:
		throw FileException("too many open files", path, errno);
	default:
		throw FileException(Error::getMessage(errno), path, errno);
	}
}
Beispiel #14
0
// only can call on unopened BzipOutputStream
void BzipOutputStream::open(const char* fpath, const char* mode)
{
	assert(0 == m_fp);

	int err;
	m_cf = fopen(fpath, mode);
	if (0 == m_cf)
	{
		std::ostringstream oss;
		oss << "mode=" << mode;
		throw OpenFileException(fpath, oss.str().c_str());
	}
	m_fp = BZ2_bzWriteOpen(&err, m_cf
		,  9   // blocksize100k
		,  0   // verbosity
		, 30   // workFactor, default=30
		);
	if (0 == m_fp)
	{
		std::ostringstream oss;
		oss << "mode=" << mode << ", err=" << strbzerr(err);
		throw OpenFileException(fpath, oss.str().c_str());
	}
}
Beispiel #15
0
WinFileStream::WinFileStream(
		LPCSTR szFile,
		DWORD dwDesiredAccess,
		DWORD dwShareMode,
		LPSECURITY_ATTRIBUTES lpSecurityAttributes,
		DWORD dwCreationDisposition,
		DWORD dwFlagsAndAttributes,
		HANDLE hTemplateFile)
{
	if (!open(szFile, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
				dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile))
	{
		throw OpenFileException(szFile, ErrorText(GetLastError()).c_str());
	}
}
Beispiel #16
0
size_t BzipInputStream::read(void* buf, size_t size)
{
	assert(m_cf);
	assert(m_fp);
	int err = BZ_OK;
	int nRead = BZ2_bzRead(&err, m_fp, buf, size);
	if (BZ_OK != err)
	{
		std::ostringstream oss;
		oss << "BZ2_bzRead err=" << strbzerr(err)
			<< ", in " << BOOST_CURRENT_FUNCTION;
		throw OpenFileException("<fd>", oss.str().c_str());
	}
	assert(nRead <= (int)size);
	return (size_t)nRead;
}
Beispiel #17
0
void BzipInputStream::close()
{
	assert(m_fp);
	assert(m_cf);
	int err;
	BZ2_bzReadClose(&err, m_fp);
	::fclose(m_cf);
	m_fp = 0;
	m_cf = 0;
	if (BZ_OK != err)
	{
		std::ostringstream oss;
		oss << "BZ2_bzReadClose err=" << strbzerr(err)
			<< ", in " << BOOST_CURRENT_FUNCTION;
		throw OpenFileException("<fd>", oss.str().c_str());
	}
}
Beispiel #18
0
SharedMemoryImpl::SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void*):
	_name(file.path()),
	_memHandle(INVALID_HANDLE_VALUE),
	_fileHandle(INVALID_HANDLE_VALUE),
	_size(0),
	_mode(PAGE_READONLY),
	_address(0)
{
	if (!file.exists() || !file.isFile())
		throw FileNotFoundException(_name);

	_size = static_cast<DWORD>(file.getSize());

	DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
	DWORD fileMode  = GENERIC_READ;

	if (mode == SharedMemory::AM_WRITE)
	{
		_mode = PAGE_READWRITE;
		fileMode |= GENERIC_WRITE;
	}

#if defined (POCO_WIN32_UTF8)
	std::wstring utf16name;
	UnicodeConverter::toUTF16(_name, utf16name);
	_fileHandle = CreateFileW(utf16name.c_str(), fileMode, shareMode, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#else
	_fileHandle = CreateFileA(_name.c_str(), fileMode, shareMode, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#endif

	if (_fileHandle == INVALID_HANDLE_VALUE)
		throw OpenFileException("Cannot open memory mapped file", _name);

	_memHandle = CreateFileMapping(_fileHandle, NULL, _mode, 0, 0, NULL);
	if (!_memHandle)
	{
		CloseHandle(_fileHandle);
		_fileHandle = INVALID_HANDLE_VALUE;
		throw SystemException("Cannot map file into shared memory", _name);
	}
	map();
}
Beispiel #19
0
	Void compressImpl(const std::string& path)
	{
		std::string gzPath(path);
		gzPath.append(".gz");
		FileInputStream istr(path, std::ios::binary | std::ios::in);
		if (!istr.good()) throw OpenFileException(path);
		FileOutputStream ostr(gzPath, std::ios::binary | std::ios::out);
		if (ostr.good())
		{
			DeflatingOutputStream deflater(ostr, DeflatingStreamBuf::STREAM_GZIP);
			StreamCopier::copyStream(istr, deflater);
			deflater.close();
			ostr.close();
			istr.close();
			File f(path);
			f.remove();
		}
		else throw CreateFileException(gzPath);
		return Void();
	}
Beispiel #20
0
DirectoryIteratorImpl::DirectoryIteratorImpl(const std::string& path): _rc(1)
{
	Path p(path);
	p.makeDirectory();
	_search = p.toString();
	_search.append("*.*;*");

	_fab = cc$rms_fab;
	_fab.fab$l_fna = (char*) _search.c_str();
	_fab.fab$b_fns = _search.size();
	_fab.fab$l_nam = &_nam;

	_nam = cc$rms_nam;
	_nam.nam$l_esa = _spec;
	_nam.nam$b_ess = sizeof(_spec);

	if (sys$parse(&_fab) & 1)
		throw OpenFileException(path);

	next();
}
Beispiel #21
0
bool File::open(const char* filename, bool create, bool append, bool lock)
{
    if (isOpen())
    {
        close();

        ali::dealloc(this->filename);
    }

    // Set mode
    int oflag = O_RDWR;

    if (create)
        oflag |= O_CREAT;

    if (append)
        oflag |= O_APPEND;

    // Open file descriptor
    fd = ::open(filename, oflag, S_IRWXU);

    if (fd == -1)
    {
        // Reinitialize fd
        fd = -1;

        throw OpenFileException();

        return false;
    }

    opened = true;

    int size = strlen(filename) + 1;
    this->filename = (char*)ali::alloc(size);
    ali::copy(this->filename, (char*)filename, size);

    return true;
}
Beispiel #22
0
SharedMemoryImpl::SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void* addrHint):
    _size(0),
    _fd(-1),
    _address(0),
    _access(mode),
    _name(file.path()),
    _fileMapped(true),
    _server(false)
{
    if (!file.exists() || !file.isFile())
        throw FileNotFoundException(file.path());

    _size = file.getSize();
    int flag = O_RDONLY;
    if (mode == SharedMemory::AM_WRITE)
        flag = O_RDWR;
    _fd = ::open(_name.c_str(), flag);
    if (-1 == _fd)
        throw OpenFileException("Cannot open memory mapped file", _name);

    map(addrHint);
}
Beispiel #23
0
	bool File::open(const char* filename, bool create, bool append, bool lock)
	{
		// Make sure no other file is open
		if (isOpen())
		{
			close();

			ali::dealloc(this->filename);
		}

		// If append == true, attempt to open file for appending
		int access = GENERIC_READ | GENERIC_WRITE;
		
		if (append)
			access |= FILE_APPEND_DATA;

		// If create == true, create file if nonexistent
		int creation = 0;
		if (create)
		{
			if (File::exists(filename))
			{
				creation = OPEN_ALWAYS;
			}
			else
			{
				creation = CREATE_NEW;
			}
		}
		else
		{
			creation = OPEN_ALWAYS;
		}

		// If lock == true, lock file for reading/writing/deletion
		int security = (lock ? 0 : FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE);

		file = CreateFile(	filename,				// The filename
							access,					// Preferred access
							security,				// Whether to lock file
							NULL,
							creation,				// Whether to create if not existent
							FILE_ATTRIBUTE_NORMAL,	// Normal attributes
							NULL);

		if (file == INVALID_HANDLE_VALUE)
		{
			// Reinitialize handle
			file = NULL;

			throw OpenFileException();

			return false;
		}
		else
		{
			opened = true;

			// Save filename
			int size = strlen(filename) + 1;
			this->filename = (char*)ali::alloc(size);
			ali::copy(this->filename, (char*)filename, size);
		}

		return true;
	}