Ejemplo n.º 1
0
/*
 * Converts the specified registry file.  The specified file is
 * removed if the conversion is successful.  If conversion_count
 * is not NULL, the total number of Articles converted will be
 * passed back.
 */
int
wsreg_convert_registry(const char *filename, int *conversion_count,
    Progress_function progress_callback)
{
	File_util *futil = _wsreg_fileutil_initialize();

	if (initialized == WSREG_NOT_INITIALIZED) {
		return (WSREG_NOT_INITIALIZED);
	}

	if (!futil->exists(filename)) {
		/*
		 * Bad filename.
		 */
		return (WSREG_FILE_NOT_FOUND);
	}
	if (futil->can_read(filename) && futil->can_write(filename)) {
		/*
		 * The registry file can be read and removed.
		 */
		if (wsreg_can_access_registry(O_RDWR)) {
			/*
			 * The conversion permissions are appropriate.
			 * Perform the conversion.
			 */
			int result;
			int article_count = 0;
			Progress *progress =
			    _wsreg_progress_create(
				    (Progress_callback)*progress_callback);
			int count = 0;
			Unz_article_input_stream *ain = NULL;
			Conversion *c = NULL;

			/*
			 * The first progress section represents the
			 * unzipping of the data file.
			 */
			progress->set_section_bounds(progress, 5, 1);
			ain = _wsreg_uzais_open(filename, &result);
			progress->finish_section(progress);
			if (result != WSREG_SUCCESS) {
				/*
				 * The open failed.  Clean up and
				 * return the error code.
				 */
				if (ain != NULL) {
					ain->close(ain);
				}
				progress->free(progress);
				return (result);
			}

			c = _wsreg_conversion_create(progress);

			/*
			 * The second progress section represents
			 * the reading of articles.
			 */
			article_count = ain->get_article_count(ain);
			progress->set_section_bounds(progress, 8,
			    article_count);
			while (ain->has_more_articles(ain)) {
				Article *a = ain->get_next_article(ain);
				if (a != NULL) {
					c->add_article(c, a);
				}
				progress->increment(progress);
			}
			progress->finish_section(progress);
			ain->close(ain);

			/*
			 * The third progress section represents
			 * the conversion and registration of the
			 * resulting components.
			 */
			progress->set_section_bounds(progress, 100,
			    article_count);
			count = c->register_components(c, NULL, FALSE);
			progress->finish_section(progress);

			/*
			 * Pass the count back to the caller.
			 */
			if (conversion_count != NULL) {
				*conversion_count = count;
			}

			/*
			 * Remove the old registry file.
			 */
			futil->remove(filename);

			/*
			 * Cleanup objects.
			 */
			c->free(c);
			progress->free(progress);

			return (WSREG_SUCCESS);
		} else {
			/*
			 * No permission to modify the registry.
			 */
			return (WSREG_NO_REG_ACCESS);
		}
	} else {
		/*
		 * No permission to read and delete the specified file.
		 */
		return (WSREG_NO_FILE_ACCESS);
	}
}