Initial Contribution

This commit is contained in:
The Android Open Source Project
2008-10-21 07:00:00 -07:00
commit b6c1cf6de7
384 changed files with 648683 additions and 0 deletions

20
tools/atree/Android.mk Normal file
View File

@@ -0,0 +1,20 @@
# Copyright 2007 The Android Open Source Project
#
# Copies files into the directory structure described by a manifest
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
atree.cpp \
files.cpp \
fs.cpp
LOCAL_STATIC_LIBRARIES := \
libhost
LOCAL_C_INCLUDES := build/libs/host/include
LOCAL_MODULE := atree
include $(BUILD_HOST_EXECUTABLE)

274
tools/atree/atree.cpp Normal file
View File

@@ -0,0 +1,274 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "options.h"
#include "files.h"
#include "fs.h"
#include <set>
using namespace std;
bool g_debug = false;
vector<string> g_listFiles;
vector<string> g_inputBases;
string g_outputBase;
string g_dependency;
bool g_useHardLinks = false;
const char* USAGE =
"\n"
"Usage: atree OPTIONS\n"
"\n"
"Options:\n"
" -f FILELIST Specify one or more files containing the\n"
" list of files to copy.\n"
" -I INPUTDIR Specify one or more base directories in\n"
" which to look for the files\n"
" -o OUTPUTDIR Specify the directory to copy all of the\n"
" output files to.\n"
" -l Use hard links instead of copying the files.\n"
" -m DEPENDENCY Output a make-formatted file containing the list.\n"
" of files included. It sets the variable ATREE_FILES.\n"
"\n"
"FILELIST file format:\n"
" The FILELIST files contain the list of files that will end up\n"
" in the final OUTPUTDIR. Atree will look for files in the INPUTDIR\n"
" directories in the order they are specified.\n"
"\n"
" In a FILELIST file, comment lines start with a #. Other lines\n"
" are of the format:\n"
"\n"
" DEST\n"
" SRC DEST\n"
" -SRCPATTERN\n"
"\n"
" DEST should be path relative to the output directory.\n"
" If SRC is supplied, the file names can be different.\n"
" SRCPATTERN is a pattern for the filenames.\n"
"\n";
int usage()
{
fwrite(USAGE, strlen(USAGE), 1, stderr);
return 1;
}
int
main(int argc, char* const* argv)
{
int err;
bool done = false;
while (!done) {
int opt = getopt(argc, argv, "f:I:o:hlm:");
switch (opt)
{
case -1:
done = true;
break;
case 'f':
g_listFiles.push_back(string(optarg));
break;
case 'I':
g_inputBases.push_back(string(optarg));
break;
case 'o':
if (g_outputBase.length() != 0) {
fprintf(stderr, "%s: -o may only be supplied once -- "
"-o %s\n", argv[0], optarg);
return usage();
}
g_outputBase = optarg;
break;
case 'l':
g_useHardLinks = true;
break;
case 'm':
if (g_dependency.length() != 0) {
fprintf(stderr, "%s: -m may only be supplied once -- "
"-m %s\n", argv[0], optarg);
return usage();
}
g_dependency = optarg;
break;
default:
case '?':
case 'h':
return usage();
}
}
if (optind != argc) {
fprintf(stderr, "%s: invalid argument -- %s\n", argv[0], argv[optind]);
return usage();
}
if (g_listFiles.size() == 0) {
fprintf(stderr, "%s: At least one -f option must be supplied.\n",
argv[0]);
return usage();
}
if (g_inputBases.size() == 0) {
fprintf(stderr, "%s: At least one -I option must be supplied.\n",
argv[0]);
return usage();
}
if (g_outputBase.length() == 0) {
fprintf(stderr, "%s: -o option must be supplied.\n", argv[0]);
return usage();
}
#if 0
for (vector<string>::iterator it=g_listFiles.begin();
it!=g_listFiles.end(); it++) {
printf("-f \"%s\"\n", it->c_str());
}
for (vector<string>::iterator it=g_inputBases.begin();
it!=g_inputBases.end(); it++) {
printf("-I \"%s\"\n", it->c_str());
}
printf("-o \"%s\"\n", g_outputBase.c_str());
if (g_useHardLinks) {
printf("-l\n");
}
#endif
vector<FileRecord> files;
vector<FileRecord> more;
vector<string> excludes;
set<string> directories;
set<string> deleted;
// read file lists
for (vector<string>::iterator it=g_listFiles.begin();
it!=g_listFiles.end(); it++) {
err = read_list_file(*it, &files, &excludes);
if (err != 0) {
return err;
}
}
// look for input files
err = 0;
for (vector<FileRecord>::iterator it=files.begin();
it!=files.end(); it++) {
err |= locate(&(*it), g_inputBases);
}
// expand the directories that we should copy into a list of files
for (vector<FileRecord>::iterator it=files.begin();
it!=files.end(); it++) {
if (it->sourceIsDir) {
err |= list_dir(*it, excludes, &more);
}
}
for (vector<FileRecord>::iterator it=more.begin();
it!=more.end(); it++) {
files.push_back(*it);
}
// get the name and modtime of the output files
for (vector<FileRecord>::iterator it=files.begin();
it!=files.end(); it++) {
stat_out(g_outputBase, &(*it));
}
if (err != 0) {
return 1;
}
// gather directories
for (vector<FileRecord>::iterator it=files.begin();
it!=files.end(); it++) {
if (it->sourceIsDir) {
directories.insert(it->outPath);
} else {
string s = dir_part(it->outPath);
if (s != ".") {
directories.insert(s);
}
}
}
// gather files that should become directores and directories that should
// become files
for (vector<FileRecord>::iterator it=files.begin();
it!=files.end(); it++) {
if (it->outMod != 0 && it->sourceIsDir != it->outIsDir) {
deleted.insert(it->outPath);
}
}
// delete files
for (set<string>::iterator it=deleted.begin();
it!=deleted.end(); it++) {
if (g_debug) {
printf("deleting %s\n", it->c_str());
}
err = remove_recursively(*it);
if (err != 0) {
return err;
}
}
// make directories
for (set<string>::iterator it=directories.begin();
it!=directories.end(); it++) {
if (g_debug) {
printf("mkdir %s\n", it->c_str());
}
err = mkdir_recursively(*it);
if (err != 0) {
return err;
}
}
// copy (or link) files
for (vector<FileRecord>::iterator it=files.begin();
it!=files.end(); it++) {
if (!it->sourceIsDir) {
if (g_debug) {
printf("copy %s(%ld) ==> %s(%ld)", it->sourcePath.c_str(),
it->sourceMod, it->outPath.c_str(), it->outMod);
fflush(stdout);
}
if (it->outMod < it->sourceMod) {
err = copy_file(it->sourcePath, it->outPath);
if (g_debug) {
printf(" done.\n");
}
if (err != 0) {
return err;
}
} else {
if (g_debug) {
printf(" skipping.\n");
}
}
}
}
// output the dependency file
if (g_dependency.length() != 0) {
FILE *f = fopen(g_dependency.c_str(), "w");
if (f != NULL) {
fprintf(f, "ATREE_FILES := $(ATREE_FILES) \\\n");
for (vector<FileRecord>::iterator it=files.begin();
it!=files.end(); it++) {
if (!it->sourceIsDir) {
fprintf(f, "%s \\\n", it->sourcePath.c_str());
}
}
fprintf(f, "\n");
fclose(f);
} else {
fprintf(stderr, "error opening manifest file for write: %s\n",
g_dependency.c_str());
}
}
return 0;
}

357
tools/atree/files.cpp Normal file
View File

@@ -0,0 +1,357 @@
#include "files.h"
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fnmatch.h>
static bool
is_comment_line(const char* p)
{
while (*p && isspace(*p)) {
p++;
}
return *p == '#';
}
static string
path_append(const string& base, const string& leaf)
{
string full = base;
if (base.length() > 0 && leaf.length() > 0) {
full += '/';
}
full += leaf;
return full;
}
static bool
is_whitespace_line(const char* p)
{
while (*p) {
if (!isspace(*p)) {
return false;
}
p++;
}
return true;
}
static bool
is_exclude_line(const char* p) {
while (*p) {
if (*p == '-') {
return true;
}
else if (isspace(*p)) {
p++;
}
else {
return false;
}
}
return false;
}
void
split_line(const char* p, vector<string>* out)
{
const char* q = p;
enum { WHITE, TEXT } state = WHITE;
while (*p) {
if (*p == '#') {
break;
}
switch (state)
{
case WHITE:
if (!isspace(*p)) {
q = p;
state = TEXT;
}
break;
case TEXT:
if (isspace(*p)) {
if (q != p) {
out->push_back(string(q, p-q));
}
state = WHITE;
}
break;
}
p++;
}
if (state == TEXT) {
out->push_back(string(q, p-q));
}
}
static void
add_file(vector<FileRecord>* files, const string& listFile, int listLine,
const string& sourceName, const string& outName)
{
FileRecord rec;
rec.listFile = listFile;
rec.listLine = listLine;
rec.sourceName = sourceName;
rec.outName = outName;
files->push_back(rec);
}
int
read_list_file(const string& filename, vector<FileRecord>* files,
vector<string>* excludes)
{
int err = 0;
FILE* f = NULL;
long size;
char* buf = NULL;
char *p, *q;
int i, lineCount;
f = fopen(filename.c_str(), "r");
if (f == NULL) {
fprintf(stderr, "Could not open list file (%s): %s\n",
filename.c_str(), strerror(errno));
err = errno;
goto cleanup;
}
err = fseek(f, 0, SEEK_END);
if (err != 0) {
fprintf(stderr, "Could not seek to the end of file %s. (%s)\n",
filename.c_str(), strerror(errno));
err = errno;
goto cleanup;
}
size = ftell(f);
err = fseek(f, 0, SEEK_SET);
if (err != 0) {
fprintf(stderr, "Could not seek to the beginning of file %s. (%s)\n",
filename.c_str(), strerror(errno));
err = errno;
goto cleanup;
}
buf = (char*)malloc(size+1);
if (buf == NULL) {
// (potentially large)
fprintf(stderr, "out of memory (%ld)\n", size);
err = ENOMEM;
goto cleanup;
}
if (1 != fread(buf, size, 1, f)) {
fprintf(stderr, "error reading file %s. (%s)\n",
filename.c_str(), strerror(errno));
err = errno;
goto cleanup;
}
// split on lines
p = buf;
q = buf+size;
lineCount = 0;
while (p<q) {
if (*p == '\r' || *p == '\n') {
*p = '\0';
lineCount++;
}
p++;
}
// read lines
p = buf;
for (i=0; i<lineCount; i++) {
int len = strlen(p);
q = p + len + 1;
if (is_whitespace_line(p) || is_comment_line(p)) {
;
}
else if (is_exclude_line(p)) {
while (*p != '-') p++;
p++;
excludes->push_back(string(p));
}
else {
vector<string> words;
split_line(p, &words);
#if 0
printf("[ ");
for (size_t k=0; k<words.size(); k++) {
printf("'%s' ", words[k].c_str());
}
printf("]\n");
#endif
if (words.size() == 1) {
// pattern: DEST
add_file(files, filename, i+1, words[0], words[0]);
}
else if (words.size() == 2) {
// pattern: SRC DEST
add_file(files, filename, i+1, words[0], words[1]);
}
else {
fprintf(stderr, "%s:%d: bad format: %s\n", filename.c_str(),
i+1, p);
err = 1;
}
}
p = q;
}
cleanup:
if (buf != NULL) {
free(buf);
}
if (f != NULL) {
fclose(f);
}
return err;
}
int
locate(FileRecord* rec, const vector<string>& search)
{
int err;
for (vector<string>::const_iterator it=search.begin();
it!=search.end(); it++) {
string full = path_append(*it, rec->sourceName);
struct stat st;
err = stat(full.c_str(), &st);
if (err == 0) {
rec->sourceBase = *it;
rec->sourcePath = full;
rec->sourceMod = st.st_mtime;
rec->sourceIsDir = S_ISDIR(st.st_mode);
return 0;
}
}
fprintf(stderr, "%s:%d: couldn't locate source file: %s\n",
rec->listFile.c_str(), rec->listLine, rec->sourceName.c_str());
return 1;
}
void
stat_out(const string& base, FileRecord* rec)
{
rec->outPath = path_append(base, rec->outName);
int err;
struct stat st;
err = stat(rec->outPath.c_str(), &st);
if (err == 0) {
rec->outMod = st.st_mtime;
rec->outIsDir = S_ISDIR(st.st_mode);
} else {
rec->outMod = 0;
rec->outIsDir = false;
}
}
string
dir_part(const string& filename)
{
int pos = filename.rfind('/');
if (pos <= 0) {
return ".";
}
return filename.substr(0, pos);
}
static void
add_more(const string& entry, bool isDir,
const FileRecord& rec, vector<FileRecord>*more)
{
FileRecord r;
r.listFile = rec.listFile;
r.listLine = rec.listLine;
r.sourceName = path_append(rec.sourceName, entry);
r.sourcePath = path_append(rec.sourceBase, r.sourceName);
struct stat st;
int err = stat(r.sourcePath.c_str(), &st);
if (err == 0) {
r.sourceMod = st.st_mtime;
}
r.sourceIsDir = isDir;
r.outName = path_append(rec.outName, entry);
more->push_back(r);
}
static bool
matches_excludes(const char* file, const vector<string>& excludes)
{
for (vector<string>::const_iterator it=excludes.begin();
it!=excludes.end(); it++) {
if (0 == fnmatch(it->c_str(), file, FNM_PERIOD)) {
return true;
}
}
return false;
}
static int
list_dir(const string& path, const FileRecord& rec,
const vector<string>& excludes,
vector<FileRecord>* more)
{
int err;
string full = path_append(rec.sourceBase, rec.sourceName);
full = path_append(full, path);
DIR *d = opendir(full.c_str());
if (d == NULL) {
return errno;
}
vector<string> dirs;
struct dirent *ent;
while (NULL != (ent = readdir(d))) {
if (0 == strcmp(".", ent->d_name)
|| 0 == strcmp("..", ent->d_name)) {
continue;
}
if (matches_excludes(ent->d_name, excludes)) {
continue;
}
string entry = path_append(path, ent->d_name);
#ifdef HAVE_DIRENT_D_TYPE
bool is_directory = (ent->d_type == DT_DIR);
#else
// If dirent.d_type is missing, then use stat instead
struct stat stat_buf;
stat(entry.c_str(), &stat_buf);
bool is_directory = S_ISDIR(stat_buf.st_mode);
#endif
add_more(entry, is_directory, rec, more);
if (is_directory) {
dirs.push_back(entry);
}
}
closedir(d);
for (vector<string>::iterator it=dirs.begin(); it!=dirs.end(); it++) {
list_dir(*it, rec, excludes, more);
}
return 0;
}
int
list_dir(const FileRecord& rec, const vector<string>& excludes,
vector<FileRecord>* files)
{
return list_dir("", rec, excludes, files);
}

36
tools/atree/files.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef FILES_H
#define FILES_H
#include <string>
#include <vector>
#include <sys/types.h>
using namespace std;
struct FileRecord
{
string listFile;
int listLine;
string sourceBase;
string sourceName;
string sourcePath;
bool sourceIsDir;
time_t sourceMod;
string outName;
string outPath;
time_t outMod;
bool outIsDir;
unsigned int mode;
};
int read_list_file(const string& filename, vector<FileRecord>* files,
vector<string>* excludes);
int locate(FileRecord* rec, const vector<string>& search);
void stat_out(const string& base, FileRecord* rec);
string dir_part(const string& filename);
int list_dir(const FileRecord& rec, const vector<string>& excludes,
vector<FileRecord>* files);
#endif // FILES_H

142
tools/atree/fs.cpp Normal file
View File

@@ -0,0 +1,142 @@
#include "fs.h"
#include "files.h"
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <host/CopyFile.h>
using namespace std;
static bool
is_dir(const string& path)
{
int err;
struct stat st;
err = stat(path.c_str(), &st);
return err != 0 || S_ISDIR(st.st_mode);
}
static int
remove_file(const string& path)
{
int err = unlink(path.c_str());
if (err != 0) {
fprintf(stderr, "error deleting file %s (%s)\n", path.c_str(),
strerror(errno));
return errno;
}
return 0;
}
int
remove_recursively(const string& path)
{
int err;
if (is_dir(path)) {
DIR *d = opendir(path.c_str());
if (d == NULL) {
fprintf(stderr, "error getting directory contents %s (%s)\n",
path.c_str(), strerror(errno));
return errno;
}
vector<string> files;
vector<string> dirs;
struct dirent *ent;
while (NULL != (ent = readdir(d))) {
if (0 == strcmp(".", ent->d_name)
|| 0 == strcmp("..", ent->d_name)) {
continue;
}
string full = path;
full += '/';
full += ent->d_name;
#ifdef HAVE_DIRENT_D_TYPE
bool is_directory = (ent->d_type == DT_DIR);
#else
// If dirent.d_type is missing, then use stat instead
struct stat stat_buf;
stat(full.c_str(), &stat_buf);
bool is_directory = S_ISDIR(stat_buf.st_mode);
#endif
if (is_directory) {
dirs.push_back(full);
} else {
files.push_back(full);
}
}
closedir(d);
for (vector<string>::iterator it=files.begin(); it!=files.end(); it++) {
err = remove_file(*it);
if (err != 0) {
return err;
}
}
for (vector<string>::iterator it=dirs.begin(); it!=dirs.end(); it++) {
err = remove_recursively(*it);
if (err != 0) {
return err;
}
}
err = rmdir(path.c_str());
if (err != 0) {
fprintf(stderr, "error deleting directory %s (%s)\n", path.c_str(),
strerror(errno));
return errno;
}
return 0;
} else {
return remove_file(path);
}
}
int
mkdir_recursively(const string& path)
{
int err;
size_t pos = 0;
while (true) {
pos = path.find('/', pos);
string p = path.substr(0, pos);
struct stat st;
err = stat(p.c_str(), &st);
if (err != 0) {
err = mkdir(p.c_str(), 0770);
if (err != 0) {
fprintf(stderr, "can't create directory %s (%s)\n",
path.c_str(), strerror(errno));
return errno;
}
}
else if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "can't create directory %s because %s is a file.\n",
path.c_str(), p.c_str());
return 1;
}
pos++;
if (p == path) {
return 0;
}
}
}
int
copy_file(const string& src, const string& dst)
{
int err;
err = copyFile(src.c_str(), dst.c_str(),
COPY_NO_DEREFERENCE | COPY_FORCE | COPY_PERMISSIONS);
return err;
}

12
tools/atree/fs.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef FS_H
#define FS_H
#include <string>
using namespace std;
int remove_recursively(const string& path);
int mkdir_recursively(const string& path);
int copy_file(const string& src, const string& dst);
#endif // FS_H

14
tools/atree/options.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef OPTIONS_H
#define OPTIONS_H
#include <string>
#include <vector>
using namespace std;
extern vector<string> g_listFiles;
extern vector<string> g_inputBases;
extern string g_outputBase;
extern bool g_useHardLinks;
#endif // OPTIONS_H