DynBuf.cpp 2.96 KB
Newer Older
Abhijith PA's avatar
Abhijith PA committed
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
/**
 * Copyright (C) ARM Limited 2013-2014. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include "DynBuf.h"

#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>

#include "Logging.h"

// Pick an aggressive size as buffer is primarily used for disk IO
#define MIN_BUFFER_FREE (1 << 12)

int DynBuf::resize(const size_t minCapacity) {
	size_t scaledCapacity = 2 * capacity;
	if (scaledCapacity < minCapacity) {
		scaledCapacity = minCapacity;
	}
	if (scaledCapacity < 2 * MIN_BUFFER_FREE) {
		scaledCapacity = 2 * MIN_BUFFER_FREE;
	}
	capacity = scaledCapacity;

	buf = static_cast<char *>(realloc(buf, capacity));
	if (buf == NULL) {
		return -errno;
	}

	return 0;
}

bool DynBuf::read(const char *const path) {
	int result = false;

	const int fd = open(path, O_RDONLY | O_CLOEXEC);
	if (fd < 0) {
		logg->logMessage("%s(%s:%i): open failed", __FUNCTION__, __FILE__, __LINE__);
		return false;
	}

	length = 0;

	for (;;) {
		const size_t minCapacity = length + MIN_BUFFER_FREE + 1;
		if (capacity < minCapacity) {
			if (resize(minCapacity) != 0) {
				logg->logMessage("%s(%s:%i): DynBuf::resize failed", __FUNCTION__, __FILE__, __LINE__);
				goto fail;
			}
		}

		const ssize_t bytes = ::read(fd, buf + length, capacity - length - 1);
		if (bytes < 0) {
			logg->logMessage("%s(%s:%i): read failed", __FUNCTION__, __FILE__, __LINE__);
			goto fail;
		} else if (bytes == 0) {
			break;
		}
		length += bytes;
	}

	buf[length] = '\0';
	result = true;

 fail:
	close(fd);

	return result;
}

int DynBuf::readlink(const char *const path) {
	ssize_t bytes = MIN_BUFFER_FREE;

	for (;;) {
		if (static_cast<size_t>(bytes) >= capacity) {
			const int err = resize(2 * bytes);
			if (err != 0) {
				return err;
			}
		}
		bytes = ::readlink(path, buf, capacity);
		if (bytes < 0) {
			return -errno;
		} else if (static_cast<size_t>(bytes) < capacity) {
			break;
		}
	}

	length = bytes;
	buf[bytes] = '\0';

	return 0;
}

bool DynBuf::printf(const char *format, ...) {
	va_list ap;

	if (capacity <= 0) {
		if (resize(2 * MIN_BUFFER_FREE) != 0) {
			logg->logMessage("%s(%s:%i): DynBuf::resize failed", __FUNCTION__, __FILE__, __LINE__);
			return false;
		}
	}

	va_start(ap, format);
	int bytes = vsnprintf(buf, capacity, format, ap);
	va_end(ap);
	if (bytes < 0) {
		logg->logMessage("%s(%s:%i): fsnprintf failed", __FUNCTION__, __FILE__, __LINE__);
		return false;
	}

	if (static_cast<size_t>(bytes) > capacity) {
		if (resize(bytes + 1) != 0) {
			logg->logMessage("%s(%s:%i): DynBuf::resize failed", __FUNCTION__, __FILE__, __LINE__);
			return false;
		}

		va_start(ap, format);
		bytes = vsnprintf(buf, capacity, format, ap);
		va_end(ap);
		if (bytes < 0) {
			logg->logMessage("%s(%s:%i): fsnprintf failed", __FUNCTION__, __FILE__, __LINE__);
			return false;
		}
	}

	length = bytes;

	return true;
}