...
 
Commits (51)
This diff is collapsed.
/*
* Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This file is part of the turris updater.
*
* Updater is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Updater is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Updater. If not, see <http://www.gnu.org/licenses/>.
*/
/* TODO: change to path_exists */
int file_exists(const char *file);
int is_dir(const char *file);
/*
* Make directory with same attributes as 'src'
*/
int mkdir_from(const char *name, const char *src);
/*
* Return filename from path
*/
const char* get_filename(const char *path);
/*
* Construct path from SRC where first dir is replaced by DST
*/
int get_dst_path(const char *src, const char *dst, char *path);
/*
* Make full path from src path and dst name
*/
int get_full_dst(const char *src, const char *dst, char *fulldst);
int path_length(const char *dir, const char *file);
int make_path(const char *dir, const char *file, char *path);
/* ------ */
/* --- MAIN FUNC --- */
struct tree_funcs {
int (*file_func)(const char *);
int (*link_func)(const char *);
int (*dir_func)(const char *, int);
};
int ff_success;
int foreach_file_inner (const char * dir_name, struct tree_funcs funcs);
int foreach_file(const char *dirname, struct tree_funcs funcs);
/* --- PRINT TREE --- */
int dir_depth = 0;
const char *dir_prefix = "--------------------"; /* max allowed depth is 20 dirs, enough for testing */
char prefix[20];
int print_file(const char *name);
int print_dir(const char *name, int type);
struct tree_funcs print_tree = {
print_file,
print_file,
print_dir
};
int tree(const char *name);
/* --- REMOVE FILE/DIR --- */
int rm_file(const char *name);
int rm_link(const char *name);
int rm_dir(const char *name, int type);
struct tree_funcs rm_tree = {
rm_file,
rm_file,
rm_dir
};
int rm(const char *name);
/* --- COPY/MOVE FILE/DIR --- */
char file_dst_path[PATH_MAX];
int do_cp_file(const char *src, const char *dst);
int cp_file(const char *name);
int cp_dir(const char *name, int type);
struct tree_funcs cp_tree = {
cp_file,
cp_file,
cp_dir
};
int mv_file(const char *name);
int mv_dir(const char *name, int type);
struct tree_funcs mv_tree = {
mv_file,
mv_file,
mv_dir
};
int cpmv(const char *src, const char *dst, int move);
int cp(const char *src, const char *dst);
int mv(const char *src, const char *dst);
/* --- FIND FILE --- */
char find_name[PATH_MAX];
char found_name[PATH_MAX];
int find_file(const char *name);
int find_dir(const char *name, int type);
struct tree_funcs find_tree = {
find_file,
find_file,
find_dir
};
const char* find(const char *where, const char *what, char *found_name);
This diff is collapsed.
This diff is collapsed.
rm -rf dir
rm -rf newdir
rm -rf cpdir
rm -rf mvdir
rm file
touch file
echo test > file
mkdir dir
mkdir dir/subdir1
touch dir/file1
echo file1 > dir/file1
touch dir/file2
echo file2 > dir/file2
touch dir/subdir1/fileA
echo fileA > dir/subdir1/fileA
touch dir/file_to_find
echo kuku > dir/file_to_find
#! /bin/sh
# gcc -g -O0 -o rls ls.c
gcc -g -O0 -o rls ../src/lib/file-funcs.c
rm -rf dir
./makefiles.sh
# ./rls dir
# valgrind --log-file=valgrind.log --leak-check=full --track-origins=yes --show-reachable=yes ./rls dir
valgrind -v --leak-check=full --track-origins=yes --show-reachable=yes ./rls dir
......@@ -9,6 +9,7 @@ C_TESTS := \
arguments \
events \
util \
files \
subprocess \
interpreter
......
/*
* Copyright 2019, CZ.NIC z.s.p.o. (http://www.nic.cz/)
*
* This file is part of the turris updater.
*
* Updater is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Updater is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Updater. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ctest.h"
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "../src/lib/interpreter.h"
#include "../src/lib/file-funcs.h"
const char *test_file = "../testdir/test";
const char *test_file2 = "../testdir/test2";
const char *dst_file = "../testdir/test-copy";
const char *dst_dir = "../testdir/dst-dir";
const char *dst_dir2 = "../testdir/dst-dir2";
int generate_file(const char *path, int length) {
int f_path = open(path, O_WRONLY | O_CREAT | O_EXCL, 0777);
write(f_path, "abcdefgh", length);
return 0;
}
/* TODO: this is not test, make a function */
START_TEST(init) {
/* cleanup */
system("rm -rf ../testdir/*");
/* make files and dirs */
generate_file(test_file, 5);
generate_file(test_file2, 7);
mkdir(dst_dir, 0777);
mkdir(dst_dir2, 0777);
}
END_TEST
START_TEST(file_exist) {
printf ("file_exists returned %d\n", file_exists(test_file));
}
END_TEST
START_TEST(remove_file) {
int ret = rm_file(test_file);
ck_assert_int_eq(ret, 0);
}
END_TEST
START_TEST(copy_file) {
int ret;
/* copy file to new file */
ret = cp(test_file, dst_file);
ck_assert_int_eq(ret, 0);
/* copy file over existing file */
ret = cp(test_file2, dst_file);
ck_assert_int_eq(ret, 0);
/* copy file over itself (should fail) */
ret = cp(test_file, test_file);
ck_assert_int_eq(ret, -1);
/* copy file to directory */
ret = cp(test_file, dst_dir);
ck_assert_int_eq(ret, 0);
/* copy directory to directory */
}
END_TEST
/*
ck_assert();
ck_assert_int_eq();
*/
Suite *gen_test_suite(void) {
Suite *result = suite_create("File");
TCase *file = tcase_create("file");
tcase_set_timeout(file, 30);
tcase_add_test(file, init);
tcase_add_test(file, file_exist);
tcase_add_test(file, copy_file);
tcase_add_test(file, remove_file);
suite_add_tcase(result, file);
return result;
}
......@@ -33,7 +33,7 @@ set -e
export TMPDIR="$O/testdir"
mkdir -p "$TMPDIR"
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' SIGHUP SIGINT SIGQUIT SIGABRT SIGALRM SIGTERM SIGILL SIGPIPE EXIT
# trap 'rm -rf "$TMP_DIR"' SIGHUP SIGINT SIGQUIT SIGABRT SIGALRM SIGTERM SIGILL SIGPIPE EXIT
export ROOT_DIR="$TMP_DIR"/root
DEFINITION="$2"
DATA_DIR="$S"/tests/data
......