Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
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.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
2016-04-10 09:19:12 -04:00
|
|
|
* Copyright (c) 2012, 2013, 2014, 2015, 2016 Jonas 'Sortie' Termansen.
|
Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
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.
2016-03-02 17:38:16 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* mkinitrd.c
|
|
|
|
* Produces a simple ramdisk filesystem readable by the Sortix kernel.
|
|
|
|
*/
|
2011-08-27 08:50:52 -04:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2014-04-23 08:46:09 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2015-11-21 18:26:23 -05:00
|
|
|
#include <assert.h>
|
2014-04-23 08:46:09 -04:00
|
|
|
#include <dirent.h>
|
2015-01-19 10:27:52 -05:00
|
|
|
#include <endian.h>
|
2016-08-09 19:29:04 -04:00
|
|
|
#include <err.h>
|
2011-08-27 08:50:52 -04:00
|
|
|
#include <errno.h>
|
2012-03-23 14:36:51 -04:00
|
|
|
#include <fcntl.h>
|
2013-09-25 17:40:27 -04:00
|
|
|
#include <ioleast.h>
|
2016-02-28 17:03:16 -05:00
|
|
|
#include <stdalign.h>
|
|
|
|
#include <stdbool.h>
|
2012-03-23 14:36:51 -04:00
|
|
|
#include <stdint.h>
|
2014-04-23 08:46:09 -04:00
|
|
|
#include <stdio.h>
|
2012-03-23 14:36:51 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-08-27 08:50:52 -04:00
|
|
|
#include <unistd.h>
|
2014-04-23 08:46:09 -04:00
|
|
|
|
2011-08-27 08:50:52 -04:00
|
|
|
#include <sortix/initrd.h>
|
|
|
|
|
2014-04-23 11:53:19 -04:00
|
|
|
#define DEFAULT_FORMAT "sortix-initrd-2"
|
|
|
|
|
2013-01-29 14:01:46 -05:00
|
|
|
#include "rules.h"
|
2015-01-19 10:27:52 -05:00
|
|
|
#include "serialize.h"
|
2012-03-23 14:36:51 -04:00
|
|
|
|
|
|
|
uint32_t HostModeToInitRD(mode_t mode)
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t result = mode & 0777; // Lower 9 bits per POSIX and tradition.
|
|
|
|
if ( S_ISVTX & mode ) { result |= INITRD_S_ISVTX; }
|
|
|
|
if ( S_ISSOCK(mode) ) { result |= INITRD_S_IFSOCK; }
|
|
|
|
if ( S_ISLNK(mode) ) { result |= INITRD_S_IFLNK; }
|
|
|
|
if ( S_ISREG(mode) ) { result |= INITRD_S_IFREG; }
|
|
|
|
if ( S_ISBLK(mode) ) { result |= INITRD_S_IFBLK; }
|
|
|
|
if ( S_ISDIR(mode) ) { result |= INITRD_S_IFDIR; }
|
|
|
|
if ( S_ISCHR(mode) ) { result |= INITRD_S_IFCHR; }
|
|
|
|
if ( S_ISFIFO(mode) ) { result |= INITRD_S_IFIFO; }
|
|
|
|
return result;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
mode_t InitRDModeToHost(uint32_t mode)
|
|
|
|
{
|
|
|
|
mode_t result = mode & 0777; // Lower 9 bits per POSIX and tradition.
|
|
|
|
if ( INITRD_S_ISVTX & mode ) { result |= S_ISVTX; }
|
|
|
|
if ( INITRD_S_ISSOCK(mode) ) { result |= S_IFSOCK; }
|
|
|
|
if ( INITRD_S_ISLNK(mode) ) { result |= S_IFLNK; }
|
|
|
|
if ( INITRD_S_ISREG(mode) ) { result |= S_IFREG; }
|
|
|
|
if ( INITRD_S_ISBLK(mode) ) { result |= S_IFBLK; }
|
|
|
|
if ( INITRD_S_ISDIR(mode) ) { result |= S_IFDIR; }
|
|
|
|
if ( INITRD_S_ISCHR(mode) ) { result |= S_IFCHR; }
|
|
|
|
if ( INITRD_S_ISFIFO(mode) ) { result |= S_IFIFO; }
|
|
|
|
return result;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
struct Node;
|
|
|
|
struct DirEntry;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
struct DirEntry
|
|
|
|
{
|
|
|
|
char* name;
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* node;
|
2012-03-23 14:36:51 -04:00
|
|
|
};
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
int DirEntryCompare(const struct DirEntry* a, const struct DirEntry* b)
|
2015-07-24 22:04:34 -04:00
|
|
|
{
|
|
|
|
return strcmp(a->name, b->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
int DirEntryCompareIndirect(const void* a_ptr, const void* b_ptr)
|
|
|
|
{
|
2016-02-28 17:03:16 -05:00
|
|
|
const struct DirEntry* a = (const struct DirEntry*) a_ptr;
|
|
|
|
const struct DirEntry* b = (const struct DirEntry*) b_ptr;
|
2015-07-24 22:04:34 -04:00
|
|
|
return DirEntryCompare(a, b);
|
|
|
|
}
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
struct Node
|
|
|
|
{
|
|
|
|
char* path;
|
|
|
|
uint32_t ino;
|
|
|
|
uint32_t nlink;
|
|
|
|
size_t direntsused;
|
|
|
|
size_t direntslength;
|
2016-02-28 17:03:16 -05:00
|
|
|
struct DirEntry* dirents;
|
2012-03-23 14:36:51 -04:00
|
|
|
mode_t mode;
|
|
|
|
time_t ctime;
|
|
|
|
time_t mtime;
|
2012-12-09 07:23:18 -05:00
|
|
|
bool written;
|
|
|
|
size_t refcount;
|
2012-03-23 14:36:51 -04:00
|
|
|
};
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
void FreeNode(struct Node* node)
|
2011-12-01 15:44:11 -05:00
|
|
|
{
|
2015-07-24 22:04:34 -04:00
|
|
|
if ( !node )
|
|
|
|
return;
|
2012-03-23 14:36:51 -04:00
|
|
|
if ( 1 < node->nlink ) { node->nlink--; return; }
|
|
|
|
for ( size_t i = 0; i < node->direntsused; i++ )
|
2011-12-01 15:44:11 -05:00
|
|
|
{
|
2016-02-28 17:03:16 -05:00
|
|
|
struct DirEntry* entry = node->dirents + i;
|
2015-07-24 22:04:34 -04:00
|
|
|
if ( !entry->name )
|
|
|
|
continue;
|
2012-03-23 14:36:51 -04:00
|
|
|
if ( strcmp(entry->name, ".") != 0 && strcmp(entry->name, "..") != 0 )
|
|
|
|
{
|
2012-12-09 07:23:18 -05:00
|
|
|
if ( --entry->node->refcount == 0 )
|
|
|
|
FreeNode(entry->node);
|
2012-03-23 14:36:51 -04:00
|
|
|
}
|
|
|
|
free(entry->name);
|
2011-12-01 15:44:11 -05:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
free(node->dirents);
|
|
|
|
free(node->path);
|
|
|
|
free(node);
|
2011-12-01 15:44:11 -05:00
|
|
|
}
|
|
|
|
|
2012-12-09 07:23:18 -05:00
|
|
|
struct CacheEntry
|
|
|
|
{
|
|
|
|
ino_t ino;
|
|
|
|
dev_t dev;
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* node;
|
2012-12-09 07:23:18 -05:00
|
|
|
};
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
static size_t cacheused = 0;
|
|
|
|
static size_t cachelen = 0;
|
|
|
|
static struct CacheEntry* cache = NULL;
|
2012-12-09 07:23:18 -05:00
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* LookupCache(dev_t dev, ino_t ino)
|
2012-12-09 07:23:18 -05:00
|
|
|
{
|
|
|
|
for ( size_t i = 0; i < cacheused; i++ )
|
|
|
|
if ( cache[i].dev == dev && cache[i].ino == ino )
|
|
|
|
return cache[i].node;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
bool AddToCache(struct Node* node, dev_t dev, ino_t ino)
|
2012-12-09 07:23:18 -05:00
|
|
|
{
|
|
|
|
if ( cacheused == cachelen )
|
|
|
|
{
|
|
|
|
size_t newcachelen = cachelen ? 2 * cachelen : 256;
|
2016-02-28 17:03:16 -05:00
|
|
|
size_t newcachesize = newcachelen * sizeof(struct CacheEntry);
|
|
|
|
struct CacheEntry* newcache =
|
|
|
|
(struct CacheEntry*) realloc(cache, newcachesize);
|
2012-12-09 07:23:18 -05:00
|
|
|
if ( !newcache )
|
|
|
|
return false;
|
|
|
|
cache = newcache;
|
|
|
|
cachelen = newcachelen;
|
|
|
|
}
|
|
|
|
size_t index = cacheused++;
|
|
|
|
cache[index].ino = ino;
|
|
|
|
cache[index].dev = dev;
|
|
|
|
cache[index].node = node;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* RecursiveSearch(const char* real_path, const char* virt_path,
|
|
|
|
uint32_t* ino, struct Node* parent)
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2013-01-29 14:01:46 -05:00
|
|
|
printf("%s\n", virt_path);
|
2016-02-07 16:20:12 -05:00
|
|
|
fflush(stdout);
|
2013-01-29 14:01:46 -05:00
|
|
|
|
|
|
|
if ( virt_path[0] == '/' && !virt_path[1] )
|
|
|
|
virt_path = "";
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
struct stat st;
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( lstat(real_path, &st) != 0 )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("stat: %s", real_path);
|
2014-04-23 08:46:09 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2015-07-24 20:41:45 -04:00
|
|
|
if ( !S_ISDIR(st.st_mode) && 2 <= st.st_nlink )
|
|
|
|
{
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* cached = LookupCache(st.st_dev, st.st_ino);
|
2015-07-24 20:41:45 -04:00
|
|
|
if ( cached )
|
|
|
|
return cached->nlink++, cached->refcount++, cached;
|
|
|
|
}
|
2012-12-09 07:23:18 -05:00
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* node = (struct Node*) calloc(1, sizeof(struct Node));
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !node )
|
|
|
|
return NULL;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2014-04-29 17:34:12 -04:00
|
|
|
node->nlink = 1;
|
2012-12-09 07:23:18 -05:00
|
|
|
node->refcount = 1;
|
2012-03-23 14:36:51 -04:00
|
|
|
node->mode = st.st_mode;
|
|
|
|
node->ino = (*ino)++;
|
2013-03-22 15:19:45 -04:00
|
|
|
node->ctime = st.st_ctim.tv_sec;
|
|
|
|
node->mtime = st.st_mtim.tv_sec;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2013-01-29 14:01:46 -05:00
|
|
|
char* real_path_clone = strdup(real_path);
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !real_path_clone )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("strdup");
|
2014-04-23 08:46:09 -04:00
|
|
|
free(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2013-01-29 14:01:46 -05:00
|
|
|
node->path = real_path_clone;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2015-07-24 20:41:45 -04:00
|
|
|
if ( !S_ISDIR(st.st_mode))
|
2012-12-09 07:23:18 -05:00
|
|
|
{
|
2015-07-24 20:41:45 -04:00
|
|
|
if ( 2 <= st.st_nlink && !AddToCache(node, st.st_dev, st.st_ino) )
|
2012-12-09 07:23:18 -05:00
|
|
|
{
|
2013-01-29 14:01:46 -05:00
|
|
|
free(real_path_clone);
|
2012-12-09 07:23:18 -05:00
|
|
|
free(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2013-01-29 14:01:46 -05:00
|
|
|
DIR* dir = opendir(real_path);
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !dir )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("opendir: %s", real_path);
|
2014-04-23 08:46:09 -04:00
|
|
|
FreeNode(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
|
2013-01-29 14:01:46 -05:00
|
|
|
size_t real_path_len = strlen(real_path);
|
|
|
|
size_t virt_path_len = strlen(virt_path);
|
2012-03-23 14:36:51 -04:00
|
|
|
|
|
|
|
bool successful = true;
|
|
|
|
struct dirent* entry;
|
|
|
|
while ( (entry = readdir(dir)) )
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
size_t namelen = strlen(entry->d_name);
|
2013-01-29 14:01:46 -05:00
|
|
|
|
|
|
|
size_t virt_subpath_len = virt_path_len + 1 + namelen;
|
|
|
|
char* virt_subpath = (char*) malloc(virt_subpath_len+1);
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !virt_subpath )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("malloc");
|
2014-04-23 08:46:09 -04:00
|
|
|
successful = false;
|
|
|
|
break;
|
|
|
|
}
|
2013-01-29 14:01:46 -05:00
|
|
|
stpcpy(stpcpy(stpcpy(virt_subpath, virt_path), "/"), entry->d_name);
|
|
|
|
|
|
|
|
if ( strcmp(entry->d_name, ".") != 0 &&
|
|
|
|
strcmp(entry->d_name, "..") != 0 &&
|
2016-02-28 17:03:16 -05:00
|
|
|
!IncludesPath(virt_subpath) )
|
2013-01-29 14:01:46 -05:00
|
|
|
{
|
|
|
|
free(virt_subpath);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t real_subpath_len = real_path_len + 1 + namelen;
|
|
|
|
char* real_subpath = (char*) malloc(real_subpath_len+1);
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !real_subpath )
|
|
|
|
{
|
|
|
|
free(virt_subpath);
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("malloc");
|
2014-04-23 08:46:09 -04:00
|
|
|
successful = false;
|
|
|
|
break;
|
|
|
|
}
|
2013-01-29 14:01:46 -05:00
|
|
|
stpcpy(stpcpy(stpcpy(real_subpath, real_path), "/"), entry->d_name);
|
2012-03-23 14:36:51 -04:00
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* child = NULL;
|
2013-01-29 14:01:46 -05:00
|
|
|
if ( !strcmp(entry->d_name, ".") )
|
|
|
|
child = node;
|
|
|
|
if ( !strcmp(entry->d_name, "..") )
|
|
|
|
child = parent ? parent : node;
|
|
|
|
if ( !child )
|
|
|
|
child = RecursiveSearch(real_subpath, virt_subpath, ino, node);
|
|
|
|
free(real_subpath);
|
|
|
|
free(virt_subpath);
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !child )
|
|
|
|
{
|
|
|
|
successful = false;
|
|
|
|
break;
|
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
|
|
|
|
if ( node->direntsused == node->direntslength )
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
size_t oldlength = node->direntslength;
|
|
|
|
size_t newlength = oldlength ? 2 * oldlength : 8;
|
2016-02-28 17:03:16 -05:00
|
|
|
size_t newsize = sizeof(struct DirEntry) * newlength;
|
|
|
|
struct DirEntry* newdirents = (struct DirEntry*) realloc(node->dirents, newsize);
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !newdirents )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("realloc");
|
2014-04-23 08:46:09 -04:00
|
|
|
successful = false;
|
|
|
|
break;
|
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
node->dirents = newdirents;
|
|
|
|
node->direntslength = newlength;
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
|
|
|
|
char* nameclone = strdup(entry->d_name);
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !nameclone )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("strdup");
|
2014-04-23 08:46:09 -04:00
|
|
|
successful = false;
|
|
|
|
break;
|
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
struct DirEntry* entry = node->dirents + node->direntsused++;
|
2012-03-23 14:36:51 -04:00
|
|
|
|
|
|
|
entry->name = nameclone;
|
|
|
|
entry->node = child;
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
closedir(dir);
|
2015-07-24 20:41:45 -04:00
|
|
|
if ( !successful )
|
2012-12-09 07:23:18 -05:00
|
|
|
{
|
|
|
|
FreeNode(node);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-02-28 17:03:16 -05:00
|
|
|
qsort(node->dirents, node->direntsused, sizeof(struct DirEntry),
|
2015-07-24 22:04:34 -04:00
|
|
|
DirEntryCompareIndirect);
|
2012-03-23 14:36:51 -04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* MergeNodes(struct Node* a, struct Node* b)
|
2015-07-24 22:04:34 -04:00
|
|
|
{
|
|
|
|
if ( !S_ISDIR(a->mode) || !S_ISDIR(b->mode) )
|
|
|
|
{
|
|
|
|
FreeNode(b);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
size_t dirents_used = 0;
|
|
|
|
size_t dirents_length = a->direntsused + b->direntsused;
|
2016-02-28 17:03:16 -05:00
|
|
|
struct DirEntry* dirents = (struct DirEntry*)
|
|
|
|
malloc(sizeof(struct DirEntry) * dirents_length);
|
2015-07-24 22:04:34 -04:00
|
|
|
if ( !dirents )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("malloc");
|
2015-07-24 22:04:34 -04:00
|
|
|
FreeNode(a);
|
|
|
|
FreeNode(b);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bool failure = false;
|
|
|
|
size_t ai = 0;
|
|
|
|
size_t bi = 0;
|
|
|
|
while ( ai != a->direntsused || bi != b->direntsused )
|
|
|
|
{
|
|
|
|
if ( bi == b->direntsused ||
|
|
|
|
(ai != a->direntsused &&
|
|
|
|
bi != b->direntsused &&
|
|
|
|
DirEntryCompare(&a->dirents[ai], &b->dirents[bi]) < 0) )
|
|
|
|
{
|
|
|
|
dirents[dirents_used++] = a->dirents[ai];
|
|
|
|
a->dirents[ai].name = NULL;
|
|
|
|
a->dirents[ai].node = NULL;
|
|
|
|
ai++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( ai == a->direntsused ||
|
|
|
|
(ai != a->direntsused &&
|
|
|
|
bi != b->direntsused &&
|
|
|
|
DirEntryCompare(&a->dirents[ai], &b->dirents[bi]) > 0) )
|
|
|
|
{
|
|
|
|
dirents[dirents_used++] = b->dirents[bi];
|
|
|
|
for ( size_t i = 0; i < b->dirents[bi].node->direntsused; i++ )
|
|
|
|
{
|
|
|
|
if ( strcmp(b->dirents[bi].node->dirents[i].name, "..") != 0 )
|
|
|
|
continue;
|
|
|
|
b->dirents[bi].node->dirents[i].node = a;
|
|
|
|
}
|
|
|
|
b->dirents[bi].name = NULL;
|
|
|
|
b->dirents[bi].node = NULL;
|
|
|
|
bi++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const char* name = a->dirents[ai].name;
|
|
|
|
dirents[dirents_used].name = a->dirents[ai].name;
|
|
|
|
if ( !strcmp(name, ".") || !strcmp(name, "..") )
|
|
|
|
dirents[dirents_used].node = a->dirents[ai].node;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dirents[dirents_used].node =
|
|
|
|
MergeNodes(a->dirents[ai].node, b->dirents[bi].node);
|
|
|
|
if ( !dirents[dirents_used].node )
|
|
|
|
failure = true;
|
|
|
|
}
|
|
|
|
dirents_used++;
|
|
|
|
a->dirents[ai].name = NULL;
|
|
|
|
a->dirents[ai].node = NULL;
|
|
|
|
ai++;
|
|
|
|
free(b->dirents[bi].name);
|
|
|
|
b->dirents[bi].name = NULL;
|
|
|
|
b->dirents[bi].node = NULL;
|
|
|
|
bi++;
|
|
|
|
}
|
|
|
|
free(a->dirents);
|
|
|
|
a->dirents = dirents;
|
|
|
|
a->direntsused = dirents_used;
|
|
|
|
a->direntslength = dirents_length;
|
|
|
|
b->direntsused = 0;
|
|
|
|
FreeNode(b);
|
|
|
|
if ( failure )
|
2016-02-28 17:03:16 -05:00
|
|
|
return FreeNode(b), (struct Node*) NULL;
|
2015-07-24 22:04:34 -04:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
bool WriteNode(struct initrd_superblock* sb, int fd, const char* outputname,
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* node)
|
2014-04-23 08:46:09 -04:00
|
|
|
{
|
2012-12-09 07:23:18 -05:00
|
|
|
if ( node->written )
|
|
|
|
return true;
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t filesize = 0;
|
|
|
|
uint32_t origfssize = sb->fssize;
|
|
|
|
uint32_t dataoff = origfssize;
|
|
|
|
uint32_t filestart = dataoff;
|
|
|
|
|
|
|
|
if ( S_ISLNK(node->mode) ) // Symbolic link
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
const size_t NAME_SIZE = 1024UL;
|
|
|
|
char name[NAME_SIZE];
|
|
|
|
ssize_t namelen = readlink(node->path, name, NAME_SIZE);
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( namelen < 0 )
|
2016-08-09 19:29:04 -04:00
|
|
|
return warn("readlink: %s", node->path), false;
|
2012-03-23 14:36:51 -04:00
|
|
|
filesize = (uint32_t) namelen;
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( pwriteall(fd, name, filesize, dataoff) < filesize )
|
2016-08-09 19:29:04 -04:00
|
|
|
return warn("read: %s", node->path), false;
|
2012-03-23 14:36:51 -04:00
|
|
|
dataoff += filesize;
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
else if ( S_ISREG(node->mode) ) // Regular file
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
int nodefd = open(node->path, O_RDONLY);
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( nodefd < 0 )
|
2016-08-09 19:29:04 -04:00
|
|
|
return warn("open: %s", node->path), false;
|
2012-03-23 14:36:51 -04:00
|
|
|
const size_t BUFFER_SIZE = 16UL * 1024UL;
|
|
|
|
uint8_t buffer[BUFFER_SIZE];
|
|
|
|
ssize_t amount;
|
|
|
|
while ( 0 < (amount = read(nodefd, buffer, BUFFER_SIZE)) )
|
|
|
|
{
|
|
|
|
if ( pwriteall(fd, buffer, amount, dataoff) < (size_t) amount )
|
|
|
|
{
|
|
|
|
close(nodefd);
|
2016-08-09 19:29:04 -04:00
|
|
|
return warn("write: %s", outputname), false;
|
2012-03-23 14:36:51 -04:00
|
|
|
}
|
|
|
|
dataoff += amount;
|
|
|
|
filesize += amount;
|
|
|
|
}
|
|
|
|
close(nodefd);
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( amount < 0 )
|
2016-08-09 19:29:04 -04:00
|
|
|
return warn("read: %s", node->path), false;
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
else if ( S_ISDIR(node->mode) ) // Directory
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2012-03-23 14:36:51 -04:00
|
|
|
for ( size_t i = 0; i < node->direntsused; i++ )
|
|
|
|
{
|
2016-02-28 17:03:16 -05:00
|
|
|
struct DirEntry* entry = node->dirents + i;
|
2012-03-23 14:36:51 -04:00
|
|
|
const char* name = entry->name;
|
|
|
|
size_t namelen = strlen(entry->name);
|
|
|
|
struct initrd_dirent dirent;
|
|
|
|
dirent.inode = entry->node->ino;
|
|
|
|
dirent.namelen = (uint16_t) namelen;
|
|
|
|
dirent.reclen = sizeof(dirent) + dirent.namelen + 1;
|
2012-12-09 07:23:18 -05:00
|
|
|
dirent.reclen = (dirent.reclen+3)/4*4; // Align entries.
|
2012-03-23 14:36:51 -04:00
|
|
|
size_t entsize = sizeof(dirent);
|
2015-01-19 10:27:52 -05:00
|
|
|
export_initrd_dirent(&dirent);
|
2015-11-21 18:26:23 -05:00
|
|
|
assert((dataoff & (alignof(dirent)-1)) == 0 );
|
2012-03-23 14:36:51 -04:00
|
|
|
ssize_t hdramt = pwriteall(fd, &dirent, entsize, dataoff);
|
2015-01-19 10:27:52 -05:00
|
|
|
import_initrd_dirent(&dirent);
|
2012-03-23 14:36:51 -04:00
|
|
|
ssize_t nameamt = pwriteall(fd, name, namelen+1, dataoff + entsize);
|
|
|
|
if ( hdramt < (ssize_t) entsize || nameamt < (ssize_t) (namelen+1) )
|
2016-08-09 19:29:04 -04:00
|
|
|
return warn("write: %s", outputname), false;
|
2012-12-09 07:23:18 -05:00
|
|
|
size_t padding = dirent.reclen - (entsize + (namelen+1));
|
2015-11-21 18:26:23 -05:00
|
|
|
for ( size_t n = 0; n < padding; n++ )
|
2012-12-09 07:23:18 -05:00
|
|
|
{
|
|
|
|
uint8_t nul = 0;
|
2015-11-21 18:26:23 -05:00
|
|
|
if ( pwrite(fd, &nul, 1, dataoff+entsize+namelen+1+n) != 1 )
|
2016-08-09 19:29:04 -04:00
|
|
|
return warn("write: %s", outputname), false;
|
2012-12-09 07:23:18 -05:00
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
filesize += dirent.reclen;
|
|
|
|
dataoff += dirent.reclen;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
struct initrd_inode inode;
|
|
|
|
inode.mode = HostModeToInitRD(node->mode);
|
2015-08-01 07:52:53 -04:00
|
|
|
inode.uid = 0;
|
|
|
|
inode.gid = 0;
|
2012-03-23 14:36:51 -04:00
|
|
|
inode.nlink = node->nlink;
|
|
|
|
inode.ctime = (uint64_t) node->ctime;
|
|
|
|
inode.mtime = (uint64_t) node->mtime;
|
|
|
|
inode.dataoffset = filestart;
|
|
|
|
inode.size = filesize;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t inodepos = sb->inodeoffset + node->ino * sb->inodesize;
|
|
|
|
uint32_t inodesize = sizeof(inode);
|
2015-01-19 10:27:52 -05:00
|
|
|
export_initrd_inode(&inode);
|
2015-11-21 18:26:23 -05:00
|
|
|
assert((inodepos & (alignof(inode)-1)) == 0 );
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( pwriteall(fd, &inode, inodesize, inodepos) < inodesize )
|
2016-08-09 19:29:04 -04:00
|
|
|
return warn("write: %s", outputname), false;
|
2015-01-19 10:27:52 -05:00
|
|
|
import_initrd_inode(&inode);
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t increment = dataoff - origfssize;
|
|
|
|
sb->fssize += increment;
|
2015-11-21 18:26:23 -05:00
|
|
|
sb->fssize = (sb->fssize+7)/8*8; // Align upwards.
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-12-09 07:23:18 -05:00
|
|
|
return node->written = true;
|
2012-03-23 14:36:51 -04:00
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
bool WriteNodeRecursive(struct initrd_superblock* sb, int fd,
|
2016-02-28 17:03:16 -05:00
|
|
|
const char* outputname, struct Node* node)
|
2012-03-23 14:36:51 -04:00
|
|
|
{
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !WriteNode(sb, fd, outputname, node) )
|
|
|
|
return false;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !S_ISDIR(node->mode) )
|
|
|
|
return true;
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
for ( size_t i = 0; i < node->direntsused; i++ )
|
|
|
|
{
|
2016-02-28 17:03:16 -05:00
|
|
|
struct DirEntry* entry = node->dirents + i;
|
2012-03-23 14:36:51 -04:00
|
|
|
const char* name = entry->name;
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* child = entry->node;
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !strcmp(name, ".") || !strcmp(name, ".." ) )
|
|
|
|
continue;
|
|
|
|
if ( !WriteNodeRecursive(sb, fd, outputname, child) )
|
|
|
|
return false;
|
2012-03-23 14:36:51 -04:00
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
bool FormatFD(const char* outputname, int fd, uint32_t inodecount,
|
|
|
|
struct Node* root)
|
2012-03-23 14:36:51 -04:00
|
|
|
{
|
|
|
|
struct initrd_superblock sb;
|
|
|
|
memset(&sb, 0, sizeof(sb));
|
|
|
|
strncpy(sb.magic, "sortix-initrd-2", sizeof(sb.magic));
|
|
|
|
sb.revision = 0;
|
|
|
|
sb.fssize = sizeof(sb);
|
2016-02-28 17:03:16 -05:00
|
|
|
sb.inodesize = sizeof(struct initrd_inode);
|
2012-03-23 14:36:51 -04:00
|
|
|
sb.inodeoffset = sizeof(sb);
|
|
|
|
sb.inodecount = inodecount;
|
|
|
|
sb.root = root->ino;
|
|
|
|
|
|
|
|
uint32_t inodebytecount = sb.inodesize * sb.inodecount;
|
|
|
|
sb.fssize += inodebytecount;
|
2015-11-21 18:26:23 -05:00
|
|
|
sb.fssize = (sb.fssize+7)/8*8; // Align upwards.
|
2012-03-23 14:36:51 -04:00
|
|
|
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !WriteNodeRecursive(&sb, fd, outputname, root) )
|
|
|
|
return false;
|
2012-03-23 14:36:51 -04:00
|
|
|
|
2016-04-10 09:19:12 -04:00
|
|
|
sb.sumalgorithm = INITRD_ALGO_NONE;
|
|
|
|
sb.sumsize = 0;
|
2015-11-21 18:26:23 -05:00
|
|
|
sb.fssize = (sb.fssize+3)/4*4; // Align upwards.
|
2012-03-23 14:36:51 -04:00
|
|
|
sb.fssize += sb.sumsize;
|
|
|
|
|
2015-01-19 10:27:52 -05:00
|
|
|
export_initrd_superblock(&sb);
|
2012-03-23 14:36:51 -04:00
|
|
|
if ( pwriteall(fd, &sb, sizeof(sb), 0) < sizeof(sb) )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("write: %s", outputname);
|
2012-03-23 14:36:51 -04:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-19 10:27:52 -05:00
|
|
|
import_initrd_superblock(&sb);
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2015-11-21 18:26:23 -05:00
|
|
|
if ( ftruncate(fd, sb.fssize) < 0 )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warn("truncate: %s", outputname);
|
2015-11-21 18:26:23 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2016-02-28 17:03:16 -05:00
|
|
|
bool Format(const char* pathname, uint32_t inodecount, struct Node* root)
|
2012-03-23 14:36:51 -04:00
|
|
|
{
|
|
|
|
int fd = open(pathname, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
2016-02-28 17:03:16 -05:00
|
|
|
bool result = FormatFD(pathname, fd, inodecount, root);
|
2012-03-23 14:36:51 -04:00
|
|
|
close(fd);
|
|
|
|
return result;
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2014-04-23 08:46:09 -04:00
|
|
|
static void compact_arguments(int* argc, char*** argv)
|
2012-03-23 14:36:51 -04:00
|
|
|
{
|
2014-04-23 08:46:09 -04:00
|
|
|
for ( int i = 0; i < *argc; i++ )
|
|
|
|
{
|
|
|
|
while ( i < *argc && !(*argv)[i] )
|
|
|
|
{
|
|
|
|
for ( int n = i; n < *argc; n++ )
|
|
|
|
(*argv)[n] = (*argv)[n+1];
|
|
|
|
(*argc)--;
|
|
|
|
}
|
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2014-04-23 08:46:09 -04:00
|
|
|
bool get_option_variable(const char* option, char** varptr,
|
|
|
|
const char* arg, int argc, char** argv, int* ip,
|
|
|
|
const char* argv0)
|
2012-03-23 14:36:51 -04:00
|
|
|
{
|
2014-04-23 08:46:09 -04:00
|
|
|
size_t option_len = strlen(option);
|
|
|
|
if ( strncmp(option, arg, option_len) != 0 )
|
|
|
|
return false;
|
|
|
|
if ( arg[option_len] == '=' )
|
|
|
|
{
|
|
|
|
*varptr = strdup(arg + option_len + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( arg[option_len] != '\0' )
|
|
|
|
return false;
|
|
|
|
if ( *ip + 1 == argc )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: expected operand after `%s'\n", argv0, option);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
*varptr = strdup(argv[++*ip]), argv[*ip] = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GET_OPTION_VARIABLE(str, varptr) \
|
|
|
|
get_option_variable(str, varptr, arg, argc, argv, &i, argv0)
|
|
|
|
|
|
|
|
static void help(FILE* fp, const char* argv0)
|
|
|
|
{
|
2015-07-24 22:04:34 -04:00
|
|
|
fprintf(fp, "Usage: %s [OPTION]... ROOT... -o OUTPUT\n", argv0);
|
2014-04-23 08:46:09 -04:00
|
|
|
fprintf(fp, "Creates a init ramdisk for the Sortix kernel.\n");
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fprintf(fp, "Mandatory arguments to long options are mandatory for short options too.\n");
|
|
|
|
fprintf(fp, " --filter=FILE import filter rules from FILE\n");
|
2014-04-23 11:53:19 -04:00
|
|
|
fprintf(fp, " --format=FORMAT format version [%s]\n", DEFAULT_FORMAT);
|
2014-04-23 08:46:09 -04:00
|
|
|
fprintf(fp, " -o, --output=FILE write result to FILE\n");
|
|
|
|
fprintf(fp, " --help display this help and exit\n");
|
|
|
|
fprintf(fp, " --version output version information and exit\n");
|
2012-03-23 14:36:51 -04:00
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2014-04-23 08:46:09 -04:00
|
|
|
static void version(FILE* fp, const char* argv0)
|
2012-03-23 14:36:51 -04:00
|
|
|
{
|
2014-04-23 08:46:09 -04:00
|
|
|
fprintf(fp, "%s (Sortix) %s\n", argv0, VERSIONSTR);
|
2012-03-23 14:36:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2014-04-23 08:46:09 -04:00
|
|
|
char* arg_filter = NULL;
|
2014-04-23 11:53:19 -04:00
|
|
|
char* arg_format = strdup(DEFAULT_FORMAT);
|
2015-12-27 20:04:01 -05:00
|
|
|
char* arg_manifest = NULL;
|
2014-04-23 08:46:09 -04:00
|
|
|
char* arg_output = NULL;
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
const char* argv0 = argv[0];
|
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
|
|
|
const char* arg = argv[i];
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( arg[0] != '-' || !arg[1] )
|
|
|
|
continue;
|
2012-03-23 14:36:51 -04:00
|
|
|
argv[i] = NULL;
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !strcmp(arg, "--") )
|
|
|
|
break;
|
|
|
|
if ( arg[1] != '-' )
|
2012-03-23 14:36:51 -04:00
|
|
|
{
|
2016-02-28 17:03:16 -05:00
|
|
|
char c;
|
|
|
|
while ( (c = *++arg) ) switch ( c )
|
2011-08-27 08:50:52 -04:00
|
|
|
{
|
2014-04-23 08:46:09 -04:00
|
|
|
case 'o':
|
|
|
|
free(arg_output);
|
|
|
|
if ( *(arg+1) )
|
|
|
|
arg_output = strdup(arg + 1);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( i + 1 == argc )
|
|
|
|
{
|
2016-08-09 19:29:04 -04:00
|
|
|
warnx("option requires an argument -- 'o'");
|
2014-04-23 08:46:09 -04:00
|
|
|
fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
|
|
|
|
exit(125);
|
|
|
|
}
|
|
|
|
arg_output = strdup(argv[i+1]);
|
|
|
|
argv[++i] = NULL;
|
|
|
|
}
|
|
|
|
arg = "o";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: unknown option -- '%c'\n", argv0, c);
|
|
|
|
help(stderr, argv0);
|
2012-03-23 14:36:51 -04:00
|
|
|
exit(1);
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
|
|
|
}
|
2014-04-23 08:46:09 -04:00
|
|
|
else if ( !strcmp(arg, "--help") )
|
|
|
|
help(stdout, argv0), exit(0);
|
|
|
|
else if ( !strcmp(arg, "--version") )
|
|
|
|
version(stdout, argv0), exit(0);
|
|
|
|
else if ( GET_OPTION_VARIABLE("--filter", &arg_filter) )
|
2013-01-29 14:01:46 -05:00
|
|
|
{
|
2014-04-23 08:46:09 -04:00
|
|
|
FILE* fp = fopen(arg_filter, "r");
|
2013-01-29 14:01:46 -05:00
|
|
|
if ( !fp )
|
2016-08-09 19:29:04 -04:00
|
|
|
err(1, "%s", arg_filter);
|
2016-02-28 17:03:16 -05:00
|
|
|
if ( !AddRulesFromFile(fp, arg_filter) )
|
2013-01-29 14:01:46 -05:00
|
|
|
exit(1);
|
|
|
|
fclose(fp);
|
2014-04-23 08:46:09 -04:00
|
|
|
free(arg_filter);
|
|
|
|
arg_filter = NULL;
|
|
|
|
}
|
2015-12-27 20:04:01 -05:00
|
|
|
else if ( GET_OPTION_VARIABLE("--manifest", &arg_manifest) )
|
|
|
|
{
|
|
|
|
FILE* fp = fopen(arg_manifest, "r");
|
|
|
|
if ( !fp )
|
2016-08-09 19:29:04 -04:00
|
|
|
err(1, "%s", arg_manifest);
|
2016-02-28 17:03:16 -05:00
|
|
|
if ( !AddManifestFromFile(fp, arg_manifest) )
|
2015-12-27 20:04:01 -05:00
|
|
|
exit(1);
|
|
|
|
fclose(fp);
|
|
|
|
free(arg_manifest);
|
|
|
|
arg_manifest = NULL;
|
|
|
|
}
|
2014-04-23 11:53:19 -04:00
|
|
|
else if ( GET_OPTION_VARIABLE("--format", &arg_format) ) { }
|
2014-04-23 08:46:09 -04:00
|
|
|
else if ( GET_OPTION_VARIABLE("--output", &arg_output) ) { }
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: unknown option: %s\n", argv0, arg);
|
|
|
|
help(stderr, argv0);
|
|
|
|
exit(1);
|
2013-01-29 14:01:46 -05:00
|
|
|
}
|
2014-04-23 08:46:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( argc < 2 )
|
|
|
|
{
|
|
|
|
help(stdout, argv0);
|
2012-03-23 14:36:51 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
2011-08-27 08:50:52 -04:00
|
|
|
|
2014-04-23 08:46:09 -04:00
|
|
|
compact_arguments(&argc, &argv);
|
|
|
|
|
|
|
|
if ( argc < 2 )
|
2012-03-23 14:36:51 -04:00
|
|
|
{
|
2014-04-23 08:46:09 -04:00
|
|
|
fprintf(stderr, "%s: No root specified\n", argv0),
|
|
|
|
help(stderr, argv0);
|
|
|
|
exit(1);
|
2011-08-27 08:50:52 -04:00
|
|
|
}
|
|
|
|
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !arg_output )
|
2011-12-01 15:44:11 -05:00
|
|
|
{
|
2014-04-23 08:46:09 -04:00
|
|
|
fprintf(stderr, "%s: No output file specified\n", argv0),
|
|
|
|
help(stderr, argv0);
|
2012-03-23 14:36:51 -04:00
|
|
|
exit(1);
|
2011-12-01 15:44:11 -05:00
|
|
|
}
|
|
|
|
|
2014-04-23 11:53:19 -04:00
|
|
|
const char* format = arg_format;
|
|
|
|
|
|
|
|
if ( !strcmp(format, "default") )
|
|
|
|
format = DEFAULT_FORMAT;
|
|
|
|
|
|
|
|
if ( strcmp(format, "sortix-initrd-2") != 0 )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Unsupported format `%s'\n", argv0, format);
|
|
|
|
fprintf(stderr, "Try `%s --help' for more information.\n", argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-03-23 14:36:51 -04:00
|
|
|
uint32_t inodecount = 1;
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* root = NULL;
|
2015-07-24 22:04:34 -04:00
|
|
|
for ( int i = 1; i < argc; i++ )
|
|
|
|
{
|
2016-02-28 17:03:16 -05:00
|
|
|
struct Node* node = RecursiveSearch(argv[i], "/", &inodecount, NULL);
|
2015-07-24 22:04:34 -04:00
|
|
|
if ( !node )
|
|
|
|
exit(1);
|
|
|
|
if ( root )
|
|
|
|
root = MergeNodes(root, node);
|
|
|
|
else
|
|
|
|
root = node;
|
|
|
|
if ( !root )
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-03-23 14:36:51 -04:00
|
|
|
|
2014-04-23 08:46:09 -04:00
|
|
|
if ( !Format(arg_output, inodecount, root) )
|
|
|
|
exit(1);
|
2012-03-23 14:36:51 -04:00
|
|
|
|
|
|
|
FreeNode(root);
|
|
|
|
|
2011-08-27 08:50:52 -04:00
|
|
|
return 0;
|
|
|
|
}
|