LINUX.ORG.RU

История изменений

Исправление hlamotron, (текущая версия) :

Да, в коде малость не хватает close(fd) на ошибках.

Исходная версия hlamotron, :

Да, в коде малость не хватает close(fd) на ошибках.

bool posix_mem_to_file_buffchain(const char *_filename, const std::vector<fir::str::BuffPtr> &_buffchain, bool _fsync)
{
	int fd = open(_filename, O_CREAT | O_TRUNC | O_NOATIME | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP);
	if ( fd < 0 ) {
		LOG_ERROR("posix_mem_to_file, open(): " << strerror(errno));
		return false;
	}

	ssize_t r = 0;
	for ( auto &buff : _buffchain ) {

		if ( ! buff.size )
			continue;

		r = write(fd, buff.ptr, buff.size);
		if ( r < 0 ) {
			LOG_ERROR("posix_mem_to_file, write(): " << strerror(errno));
			close(fd);
			return false;
		}

		if ( r != (ssize_t)buff.size ) {
			LOG_ERROR("posix_mem_to_file, write(): " << r << " / " << buff.size);
			close(fd);
			return false;
		}
	}

	if ( _fsync ) {
		r = fsync(fd);
		if ( r < 0 ) {
			LOG_ERROR("posix_mem_to_file, fsync(): " << strerror(errno));
			fclose(fd);
			return false;
		}
	}

	r = close(fd);
	if ( r < 0 ) {
		LOG_ERROR("posix_mem_to_file, close(): " << strerror(errno));
		return false;
	}

	return true;
}