Beispiel #1
0
/* =============== GPL HEADER =====================
 * TuxStrings.c is part of TuxDroidServer
 * Copyleft (C) 2012 - Joel Matteotti <joel _DOT_ matteotti _AT_ free _DOT_ fr>
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 * ====================================================
*/

#include <stdlib.h>
#include <unistd.h>

#include <unity.h>
#include "TuxUtils.h"

#define SHELL_COMMAND_LEN 256
#define PATH_LEN 256
char old_cwd[PATH_LEN];

void _saveCWD();
void _restoreCWD();
void _createTemporaryDirectory(char *directory_path);
void _createTemporaryFile(char *file_path);
void _generateTemporaryPath(char *path);
void _createDirectory(char *directory_path);
void _deleteDirectory(char *directory_path);
void _createFile(char *file_path);
void _deleteFile(char *file_path);

void setUp(void)
{
	_saveCWD();
}

void tearDown(void)
{
	_restoreCWD();
}

#ifndef _WIN32 /* TODO: make windows compatible */
void testGetCurrentDir(void)
{
	char current_working_directory[PATH_LEN];

	_createTemporaryDirectory(current_working_directory);
	chdir(current_working_directory);
	char *expected = current_working_directory;

	char *result = getCurrentDir();

	TEST_ASSERT_EQUAL_STRING(expected, result);

	free(result);
	_deleteDirectory(current_working_directory);
}
#endif

#ifndef _WIN32 /* TODO: make windows compatible */
void testFileExistsTrue(void)
{
	char temporary_file[PATH_LEN];

	_createTemporaryFile(temporary_file);

	bool result = file_exists(temporary_file);

	TEST_ASSERT_TRUE(result);

	_deleteFile(temporary_file);
}
int _doDeleteFileOrDirectory(GFile *file)
{
    GFileEnumerator *enumerator;
    GFileInfo *fileinfo;
    int error = NO_ERROR;
    GFile *child;
    
    // deletes a file or an empty directory
    error = _deleteFile(file);
    if (error == ERR_NOT_FOUND || error == NO_ERROR) {
        return error;
    }
    
    enumerator = g_file_enumerate_children(file, "standard::*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, NULL);

    if (enumerator == NULL) {
        error = ERR_UNKNOWN;
    } else {
        // recursively delete directory contents
        while ((fileinfo = g_file_enumerator_next_file(enumerator, NULL, NULL)) != NULL) {
            child = g_file_get_child(file, g_file_info_get_name(fileinfo));
            error = _doDeleteFileOrDirectory(child);
            g_object_unref(child);

            if (error != NO_ERROR) {
                break;
            }
        }

        // directory is now empty, delete it
        if (error == NO_ERROR) {
            error = _deleteFile(file);
        }

        g_object_unref(enumerator);
    }

    return error;
}