예제 #1
0
파일: rename.c 프로젝트: Aetheros/FBI
static void action_rename_kbd_finished(void* data, char* input) {
    rename_data* renameData = (rename_data*) data;

    if(strlen(input) == 0) {
        error_display(NULL, NULL, NULL, "No name specified.");
    }

    file_info* targetInfo = (file_info*) renameData->target->data;

    Result res = 0;

    char parentPath[FILE_PATH_MAX] = {'\0'};
    util_get_parent_path(parentPath, targetInfo->path, FILE_PATH_MAX);

    char dstPath[FILE_PATH_MAX] = {'\0'};
    snprintf(dstPath, FILE_PATH_MAX, "%s%s", parentPath, input);

    FS_Path* srcFsPath = util_make_path_utf8(targetInfo->path);
    if(srcFsPath != NULL) {
        FS_Path* dstFsPath = util_make_path_utf8(dstPath);
        if(dstFsPath != NULL) {
            if(targetInfo->isDirectory) {
                res = FSUSER_RenameDirectory(targetInfo->archive, *srcFsPath, targetInfo->archive, *dstFsPath);
            } else {
                res = FSUSER_RenameFile(targetInfo->archive, *srcFsPath, targetInfo->archive, *dstFsPath);
            }

            util_free_path_utf8(dstFsPath);
        } else {
            res = R_FBI_OUT_OF_MEMORY;
        }

        util_free_path_utf8(srcFsPath);
    } else {
        res = R_FBI_OUT_OF_MEMORY;
    }

    if(R_SUCCEEDED(res)) {
        if(strncmp(renameData->target->name, "<current directory>", LIST_ITEM_NAME_MAX) != 0 && strncmp(renameData->target->name, "<current file>", LIST_ITEM_NAME_MAX) != 0) {
            strncpy(renameData->target->name, input, LIST_ITEM_NAME_MAX);
        }

        strncpy(targetInfo->name, input, FILE_NAME_MAX);
        strncpy(targetInfo->path, dstPath, FILE_PATH_MAX);

        linked_list_sort(renameData->items, util_compare_file_infos);

        prompt_display("Success", "Renamed.", COLOR_TEXT, false, NULL, NULL, NULL);
    } else {
        error_display_res(NULL, NULL, NULL, res, "Failed to perform rename.");
    }

    free(data);
}
예제 #2
0
bool renameRecursive(const FS_Archive archive, const std::string source, const std::string target) {
	const FS_Path sourcePath = fsMakePath(PATH_ASCII, source.c_str());
	const FS_Path targetPath = fsMakePath(PATH_ASCII, target.c_str());

	// Open source directory
	Handle directory = NULL;
	if (FSUSER_OpenDirectory(&directory, archive, sourcePath) != 0) {
		std::printf("\nCould not open %s\n\n", source.c_str());
		return false;
	}

	// Make target directory
	if (FSUSER_CreateDirectory(archive, targetPath, FS_ATTRIBUTE_DIRECTORY) != 0) {
		std::printf("\nCould not create %s\n\n", target.c_str());
		return false;
	}

	u32 fileRead = 0;
	while (true) {
		FS_DirectoryEntry entry = {};
		FSDIR_Read(directory, &fileRead, 1, &entry);
		if (!fileRead) {
			break;
		}

		// Convert name to ASCII (just cut the other bytes)
		char name8[262] = { 0 };
		for (size_t i = 0; i < 262; i++) {
			name8[i] = entry.name[i] % 0xff;
		}
		std::string filePath = std::string("/") + name8;
		std::string from = source + filePath;
		std::string to = target + filePath;

		// Is a directory? Recurse rename
		if (entry.attributes & FS_ATTRIBUTE_DIRECTORY) {
			std::printf("  %s -> %s (DIR)\n", from.c_str(), to.c_str());
			if (!renameRecursive(archive, source + filePath, target + filePath)) {
				return false;
			}
		} else {
			FS_Path sourceFilePath = fsMakePath(PATH_ASCII, from.c_str());
			FS_Path targetFilePath = fsMakePath(PATH_ASCII, to.c_str());
			if (FSUSER_RenameFile(archive, sourceFilePath, archive, targetFilePath) != 0) {
				std::printf("\nCould not rename %s\n\n", (char*)sourceFilePath.data);
				return false;
			}
			std::printf("  %s -> %s\n", (char*)sourceFilePath.data, (char*)targetFilePath.data);
		}
	}

	return true;
}