Пример #1
0
void DirectoryArchive::forEachFile (VisitorFunc visitor, const std::string& root)
{
	std::vector<Directory*> dirs;
	UnixPath path(m_root);
	path.push(root);
	dirs.push_back(directory_open(path));

	while (!dirs.empty() && directory_good(dirs.back())) {
		const char *name = directory_read_and_increment(dirs.back());

		if (name == 0) {
			directory_close(dirs.back());
			dirs.pop_back();
			path.pop();
		} else if (!string_equal(name, ".") && !string_equal(name, "..")) {
			path.push_filename(name);

			bool is_directory = file_is_directory(path);

			if (!is_directory)
				visitor.file(os::makeRelative(path, m_root));

			path.pop();

			if (is_directory) {
				path.push(name);

				if (!visitor.directory(os::makeRelative(path, m_root), dirs.size()))
					dirs.push_back(directory_open(path));
				else
					path.pop();
			}
		}
	}
}
Пример #2
0
bool test_file_directory_find_invalid_argument (Test *test)
{
	char *path;
	Directory *directory;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/find"));
        /*
                d stage/find
                f f1
                d stage/find/d1
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (directory_find_file (NULL, "file"));
	CATCH (error_count () != 1);
	CATCH (error_at (0).error != ErrorInvalidArgument);
	error_reset ();
	CATCH (directory_find_directory (NULL, "directory"));
	CATCH (error_count () != 1);
	CATCH (error_at (0).error != ErrorInvalidArgument);
	error_reset ();
	CATCH (directory_find_file (directory, NULL));
	CATCH (error_count () != 1);
	CATCH (error_at (0).error != ErrorInvalidArgument);
	error_reset ();
	CATCH (directory_find_directory (directory, NULL));
	CATCH (error_count () != 1);
	CATCH (error_at (0).error != ErrorInvalidArgument);
	error_reset ();
	directory_close (directory);
	PASS ();
}
Пример #3
0
bool test_file_directory_read_4 (Test *test)
{
	char *path;
	Directory *directory;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/9d_9f"));
        /*
                d stage/9d_9f/d8
                d stage/9d_9f/d7
                d stage/9d_9f/d1
                d stage/9d_9f/d4
                d stage/9d_9f/d9
                d stage/9d_9f/d2
                d stage/9d_9f/d5
                d stage/9d_9f/d3
                d stage/9d_9f/d6
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!directory->directories);
	CATCH (!directory->files);
	CATCH (directory->directories->count != 9);
	CATCH (directory->files->count != 9);
	directory_close (directory);
	PASS ();
}
Пример #4
0
bool test_file_directory_read_3 (Test *test)
{
	char *path;
	Directory *directory;
	List *directories;
	List *files;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/1d_1f"));
        /*
                d stage/1d_1f
                f f1
                d stage/1d_1f/d1
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!directory->directories);
	CATCH (!directory->files);
	CATCH (directory->directories->count != 1);
	CATCH (directory->files->count != 1);
	directories = directory->directories;
	files = directory->files;
	CATCH (!directory_read (directory));
	CATCH (directories != directory->directories);
	CATCH (files != directory->files);
	directory_close (directory);
	PASS ();
}
Пример #5
0
bool test_file_directory_find (Test *test)
{
	char *path;
	Directory *directory;
	Directory *found_directory;
	File *found_file;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/find"));
        /*
                d stage/find
                f f1
                d stage/find/d1
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!directory->directories);
	CATCH (!directory->files);
	CATCH (directory->directories->count != 1);
	CATCH (directory->files->count != 1);
	CATCH (!(found_directory = directory_find_directory (directory, "d1")));
	CATCH (!string_equals (found_directory->name, "d1"));
	CATCH (directory_find_directory (directory, "d2"));
	CATCH (!(found_file = directory_find_file (directory, "f1")));
	CATCH (!string_equals (found_file->name, "f1"));
	CATCH (directory_find_file (directory, "f2"));
	directory_close (directory);
	PASS ();
}
Пример #6
0
bool test_file_directory_open_system_call (Test *test)
{
	TITLE ();
	CATCH (directory_open ("/dev/null/fail"));
	CATCH (error_count () == 0);
	CATCH (error_at (0).error != ErrorSystemCall);
	PASS ();	
}
Пример #7
0
bool test_file_directory_open_invalid_operation (Test *test)
{
	TITLE ();
	CATCH (directory_open (NULL));
	CATCH (error_count () == 0);
	CATCH (error_at (0).error != ErrorInvalidOperation);
	PASS ();	
}
Пример #8
0
bool test_file_directory_open_function_call_1 (Test *test)
{
	TITLE ();
	memory_commit_limit (sizeof (size_t) + sizeof (Directory) - 1);
	CATCH (directory_open ("/"));
	CATCH (error_count () != 2);
	CATCH (error_at (0).error != ErrorFunctionCall);
	CATCH (error_at (0).code != 1);
	CATCH (error_at (1).error != ErrorMemoryCommitLimit);
	PASS ();	
}
Пример #9
0
void screen_init_device()
{
	struct Directory* devfs = devfs_get_root();
	struct DirectoryHandle* handle = directory_open(devfs, HANDLE_WRITE);
	struct File* screen = file_create_node("screen", STATIC, &screen_ops);

	if(handle)
	{
		if(screen)
			directory_add_node(handle, screen);

		directory_close(handle);
	}
}
Пример #10
0
bool test_file_directory_read_1 (Test *test)
{
	char *path;
	Directory *directory;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/empty"));
        /* d stage/empty */
	CATCH (!(directory = directory_open (path)));
	CATCH (!directory_read (directory));
	CATCH (!directory->directories);
	CATCH (!directory->files);
	directory_close (directory);
	string_destroy (path);
	PASS ();
}
Пример #11
0
bool test_file_directory_open (Test *test)
{
	char *path;
	Directory *directory;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!(directory = directory_open (path)));
	CATCH (!string_equals (directory->path, path));
	CATCH (!string_equals (directory->name, "app.test.lib.core"));
	CATCH (directory->directories);
	CATCH (directory->files);
	CATCH (directory->modified == 0);
	directory_close (directory);
	string_destroy (path);
	PASS ();
}
Пример #12
0
CompileProject *compile_project_create (const char *path)
{
	CompileProject *project = NULL;

	if (!file_path_is_valid (path)) {
		compile_print ("The path is not valid: %s\n", path);
                error_code (FunctionCall, 1);
		return NULL;
	}
	if (!(project = memory_create (sizeof (CompileProject)))) {
                error_code (FunctionCall, 2);
		return NULL;
	}
	if (!(project->topological = topological_create ())) {
		compile_project_destroy (project);
                error_code (FunctionCall, 3);
		return NULL;
	}
	if (!(project->nodes = list_create ())) {
		compile_project_destroy (project);
                error_code (FunctionCall, 4);
		return NULL;
	}
	if (!(project->directory_to_compile = tree_create ())) {
		compile_project_destroy (project);
                error_code (FunctionCall, 5);
		return NULL;
	}
	if (!(project->directory = directory_open (path))) {
		compile_print ("Failed to open the directory: %s\n", path);
		compile_project_destroy (project);
                error_code (FunctionCall, 6);
		return NULL;
	}
	if (!string_begins_with (project->directory->name, "project.")) {
		compile_print ("The directory name must begin with 'project.'.\n");
		compile_project_destroy (project);
                error_code (FunctionCall, 7);
		return NULL;
	}
	project->sorted = NULL;
	return project;
}
Пример #13
0
bool test_file_close (Test *test)
{
	Directory *directory;
	File *file;
	char *path;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/open"));
        /*
                d stage/open
                f f1
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!(file = directory_find_file (directory, "f1")));
	file_close (file);
	directory_close (directory);
	PASS ();
}
Пример #14
0
bool test_file_readline_f2 (Test *test)
{
	Directory *directory;
	File *file;
	char *path;
	char *line;
	size_t bytes_read;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/readline"));
        /*
                d stage/readline
                f f2
                f f3 \
                         \
                        0 \
                        AB \
                        012 \
                        ABCD \
                        01234 \
                        ABCD \
                        012 \
                        AB \
                        0
                f f1
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!(file = directory_find_file (directory, "f2")));
	CATCH (!file_open (file));
	CATCH (!(line = string_create_with_size (1)));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 0);
	CATCH (memory_size (line) != 1);
	directory_close (directory);
	string_destroy (line);
	PASS ();
}
Пример #15
0
int fs_file_walker(int (*func)(int type, const char *filename, void *arg),
                   void *arg)
{
    int ret = SF_OK;
    struct directory *d;

    if (sf_list_cnt(&fs.directories) == 0) {
        return SF_OK;
    }

    d = sf_list_tail(&fs.directories);
    if (!d->isopened) {
        if ((ret = directory_open(d)) != SF_OK) {
            return ret;
        }
    }

    if (!d->iszip) {
        return fs_file_walker_dir(d, func, arg);
    } else {
        return fs_file_walker_zip(d, func, arg);
    }
}
Пример #16
0
bool test_file_directory_read_6 (Test *test)
{
	char *path;
	Directory *d3_1f;
	Directory *d1;
	Directory *d2;
	Directory *d3;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/d3_1f"));
        /*
                d stage/d3_1f
                d stage/d3_1f/d1
                f f1
                d stage/d3_1f/d1/d2
                d stage/d3_1f/d1/d2/d3
                f f1
         */
	CATCH (!(d3_1f = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (d3_1f));
	CATCH (!d3_1f->directories);
	CATCH (d3_1f->directories->count != 1);
	CATCH (!(d1 = list_first (d3_1f->directories)->data));
	CATCH (!directory_read (d1));
	CATCH (d1->files->count != 1);
	CATCH (d1->directories->count != 1);
	CATCH (!(d2 = list_first (d1->directories)->data));
	CATCH (!directory_read (d2));
	CATCH (d2->directories->count != 1);
	CATCH (!(d3 = list_first (d2->directories)->data));
	CATCH (!directory_read (d3));
	CATCH (d3->files->count != 1);
	directory_close (d3_1f);
	PASS ();
}
Пример #17
0
bool test_file_open_fail (Test *test)
{
	Directory *directory;
	File *file;
	char *path;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/open_fail"));
        /* stage does not exist */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!(file = directory_find_file (directory, "f1")));
	CATCH (file_open (file));
	CATCH (error_count () != 1);
	CATCH (error_at (0).error != ErrorSystemCall);
	error_reset ();
	CATCH (file_open (NULL));
	CATCH (error_count () != 1);
	CATCH (error_at (0).error != ErrorInvalidArgument);
	directory_close (directory);
	PASS ();
}
Пример #18
0
static int serve_client_out(CLIENT *client, int sock, const char *dir, 
    const char *hostname)
{
    FILE *fp = NULL;
    FILESTATS fstats;
    int  i, connected, file_readcnt, file_sendcnt, buflen, nbytes;

    const char *path;
    char out_dir[PATH_LEN+1];
    unsigned char buf[CH_MSG_SIZE];

    DIRECTORY directory;

    /* create incoming file path */
    sprintf(out_dir, "%s/%s/%s", dir, client->hostname, CHOUSESYS_OUTLOC);

    if ( !directory_exists(out_dir) )
    {
        fprintf(stderr, "%s: %s: error: non-existant out directory: %s\n",
                timestamp(), ProgName, out_dir); 
        return 1;
    }

    directory_init(&directory, out_dir);
    directory_open(&directory);

    /* Set file send count to 0 */
    errno = 0;
    file_readcnt = 0;
    file_sendcnt = 0;
    connected = TRUE;

    while ( (path = directory_list(&directory)) )
    {
        /* Set file statistics to 0 */
        memset(&fstats, 0, sizeof(fstats));

        if ( (fstats.name = (char *)strrchr(path, '/')) == NULL )
            fstats.name = path; /* no path in path */
        else
            fstats.name++; /* skip leading '/' */

#ifdef DEBUG
        if ( Debug > 2 )
        {
            PRINT_DBGLOC;
            fprintf(stderr, "sending %s\n", fstats.name);
        }
#endif
        if ( (fp = fopen(path, "r")) == NULL )
        {
            /* soft error */
            fprintf(stderr, "%s: %s: error: couldn't open %s: %s\n",
                timestamp(), ProgName, basename(path), strerror(ferror(fp))); 
            continue;
        }
        file_readcnt++;

        /* send current file */
        while ( fstats.filesize == fstats.socksize )
        {
            if ( (buflen = fread(buf, sizeof(*buf), sizeof(buf), fp)) == 0 )
            {
                /* finished reading file */
                break;
            }
#ifdef DEBUG
            if ( Debug > 7 )
            {
                PRINT_DBGLOC;
                print_sockseg(buf, buflen);
            }
#endif
            fstats.filesize += buflen;
            fstats.socksize += send(sock, buf, buflen, 0);
        }

        if ( fstats.filesize != fstats.socksize )
        {
                /* error */
                fstats.transfered = FALSE;
        }
        else
        {
            /* finished sending file data - now send eod string */
            nbytes = send(sock, client->eod.hex_buf, client->eod.hex_buflen, 0);
            if ( nbytes != client->eod.hex_buflen ) 
            {
                /* error */
                fprintf(stderr, 
                    "%s: %s: warning: send end-of-data to %s failed\n", 
                    timestamp(), ProgName, hostname);
                fstats.transfered = FALSE; 
            }
            else
            {
                fstats.transfered = TRUE; 
#ifdef DEBUG
                if ( Debug > 3 )
                {
                    PRINT_DBGLOC;
                    print_sockseg(client->eod.hex_buf, client->eod.hex_buflen);
                }
#endif
            }
        }

        /*  finished with current file */
        fclose(fp);

        /* log statistics for this file */
        if ( fstats.transfered )
        {
            file_sendcnt++;
            fprintf(stderr, "%s: %s: info: ", timestamp(), ProgName);
            fprintf(stderr, "send \"%s (%d bytes)\" succeeded to %s\n",
                fstats.name, fstats.filesize, hostname);
        }
        else
        {
            fprintf(stderr, "%s: %s: error: ", timestamp(), ProgName);
            fprintf(stderr, "send \"%s (%d bytes) failed to %s: ", 
                fstats.name, fstats.filesize, hostname);
            fprintf(stderr, "%d/%d bytes read/sent\n",
                    fstats.filesize, fstats.socksize);
        }

    } /* while ( (path = directory_list(&directory)) ) */

    /* close the directory */
    directory_close(&directory);

    /* The port number must be converted first to host byte
     * order before printing.  On most hosts, this is not
     * necessary, but the ntohs() call is included here so
     * that this program could easily be ported to a host
     * that does require it.
     */
    fprintf(stderr, "%s: %s: info: completed %s: %d of %d files sent\n",
        timestamp(), ProgName, hostname, file_readcnt, file_sendcnt);

    return 0;

} /* serve_client_out() */
Пример #19
0
bool test_file_readline_f3 (Test *test)
{
	Directory *directory;
	File *file;
	char *path;
	char *line;
	size_t bytes_read;
	char *check[] = { 
		"",
		"0",
		"AB",
		"012",
		"ABCD",
		"01234",
		"ABCD",
		"012",
		"AB",
		"0",
	};

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/readline"));
        /*
                d stage/readline
                f f2
                f f3 \
                         \
                        0 \
                        AB \
                        012 \
                        ABCD \
                        01234 \
                        ABCD \
                        012 \
                        AB \
                        0
                f f1
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	directory_read (directory);
	CATCH (!(file = directory_find_file (directory, "f3")));
	CATCH (!file_open (file));
	CATCH (!(line = string_create_with_size (5)));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 1);
	CATCH (!string_equals (line, check[0]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 2);
	CATCH (!string_equals (line, check[1]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 3);
	CATCH (!string_equals (line, check[2]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 4);
	CATCH (!string_equals (line, check[3]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 5);
	CATCH (!string_equals (line, check[4]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 5);
	CATCH (!string_equals (line, "0123"));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 5);
	CATCH (!string_equals (line, check[6]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 4);
	CATCH (!string_equals (line, check[7]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 3);
	CATCH (!string_equals (line, check[8]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 2);
	CATCH (!string_equals (line, check[9]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 0);
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 0);
	directory_close (directory);
	string_destroy (line);
	PASS ();
}