Re: glib 1.2 way of doing a tar/find backup?



Somewhere on the NYLXS server is a jounral article on writing a GTK backup program
which I wrote and which will help you.

Do a google search for it.

Ruben

        
        
        On Fri, Sep 10, 2004 at 01:06:50AM -0400, Tony Freeman wrote:
Thanks Jared,

It took me a good portion of the day after reading this email, but I
finally figured out how to use popen correctly.  I'm a slow learner, I
guess.  After doing a man on popen and not seeing an example, I did a
google and found lots of the same type of example that didn't really
seem to fit what I needed; which was to launch the process and run the
status bar until the process finished.  It turns out that I only needed
to change just a few lines of my code!

So ... for others looking for an example of how to run a gtk progress
bar until a process is finished:

void create_tar_file (GtkButton *button, gchar *day)
{
      FILE *fp;
      GtkLabel *main_label;
      main_label = (gpointer)lookup_widget(GTK_WIDGET(button), "label_main");
      
      GtkProgressBar *progress_bar1;
      progress_bar1 = (gpointer)lookup_widget(GTK_WIDGET(button),
"progressbar1");
      
      gchar *update_label;
      gchar *backup_command;
      gchar thisyear[10], thismonth[10], thisday[10];
      gint cmin_start, cmin_stop;
      gchar string_start[10], string_end[10];
      gint child_stat = 0;
      gboolean test = TRUE;
      gint process_status;
      gint progress = 0;
      pid_t pid;

      sprintf(thisyear, "%d", today_year);
      sprintf(thismonth, "%02.f", (gfloat)today_month);
      sprintf(thisday, "%02.f", (gfloat)today_date);
      
      g_print("\nSTART OF BACKUP FUNCTION\n");
      update_label = g_strconcat("Please Wait:\nArchiving ", day, " Files",
NULL);
      gtk_label_set_text(main_label, update_label);
      
      cmin_start = ((gint)g_hash_table_lookup(daysTable, day)) -
((gint)((24.0 - start_sun) * 60));
      cmin_stop = ((gint)g_hash_table_lookup(daysTable, day)) - ((gint)((24.0
- end_sun) * 60));
      
      sprintf(string_start, "%d", cmin_start);
      sprintf(string_end, "%d", cmin_stop);
      
      backup_command = g_strconcat(find_exe, " /usr/share -type f -exec tar
-rf /tmp/", thisyear, thismonth, thisday, ".tar {} \\;", NULL);

      g_print("BACKUP COMMAND: \n\t%s\n", backup_command);
      
      pid = fork();
      if (pid == -1) {
              g_error("Forking Failed");
      } else if (pid == 0) {
              // This is the child .... all code for the child.
              g_print("\nCHILD: Running backup command.\n");
              if ((fp = popen(backup_command, "r")) == NULL) {
                      g_error("The child process exited abnormally while trying to execute
the backup command");
                      _exit(-1);
              }
              pclose(fp);
      } else {
              // This is the parent ... all code for the parent.
              while (test) {
                      process_status = waitpid((pid_t)pid, &child_stat, WNOHANG);
                      if (process_status == 0) {
                              // process still running ... update progress bar.
                              gtk_progress_set_value (GTK_PROGRESS (progress_bar1), progress);
                              progress = (progress + 1) % 100;
                              gtk_main_iteration();
                              test = TRUE;
                      } else if (process_status == pid) {
                              g_print("Child exited: Stopping progress bar.\n");
                              update_label = g_strconcat("Finished Archiving ", day, " Files.",
NULL);
                              gtk_label_set_text(main_label, update_label);
                              g_print("Finished Archiving %s Files\n", day);
                              g_free(backup_command);
                              test = FALSE;
                      } else {
                              g_error("Some sort of error while checking process_status\n");
                              test = FALSE;
                              _exit(-1);
                      }
              }
      }
      g_print("\nEND OF BACKUP FUNCTION\n");
}



--Tony

On Thu, 2004-09-09 at 06:04, Jared Kells wrote:
I would have a look at popen, this is the clasic unix way of doing
something like that. Same as system but returns a c FILE* you read and
write to like a normal file that is connected to the stdin/out of the
command you are executing so you can read the output into your program
and work out progress that sort of thing. Also doesn't block..

man popen

On Thu, 2004-09-09 at 19:52, John Cupitt wrote:
ahem, of course that's "its" not "it's"

On Thu, 9 Sep 2004 09:52:13 +0000, John Cupitt <jcupitt gmail com> wrote:
Hi,

On Thu, 09 Sep 2004 01:03:08 -0400, Tony Freeman
<tony freeman insightbb com> wrote:
I can tar and gzip the data
really easy into a temp directory using a shell command:

tar -czf /tmp/20040808_backup.tar.gz `find /data/monday -daystart -cmin
+1440 -cmin -2880`

How can I do this in C/glib/gtk =< 1.2?

The easiest is system( "my-shell-command" ); although your interface
will appear to lock up while it does it's work. If you want to stay
live and give feedback, the next easiest is to run the shell command
as a background job (just put an "&" at the end) and poll in a timeout
for completion (in my opinion).

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

-- 
__________________________
Brooklyn Linux Solutions

So many immigrant groups have swept through our town 
that Brooklyn, like Atlantis, reaches mythological 
proportions in the mind of the world  - RI Safir 1998

DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
http://fairuse.nylxs.com

http://www.mrbrklyn.com - Consulting
http://www.inns.net <-- Happy Clients
http://www.nylxs.com - Leadership Development in Free Software
http://www2.mrbrklyn.com/resources - Unpublished Archive or stories and articles from around the net
http://www2.mrbrklyn.com/downtown.html - See the New Downtown Brooklyn....

1-718-382-0585



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]