<commit_before>/*-
 * Copyright (c) 2011 <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <libgen.h>
#include <libutil.h>
#include <inttypes.h>
#include <errno.h>

#include <fstab.h>
#include <libgeom.h>
#include <dialog.h>
#include <dlg_keys.h>

#include "diskeditor.h"
#include "partedit.h"

struct pmetadata_head part_metadata;

static int apply_changes(struct gmesh *mesh);
static struct partedit_item *read_geom_mesh(struct gmesh *mesh, int *nitems);
static void add_geom_children(struct ggeom *gp, int recurse,
    struct partedit_item **items, int *nitems);
static void init_fstab_metadata(void);
static void get_mount_points(struct partedit_item *items, int nitems);
static int validate_setup(void);

int
main(int argc, const char **argv) {
	struct partition_metadata *md;
	const char *prompt;
	struct partedit_item *items;
	struct gmesh mesh;
	int i, op, nitems, nscroll;
	int error;

	TAILQ_INIT(&part_metadata);

	init_fstab_metadata();

	init_dialog(stdin, stdout);
	if (strcmp(basename(argv[0]), "sade") != 0)
		dialog_vars.backtitle = __DECONST(char *, "FreeBSD Installer");
	dialog_vars.item_help = TRUE;
	nscroll = i = 0;

	if (strcmp(basename(argv[0]), "autopart") == 0) { /* Guided */
		prompt = "Please review the disk setup. When complete, press "
		    "the Exit button.";
		part_wizard();
	} else {
		prompt = "Create partitions for FreeBSD. No changes will be "
		    "made until you select Exit.";
	}

	/* Show the part editor either immediately, or to confirm wizard */
	while (1) {
		error = geom_gettree(&mesh);
		items = read_geom_mesh(&mesh, &nitems);
		get_mount_points(items, nitems);
		dlg_clear();
		dlg_put_backtitle();

		if (i >= nitems)
			i = nitems - 1;
		op = diskeditor_show("Partition Editor", prompt,
		    items, nitems, &i, &nscroll);

		switch (op) {
		case 0: /* Create */
			gpart_create((struct gprovider *)(items[i].cookie),
			    NULL, NULL, NULL, NULL, 1);
			break;
		case 1: /* Delete */
			gpart_delete((struct gprovider *)(items[i].cookie));
			break;
		case 2: /* Modify */
			gpart_edit((struct gprovider *)(items[i].cookie));
			break;
		case 3: /* Revert */
			gpart_revert_all(&mesh);
			while ((md = TAILQ_FIRST(&part_metadata)) != NULL) {
				if (md->fstab != NULL) {
					free(md->fstab->fs_spec);
					free(md->fstab->fs_file);
					free(md->fstab->fs_vfstype);
					free(md->fstab->fs_mntops);
					free(md->fstab->fs_type);
					free(md->fstab);
				}
				if (md->newfs != NULL)
					free(md->newfs);
				free(md->name);

				TAILQ_REMOVE(&part_metadata, md, metadata);
				free(md);
			}
			init_fstab_metadata();
			break;
		case 4: /* Auto */
			part_wizard();
			break;
		}

		error = 0;
		if (op == 5 && validate_setup()) { /* Finished */
			dialog_vars.extra_button = TRUE;
			dialog_vars.extra_label =
			    __DECONST(char *, "Abort");
			dialog_vars.ok_label = __DECONST(char *, "Save");
			op = dialog_yesno("Confirmation", "Your changes will "
			    "now be written to disk. If you have chosen to "
			    "overwrite existing data, it will be PERMANENTLY "
			    "ERASED. Are you sure you want to proceed?", 0, 0);
			dialog_vars.extra_button = FALSE;
			dialog_vars.ok_label = NULL;

			if (op == 0) { /* Save */
				error = apply_changes(&mesh);
				break;
			} else if (op == 3) { /* Don't save => Quit */
				gpart_revert_all(&mesh);
				error =	-1;
				break;
			}
		}

		geom_deletetree(&mesh);
		free(items);
	}
	

	geom_deletetree(&mesh);
	free(items);
	end_dialog();

	return (error);
}

struct partition_metadata *
get_part_metadata(const char *name, int create)
{
	struct partition_metadata *md;

	TAILQ_FOREACH(md, &part_metadata, metadata) 
		if (md->name != NULL && strcmp(md->name, name) == 0)
			break;<commit_msg>Only validate the partition setup when the user presses "Save". If the user wants to exit without saving, it's not a problem if the disk is set up in an invalid way.<commit_after>/*-
 * Copyright (c) 2011 <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#include <sys/param.h>
#include <libgen.h>
#include <libutil.h>
#include <inttypes.h>
#include <errno.h>

#include <fstab.h>
#include <libgeom.h>
#include <dialog.h>
#include <dlg_keys.h>

#include "diskeditor.h"
#include "partedit.h"

struct pmetadata_head part_metadata;

static int apply_changes(struct gmesh *mesh);
static struct partedit_item *read_geom_mesh(struct gmesh *mesh, int *nitems);
static void add_geom_children(struct ggeom *gp, int recurse,
    struct partedit_item **items, int *nitems);
static void init_fstab_metadata(void);
static void get_mount_points(struct partedit_item *items, int nitems);
static int validate_setup(void);

int
main(int argc, const char **argv) {
	struct partition_metadata *md;
	const char *prompt;
	struct partedit_item *items;
	struct gmesh mesh;
	int i, op, nitems, nscroll;
	int error;

	TAILQ_INIT(&part_metadata);

	init_fstab_metadata();

	init_dialog(stdin, stdout);
	if (strcmp(basename(argv[0]), "sade") != 0)
		dialog_vars.backtitle = __DECONST(char *, "FreeBSD Installer");
	dialog_vars.item_help = TRUE;
	nscroll = i = 0;

	if (strcmp(basename(argv[0]), "autopart") == 0) { /* Guided */
		prompt = "Please review the disk setup. When complete, press "
		    "the Exit button.";
		part_wizard();
	} else {
		prompt = "Create partitions for FreeBSD. No changes will be "
		    "made until you select Exit.";
	}

	/* Show the part editor either immediately, or to confirm wizard */
	while (1) {
		error = geom_gettree(&mesh);
		items = read_geom_mesh(&mesh, &nitems);
		get_mount_points(items, nitems);
		dlg_clear();
		dlg_put_backtitle();

		if (i >= nitems)
			i = nitems - 1;
		op = diskeditor_show("Partition Editor", prompt,
		    items, nitems, &i, &nscroll);

		switch (op) {
		case 0: /* Create */
			gpart_create((struct gprovider *)(items[i].cookie),
			    NULL, NULL, NULL, NULL, 1);
			break;
		case 1: /* Delete */
			gpart_delete((struct gprovider *)(items[i].cookie));
			break;
		case 2: /* Modify */
			gpart_edit((struct gprovider *)(items[i].cookie));
			break;
		case 3: /* Revert */
			gpart_revert_all(&mesh);
			while ((md = TAILQ_FIRST(&part_metadata)) != NULL) {
				if (md->fstab != NULL) {
					free(md->fstab->fs_spec);
					free(md->fstab->fs_file);
					free(md->fstab->fs_vfstype);
					free(md->fstab->fs_mntops);
					free(md->fstab->fs_type);
					free(md->fstab);
				}
				if (md->newfs != NULL)
					free(md->newfs);
				free(md->name);

				TAILQ_REMOVE(&part_metadata, md, metadata);
				free(md);
			}
			init_fstab_metadata();
			break;
		case 4: /* Auto */
			part_wizard();
			break;
		}

		error = 0;
		if (op == 5) { /* Finished */
			dialog_vars.extra_button = TRUE;
			dialog_vars.extra_label =
			    __DECONST(char *, "Abort");
			dialog_vars.ok_label = __DECONST(char *, "Save");
			op = dialog_yesno("Confirmation", "Your changes will "
			    "now be written to disk. If you have chosen to "
			    "overwrite existing data, it will be PERMANENTLY "
			    "ERASED. Are you sure you want to proceed?", 0, 0);
			dialog_vars.extra_button = FALSE;
			dialog_vars.ok_label = NULL;

			if (op == 0 && validate_setup()) { /* Save */
				error = apply_changes(&mesh);
				break;
			} else if (op == 3) { /* Don't save => Quit */
				gpart_revert_all(&mesh);
				error =	-1;
				break;
			}
		}

		geom_deletetree(&mesh);
		free(items);
	}
	

	geom_deletetree(&mesh);
	free(items);
	end_dialog();

	return (error);
}

struct partition_metadata *
get_part_metadata(const char *name, int create)
{
	struct partition_metadata *md;

	TAILQ_FOREACH(md, &part_metadata, metadata) 
		if (md->name != NULL && strcmp(md->name, name) == 0)
			break;
{'fraction_non_alphanumeric': 0.12252887062047367, 'fraction_numerical': 0.005284791544333529, 'mean_word_length': 3.788659793814433, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5817702', 'n_tokens_mistral': 3830, 'n_tokens_neox': 3300, 'n_words': 1074}