Exemple #1
0
static bool cleanOut(const char* dir) {
	printf("cleanOut(%s)\n", dir);
	MAHandle list = maFileListStart(dir, "*");
	MAT(list);
	char buf[2048];
	int dirLen = strlen(dir);
	strcpy(buf, dir);
	int freeBufSpace = sizeof(buf) - dirLen;
	while(1) {
		int res;
		MAHandle fh;
		char* fileName = buf + dirLen;
		res = maFileListNext(list, fileName, freeBufSpace);
		MAT_CUSTOM(res, (_res < 0 || _res >= freeBufSpace));
		if(res == 0)
			return true;
		if(fileName[res-1] == '/') {
			if(!cleanOut(buf))
				return false;
		}
		MAT(fh = maFileOpen(buf, MA_ACCESS_READ_WRITE));
		res = maFileDelete(fh);
		MAASSERT(maFileClose(fh) == 0);
		MAT(res);
	}
	MAASSERT(maFileListClose(list) == 0);
	return true;
}
Exemple #2
0
void FileLister::close() {
	if(mList > 0) {
		int res = maFileListClose(mList);
		MAASSERT(res == 0);
		mList = -1;
	}
}
Exemple #3
0
	/**
	 * Copy the files in a directory recursively.
	 */
	static int FileCopyDirectory(
		const String& sourcePathParam,
		const String& destinationPathParam)
	{
		char nameBuf[2048];

		String sourcePath = sourcePathParam;
		String destinationPath = destinationPathParam;

		// Make sure both source and destination paths end with a slash.
		FileMakeDirectoryPath(sourcePath);
		FileMakeDirectoryPath(destinationPath);

		// Open directory listing of source dir.
		MAHandle list = maFileListStart(sourcePath.c_str(), "", MA_FL_SORT_NONE);
		if (list < 0)
		{
			return -1;
		}

		// Copy all files in this directory and in subdirectories.
		while (true)
		{
			// Move to next file.
			int result = maFileListNext(list, nameBuf, 2048);
			if (0 == result)
			{
				// No more files.
				break;
			}
			if (0 > result)
			{
				// Error.
				return -1;
			}

			String fullSourcePath = sourcePath + nameBuf;
			String fullDestinationPath = destinationPath + nameBuf;

			// Is this a directory?
			if ('/' == nameBuf[result - 1])
			{
				// Copy recursively.
				FileCopyDirectory(fullSourcePath, fullDestinationPath);
			}
			else
			{
				// Copy file.
				FileCopyFile(fullSourcePath, fullDestinationPath);
			}
		}

		// Close the directory listing.
		maFileListClose(list);

		return 0;
	}
Exemple #4
0
	/**
	 * Delete a directory recursively.
	 */
	static int FileDeleteDirectory(const String& pathParam)
	{
		char nameBuf[2048];

		String path = pathParam;

		FileMakeDirectoryPath(path);

		// Open directory listing.
		MAHandle list = maFileListStart(path.c_str(), "", MA_FL_SORT_NONE);
		if (list < 0)
		{
			return -1;
		}

		// Delete all files in this directory and in subdirectories.
		while (true)
		{
			// Move to next file.
			int result = maFileListNext(list, nameBuf, 2048);
			if (0 == result)
			{
				// No more files.
				break;
			}
			if (0 > result)
			{
				// Error.
				return -1;
			}

			String fullPath = path + nameBuf;

			// Is this a directory?
			if ('/' == nameBuf[result - 1])
			{

				// Delete recursively.
				FileDeleteDirectory(fullPath);
			}
			else
			{
				// Delete file.
				FileDeleteFile(fullPath);
			}
		}

		// Close the directory listing.
		maFileListClose(list);

		// Delete the directory.
		return FileDeleteFile(path);
	}
Exemple #5
0
static bool dumpFileList(const char* path) {
	MAHandle list = maFileListStart(path, "*", 0);
	if(list < 0) {
		LPRINTFLN("FLS error %i", list);
		return false;
	}
	bool empty = true;
	while(maFileListNext(list, buffer, sizeof(buffer)) > 0) {
		checkEvents();
		MAUtil::String p2(path);
		p2 += buffer;
		LOG(p2);
		if(p2[p2.size()-1] == '/')
			dumpFileList(p2.c_str());
		empty = false;
	}
	maFileListClose(list);
	//printf("%s: %s\n", empty ? "Empty" : "Done", path);
	return !empty;
}
Exemple #6
0
	// mosync://PhoneGap?service=File&action=readEntries&args={"fullPath":"/mnt/sdcard/fob1"}&
	// PhoneGapCallBackId=File21
	void PhoneGapFile::actionReadEntries(JSONMessage& message)
	{
		String callbackID = message.getParam("PhoneGapCallBackId");

		String path = message.getArgsField("fullPath");

		// Open entry array.
		String entries = "[";

		char nameBuf[2048];

		// Make sure path end with a slash.
		FileMakeDirectoryPath(path);

		// Open directory listing.
		MAHandle list = maFileListStart(
			path.c_str(),
			"",
			MA_FL_SORT_NAME | MA_FL_ORDER_ASCENDING);
		if (list < 0)
		{
			callFileError(callbackID, FILEERROR_NOT_FOUND_ERR);
			return;
		}

		// List all files in this directory.
		while (true)
		{
			// Move to next file.
			int result = maFileListNext(list, nameBuf, 2048);
			if (0 == result)
			{
				// No more files.
				break;
			}
			if (0 > result)
			{
				maFileListClose(list);
				callFileError(callbackID, FILEERROR_NOT_FOUND_ERR);
				return;
			}

			// Add separating comma if needed.
			if (entries.size() > 1)
			{
				entries += ",";
			}

			// Full path to entry.
			String fullPath = path + nameBuf;

			// Is this a directory?
			if ('/' == nameBuf[result - 1])
			{
				// We remove the trailing slash of the directory.
				String pathWithNoSlash = fullPath.substr(0, fullPath.size() - 1);
				String entry = emitDirectoryEntry(
					FileGetName(pathWithNoSlash),
					pathWithNoSlash);
				entries += entry;
			}
			else
			{
				String entry = emitFileEntry(
					FileGetName(fullPath),
					fullPath);
				entries += entry;
			}
		}

		// Close the directory listing.
		maFileListClose(list);

		// Close entry array.
		entries += "]";

		// Return result to PhoneGap.
		callSuccess(
			callbackID,
			entries,
			"window.localFileSystem._castEntries");
	}