mirror of
https://github.com/fosslinux/live-bootstrap.git
synced 2026-03-05 02:45:23 +01:00
- This idea originates from very early in the project and was, at the
time, a very easy way to categorise things.
- Now, it doesn't really make much sense - it is fairly arbitary, often
occuring when there is a change in kernel, but not from builder-hex0
to fiwix, and sysb is in reality completely unnecessary.
- In short, the sys* stuff is a bit of a mess that makes the project
more difficult to understand.
- This puts everything down into one folder and has a manifest file that
is used to generate the build scripts on the fly rather than using
coded scripts.
- This is created in the "seed" stage.
stage0-posix -- (calls) --> seed -- (generates) --> main steps
Alongside this change there are a variety of other smaller fixups to the
general structure of the live-bootstrap rootfs.
- Creating a rootfs has become much simpler and is defined as code in
go.sh. The new structure, for an about-to-be booted system, is
/
-- /steps (direct copy of steps/)
-- /distfiles (direct copy of distfiles/)
-- all files from seed/*
-- all files from seed/stage0-posix/*
- There is no longer such a thing as /usr/include/musl, this didn't
really make any sense, as musl is the final libc used. Rather, to
separate musl and mes, we have /usr/include/mes, which is much easier
to work with.
- This also makes mes easier to blow away later.
- A few things that weren't properly in packages have been changed;
checksum-transcriber, simple-patch, kexec-fiwix have all been given
fully qualified package names.
- Highly breaking change, scripts now exist in their package directory
but NOT WITH THE packagename.sh. Rather, they use pass1.sh, pass2.sh,
etc. This avoids manual definition of passes.
- Ditto with patches; default directory is patches, but then any patch
series specific to a pass are named patches-passX.
148 lines
4.7 KiB
C
148 lines
4.7 KiB
C
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "M2libc/bootstrappable.h"
|
|
|
|
/*
|
|
SPDX-FileCopyrightText: 2023 Richard Masters <grick23@gmail.com>
|
|
SPDX-License-Identifier: MIT
|
|
|
|
Simple Patch program.
|
|
|
|
This program is written in a subset of C called M2, which is from the
|
|
stage0-posix bootstrap project.
|
|
|
|
Example usage:
|
|
./simple-patch file_to_patch before_pattern_file after_pattern_file
|
|
|
|
*/
|
|
|
|
// function prototypes
|
|
void read_file_or_die(char *file_name, char **buffer, int *file_size);
|
|
void patch_buffer_or_die(char *patch_file_before_buffer, int patch_file_before_size,
|
|
char *before_pattern_buffer, int before_pattern_size,
|
|
char *after_pattern_buffer, int after_pattern_size,
|
|
char *patch_file_after_buffer);
|
|
void writestr_fd(int fd, char *str);
|
|
int memsame(char *search_buffer, int search_size,
|
|
char *pattern_buffer, int pattern_size);
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
char *patch_file_before_buffer;
|
|
int patch_file_before_size;
|
|
|
|
char *before_pattern_buffer;
|
|
int before_pattern_size;
|
|
|
|
char *after_pattern_buffer;
|
|
int after_pattern_size;
|
|
|
|
int patch_file_after_size;
|
|
char *patch_file_after_buffer;
|
|
|
|
int patch_file_fd;
|
|
|
|
read_file_or_die(argv[1], &patch_file_before_buffer, &patch_file_before_size);
|
|
read_file_or_die(argv[2], &before_pattern_buffer, &before_pattern_size);
|
|
read_file_or_die(argv[3], &after_pattern_buffer, &after_pattern_size);
|
|
|
|
patch_file_after_size = patch_file_before_size - before_pattern_size + after_pattern_size;
|
|
patch_file_after_buffer = calloc(patch_file_after_size, sizeof(char));
|
|
|
|
patch_buffer_or_die(patch_file_before_buffer, patch_file_before_size,
|
|
before_pattern_buffer, before_pattern_size,
|
|
after_pattern_buffer, after_pattern_size,
|
|
patch_file_after_buffer);
|
|
|
|
patch_file_fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0);
|
|
write(patch_file_fd, patch_file_after_buffer, patch_file_after_size);
|
|
close(patch_file_fd);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
|
|
void read_file_or_die(char *file_name, char **buffer, int *file_size) {
|
|
int file_fd;
|
|
int num_bytes_read;
|
|
|
|
file_fd = open(file_name, O_RDONLY, 0);
|
|
if (file_fd == -1) {
|
|
writestr_fd(2, "Could not open file: ");
|
|
writestr_fd(2, file_name);
|
|
writestr_fd(2, "\n");
|
|
exit(1);
|
|
}
|
|
// determine file size
|
|
*file_size = lseek(file_fd, 0, SEEK_END);
|
|
// go back to beginning of file
|
|
lseek(file_fd, 0, SEEK_SET);
|
|
// alloc a buffer to read the entire file
|
|
*buffer = calloc(*file_size, sizeof(char));
|
|
|
|
// read the entire patch file
|
|
num_bytes_read = read(file_fd, *buffer, *file_size);
|
|
if (num_bytes_read != *file_size) {
|
|
writestr_fd(2, "Could not read file: ");
|
|
writestr_fd(2, file_name);
|
|
writestr_fd(2, "\n");
|
|
exit(1);
|
|
}
|
|
close(file_fd);
|
|
}
|
|
|
|
void patch_buffer_or_die(char *patch_file_before_buffer, int patch_file_before_size,
|
|
char *before_pattern_buffer, int before_pattern_size,
|
|
char *after_pattern_buffer, int after_pattern_size,
|
|
char *patch_file_after_buffer) {
|
|
|
|
char *pos = patch_file_before_buffer;
|
|
int prefix_len = 0;
|
|
|
|
// look for the pattern at every offset
|
|
while (prefix_len < patch_file_before_size) {
|
|
// if we find the pattern, replace it and return
|
|
if (memsame(pos, patch_file_before_size - prefix_len, before_pattern_buffer, before_pattern_size)) {
|
|
memcpy(patch_file_after_buffer, patch_file_before_buffer, prefix_len);
|
|
memcpy(patch_file_after_buffer + prefix_len, after_pattern_buffer, after_pattern_size);
|
|
memcpy(patch_file_after_buffer + prefix_len + after_pattern_size,
|
|
patch_file_before_buffer + prefix_len + before_pattern_size,
|
|
patch_file_before_size - (prefix_len + before_pattern_size));
|
|
return;
|
|
}
|
|
pos = pos + 1;
|
|
prefix_len = prefix_len + 1;
|
|
}
|
|
|
|
/* if we don't find the pattern, something is wrong, so exit with error */
|
|
exit(1);
|
|
}
|
|
|
|
/*
|
|
Write the string to the given file descriptor.
|
|
*/
|
|
void writestr_fd(int fd, char *str) {
|
|
write(fd, str, strlen(str));
|
|
}
|
|
|
|
/*
|
|
Is the pattern located at the start of the search buffer
|
|
(and not exceeding the length of the search buffer)?
|
|
*/
|
|
|
|
int memsame(char *search_buffer, int search_size,
|
|
char *pattern_buffer, int pattern_size) {
|
|
int check_offset = 0;
|
|
|
|
if (pattern_size > search_size) {
|
|
return FALSE;
|
|
}
|
|
while (check_offset < pattern_size) {
|
|
if (search_buffer[check_offset] != pattern_buffer[check_offset]) {
|
|
return FALSE;
|
|
}
|
|
check_offset = check_offset + 1;
|
|
}
|
|
return TRUE;
|
|
}
|