release.c 5.76 KB
Newer Older
1
/*
2
 * Copyright (c) 2015, 2016, 2017 Jonas 'Sortie' Termansen.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * release.c
 * Utility functions to handle release information.
 */
19 20 21 22 23 24 25 26 27 28 29

#include <err.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "release.h"

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
int abi_compare(unsigned long a_major, unsigned long a_minor,
                unsigned long b_major, unsigned long b_minor)
{
	if ( a_major < b_major )
		return -1;
	if ( a_major > b_major )
		return 1;
	if ( a_minor < b_minor )
		return -1;
	if ( a_minor > b_minor )
		return 1;
	return 0;
}

bool abi_compatible(unsigned long a_major, unsigned long a_minor,
                    unsigned long b_major, unsigned long b_minor)
{
	return a_major == b_major && a_minor <= b_minor;
}

int version_compare(unsigned long a_major, unsigned long a_minor, bool a_dev,
                    unsigned long b_major, unsigned long b_minor, bool b_dev)
{
	if ( a_major < b_major )
		return -1;
	if ( a_major > b_minor )
		return 1;
	if ( a_minor < b_minor )
		return -1;
	if ( a_minor > b_minor )
		return 1;
	if ( a_dev && !b_dev )
		return -1;
	if ( !a_dev && b_dev )
		return 1;
	return 0;
}

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 140 141 142 143 144 145 146 147 148 149 150
void release_free(struct release* release)
{
	free(release->pretty_name);
}

static char* os_release_eval(const char* string)
{
	char* result;
	size_t result_size;
	FILE* fp = open_memstream(&result, &result_size);
	if ( !fp )
		return NULL;
	bool escaped = false;
	bool singly_quote = false;
	bool doubly_quote = false;
	while ( *string )
	{
		char c = *string++;
		if ( !escaped && !singly_quote && c == '\\' )
			escaped = true;
		else if ( !escaped && !doubly_quote && c == '\'' )
			singly_quote = !singly_quote;
		else if ( !escaped && !singly_quote && c == '"' )
			doubly_quote = !doubly_quote;
		else
		{
			fputc((unsigned char) c, fp);
			escaped = false;
		}
	}
	if ( fclose(fp) == EOF )
	{
		free(result);
		return NULL;
	}
	return result;
}

static void parse_version(const char* string,
                          unsigned long* major_ptr,
                          unsigned long* minor_ptr)

{
	*major_ptr = strtoul(string, (char**) &string, 10);
	if ( *string == '.' )
		string++;
	*minor_ptr = strtoul(string, (char**) &string, 10);
}

static void parse_release(const char* string,
                          unsigned long* major_ptr,
                          unsigned long* minor_ptr,
                          bool* dev_ptr)

{
	*major_ptr = strtoul(string, (char**) &string, 10);
	if ( *string == '.' )
		string++;
	*minor_ptr = strtoul(string, (char**) &string, 10);
	*dev_ptr = *string != '\0';
}

bool os_release_load(struct release* release,
                     const char* path,
                     const char* errpath)
{
	memset(release, 0, sizeof(*release));
	FILE* fp = fopen(path, "r");
	if ( !fp )
	{
		if ( errno != ENOENT )
			warn("%s", errpath);
		return false;
	}
	bool failure = false;
	bool success = false;
	bool found_id = false;
	bool found_pretty_name = false;
	bool found_sortix_abi = false;
	bool found_version_id = false;
	char* line = NULL;
	size_t line_size = 0;
	ssize_t line_length;
151
	while ( 0 < (line_length = getline(&line, &line_size, fp)) )
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	{
		if ( line[line_length-1] == '\n' )
			line[--line_length] = '\0';
		if ( !strncmp(line, "ID=", strlen("ID=")) )
		{
			const char* param = line + strlen("ID=");
			found_id = true;
			char* value = os_release_eval(param);
			if ( value )
			{
				if ( strcmp(value, "sortix") != 0 )
				{
					warn("%s: ID does not specify a 'sortix' system", errpath);
					failure = true;
				}
				free(value);
			}
			else
			{
				warn("malloc");
				failure = true;
			}
		}
		else if ( !strncmp(line, "PRETTY_NAME=", strlen("PRETTY_NAME=")) )
		{
			const char* param = line + strlen("PRETTY_NAME=");
			found_pretty_name = true;
			if ( !(release->pretty_name = os_release_eval(param)) )
			{
				warn("malloc");
				failure = true;
			}
		}
		else if ( !strncmp(line, "SORTIX_ABI=", strlen("SORTIX_ABI=")) )
		{
			const char* param = line + strlen("SORTIX_ABI=");
			found_sortix_abi = true;
			char* value = os_release_eval(param);
			if ( value )
			{
				parse_version(value, &release->abi_major, &release->abi_minor);
				free(value);
			}
			else
			{
				warn("malloc");
				failure = true;
			}
		}
		else if ( !strncmp(line, "VERSION_ID=", strlen("VERSION_ID=")) )
		{
			const char* param = line + strlen("VERSION_ID=");
			found_version_id = true;
			char* value = os_release_eval(param);
			if ( value )
			{
				parse_release(value, &release->version_major,
				              &release->version_minor, &release->version_dev);
				free(value);
			}
			else
			{
				warn("malloc");
				failure = true;
			}
		}
	}
219
	if ( ferror(fp) )
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
		warn("%s", errpath);
	else if ( failure )
		;
	else if ( !found_id )
		warnx("%s: No ID", errpath);
	else if ( !found_pretty_name )
		warnx("%s: No PRETTY_NAME", errpath);
	else if ( !found_sortix_abi )
		warnx("%s: No SORTIX_ABI", errpath);
	else if ( !found_version_id )
		warnx("%s: No VERSION_ID", errpath);
	else
		success = true;
	free(line);
	fclose(fp);
	if ( failure || !success )
	{
		release_free(release);
		return false;
	}
	return true;
}