Skip to content
Snippets Groups Projects
Commit 85c84d99 authored by Luiza Wille's avatar Luiza Wille
Browse files

fixed the mount ipxe program and created an english version for it


Signed-off-by: default avatarLuiza Wille <lmwc14@inf.ufpr.br>
parent 04be9527
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ all: mountIPXE
mountIPXE: mountIPXE.c
gcc `pkg-config --cflags gtk+-3.0` -o mountIPXE mountIPXE.c `pkg-config --libs gtk+-3.0`
gcc `pkg-config --cflags gtk+-3.0` -o mountIPXE-en mountIPXE-en.c `pkg-config --libs gtk+-3.0`
clean:
rm mountIPXE
rm mountIPXE-en
File added
File added
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define NUM_COUNT 5 // how many time until give up to get disks
#define ARQUIVO "./tmp" // file where the script writes the returns
// global widgets
GtkEntry *entry;
GtkComboBoxText *comboboxtext;
void remove_tmp_file (){
int pid, status;
pid = fork();
if (pid == 0){
execlp("./script.sh", "script.sh", "2", NULL); // call script to remove tmp file
exit(0);
}
waitpid(pid, &status, 0); // wait until remove the file
}
// opens a file choose dialog
static void openFileDialog (GtkWidget *widget, gpointer data)
{
GtkWidget *dialog;
gint res;
dialog = gtk_file_chooser_dialog_new ("Open File", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, ("_Cancel"), GTK_RESPONSE_CANCEL, ("_Open"), GTK_RESPONSE_ACCEPT, NULL);
res = gtk_dialog_run (GTK_DIALOG(dialog));
if (res == GTK_RESPONSE_ACCEPT)
{
// when a file is selected text in the entry has to be changed too
gtk_entry_set_text (GTK_ENTRY(data), gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
}
gtk_widget_destroy(dialog);
}
// gets the pen drive and the file selected, verify and call the script
static void confirm (GObject *widget, gpointer data)
{
FILE *file;
int resp, pid, status;
GtkWidget *dialog;
gchar *disk;
gchar *image;
// store the strings for the pen drive and the file
disk = gtk_combo_box_text_get_active_text(comboboxtext);
image = (gchar *) gtk_entry_get_text(entry);
// verify if a file was selected
if (!strcmp(image, "")){
dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Error!\n No file was selected.");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return;
}
// verify if a pen drive was selected
if (disk == NULL){
dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Error!\n No pen drive was selected.");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return;
}
// asks the user to confirm
dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL, "Are you sure you want to format your device?\nContinuing, all the data inside the pen drive %s will be deleted!", disk);
resp = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
if (resp == GTK_RESPONSE_OK){
// after confirmation it will call the script to format the pen drive
pid = fork();
if (pid == 0){
execlp("./script.sh", "script.sh", "1", strtok(disk, " "), image, NULL);
exit(0);
}
waitpid(pid, &status, 0); // wait until script is finished
// the script will return to a file that should be created every time
file = fopen(ARQUIVO, "r");
if (file == NULL)
resp = 3; // should never go here, but strange things can happen and we must be prepared
else{
fscanf(file, "%d", &resp); // get the script response
fclose(file);
}
if (resp == 0){
// format sucess
dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Success!\nThe pen drive %s was formated correctly and it is ready to boot.", disk);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
// errors may have ocurred
if (resp == 1){
// format error (mkfs)
dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Error!\n It was not possible to format the pen drive %s correctly.", disk);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
if (resp == 2){
// error while copying the image to pen drive
dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Error!\n It was not possible to save the image %s in the pen drive %s", image, disk);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
if (resp == 3){
// error while copying the image to pen drive
dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Error!\n Some unexpected error happened!");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
remove_tmp_file();
}
}
// gets all pen drives connected, has to be called after the script
char *get_disks(){
GtkWidget *dialog;
FILE *file;
int tam, cont = 0;
char *buf;
int pid, status;
pid = fork();
if (pid == 0){
execlp("./script.sh", "script.sh", "0", NULL); // call get_disks from the script
exit(0);
}
waitpid(pid, &status, 0);
// wait until the file is created by the script with the pen drives connected
file = fopen(ARQUIVO, "r");
if (file == NULL){
// no pen drive connected
dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Error!\n No pen drive was found. Insert one and click to refresh.");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return NULL;
}
// got the file, now store all information in a string
fseek(file, 0, SEEK_END);
tam = ftell(file);
fseek(file, 0, SEEK_SET);
buf = (char *) malloc (tam*sizeof(char));
fread(buf, tam, 1, file);
fclose(file);
remove_tmp_file();
return buf;
}
// update the list of pen drives connected in the combo box text
void box_update(){
char *usb_disks, *disk;
// remove all lines in combo box
gtk_combo_box_text_remove_all(comboboxtext);
// gets the new list of pen drives connected and for each one
usb_disks = get_disks();
if (usb_disks != NULL){
disk = strtok(usb_disks, "\n");
while (disk != NULL){
gtk_combo_box_text_append_text(comboboxtext, disk);
disk = strtok(NULL, "\n");
}
// sets the first on the list as active
gtk_combo_box_set_active(GTK_COMBO_BOX(comboboxtext), 0);
free(usb_disks);
}
}
// control the layout and callbacks of events
static void activate (GtkApplication* app, gpointer user_data)
{
GtkWidget *window;
GtkWidget *label;
GtkWidget *button;
GtkWidget *grid;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW (window), "Create bootable USB device.");
//gtk_window_set_default_size(GTK_WINDOW (window), 400, 300);
gtk_container_set_border_width(GTK_CONTAINER (window), 10);
grid = gtk_grid_new();
gtk_grid_set_row_spacing(GTK_GRID(grid), 5);
gtk_grid_set_column_spacing(GTK_GRID(grid), 5);
gtk_container_add(GTK_CONTAINER (window), grid);
label = gtk_label_new("Select the IPXE boot file:");
gtk_grid_attach(GTK_GRID (grid), label, 0, 0, 1, 1);
entry = GTK_ENTRY(gtk_entry_new());
gtk_grid_attach(GTK_GRID (grid), GTK_WIDGET(entry), 0, 1, 6, 1);
button = gtk_button_new_with_label("...");
gtk_grid_attach_next_to(GTK_GRID (grid), button, GTK_WIDGET(entry), GTK_POS_RIGHT, 1, 1);
g_signal_connect(button, "clicked", G_CALLBACK(openFileDialog), entry);
label = gtk_label_new("Select the device that will be used:");
gtk_grid_attach(GTK_GRID (grid), label, 0, 2, 1, 1);
comboboxtext = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
gtk_grid_attach(GTK_GRID(grid), GTK_WIDGET(comboboxtext), 0, 3, 1, 1);
button = gtk_button_new_with_label("Refresh");
g_signal_connect_swapped(button, "clicked", G_CALLBACK(box_update), NULL);
gtk_grid_attach_next_to(GTK_GRID(grid), button, GTK_WIDGET(comboboxtext), GTK_POS_RIGHT, 1, 1);
button = gtk_button_new_with_label("Cancel");
g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);
gtk_grid_attach(GTK_GRID(grid), button, 0, 4, 1, 1);
button = gtk_button_new_with_label("Create");
g_signal_connect(button, "clicked", G_CALLBACK(confirm), NULL);
gtk_grid_attach(GTK_GRID(grid), button, 1, 4, 1, 1);
gtk_widget_show_all(window);
}
int main (int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
......@@ -199,7 +199,7 @@ static void activate (GtkApplication* app, gpointer user_data)
gtk_grid_set_column_spacing(GTK_GRID(grid), 5);
gtk_container_add(GTK_CONTAINER (window), grid);
label = gtk_label_new("Selcione o arquivo de boot do IPXE:");
label = gtk_label_new("Selecione o arquivo de boot do IPXE:");
gtk_grid_attach(GTK_GRID (grid), label, 0, 0, 1, 1);
entry = GTK_ENTRY(gtk_entry_new());
......@@ -209,7 +209,7 @@ static void activate (GtkApplication* app, gpointer user_data)
gtk_grid_attach_next_to(GTK_GRID (grid), button, GTK_WIDGET(entry), GTK_POS_RIGHT, 1, 1);
g_signal_connect(button, "clicked", G_CALLBACK(openFileDialog), entry);
label = gtk_label_new("Selcione o dispositivo que será utilizado:");
label = gtk_label_new("Selecione o dispositivo que será utilizado:");
gtk_grid_attach(GTK_GRID (grid), label, 0, 2, 1, 1);
comboboxtext = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
......@@ -235,7 +235,6 @@ int main (int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment