59d1e062786a2aa8a425183282ceccb602964694
[udpgate.git] / src / bundle-util.c
1 /* $Id$
2  * UDP Gateway single-file bundle utility functions
3  * Copyright (C) 2004 Jan Kratochvil <project-udpgate@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <glib/gmem.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <time.h>
31 #include <stdio.h>
32
33 #include "bundle-util.h"        /* self */
34 #include "bundle.h"
35 #include "main.h"
36
37
38 static G_CONST_RETURN guint8 *bundle_util_file_retrieve(guint32 *data_length_return,const gchar *basename)
39 {
40 const guint8 *data;
41
42         g_return_val_if_fail(data_length_return!=NULL,NULL);
43         g_return_val_if_fail(basename!=NULL,NULL);
44
45         if (optarg_verbose)
46                 g_message(_("Retrieving internally stored: %s"),basename);
47         data=g_hash_table_lookup(bundle_hash_new(),basename);
48         g_return_val_if_fail(data!=NULL,NULL);
49         *data_length_return=GUINT32_FROM_BE(*(guint32 *)data);
50         data+=sizeof(*data_length_return);
51
52         return data;
53 }
54
55 /* Returns TRUE if it safe to overwrite/unlink the file.
56  */
57 static gboolean bundle_util_file_backup_conditional(const gchar *pathname,const gchar *basename)
58 {
59 const guint8 *data;
60 guint32 data_length;
61 int fd;
62 guint8 *data_found;
63 int got;
64 char strftime_buffer[LINE_MAX];
65 const gchar *destination;
66 time_t time_current;
67
68         g_return_val_if_fail(pathname!=NULL,FALSE);
69         g_return_val_if_fail(basename!=NULL,FALSE);
70
71         if (!(data=bundle_util_file_retrieve(&data_length,basename)))
72                 return FALSE;
73
74         if (-1==(fd=open(pathname,O_RDONLY))) {
75                 if (errno==ENOENT)
76                         return TRUE;
77                 else {
78                         g_warning(_("Error checking file modifications of \"%s\": %m"),pathname);
79                         return FALSE;
80                         }
81                 }
82
83         /* WARNING: 'data_found' allocated in this block! */
84         data_found=g_malloc(data_length+1);
85         if (-1==(got=read(fd,data_found,data_length+1)))
86                 g_warning(_("Error reading during the check of file modifications of \"%s\": %m"),pathname);
87         if (close(fd))
88                 g_warning(_("Error closing the file \"%s\" during the check of its modifications: %m"),pathname);
89         g_free(data_found);
90
91         if (got==(int)data_length && !memcmp(data_found,data,data_length))
92                 return TRUE;
93
94         time_current=time(NULL);        /* It is segfault to gmtime(NULL). */
95         if (!strftime(strftime_buffer,sizeof(strftime_buffer),"GMT%FT%T",gmtime(&time_current))) {
96                 g_warning("strftime(3): %m");   /* shouldn't happen */
97                 return FALSE;
98                 }
99         destination=udpgate_printf_alloca("%s-%s-%d",pathname,strftime_buffer,(int)getpid());
100         if (rename(pathname,destination)) {
101                 g_warning(_("Error renaming your modified file \"%s\" to the backup \"%s\", giving up: %m"),
102                                 pathname,destination);
103                 return FALSE;
104                 }
105
106         return TRUE;
107 }
108
109 gboolean bundle_util_file_remove(const gchar *pathname,const gchar *basename)
110 {
111         g_return_val_if_fail(pathname!=NULL,FALSE);
112         g_return_val_if_fail(basename!=NULL,FALSE);
113
114         if (!bundle_util_file_backup_conditional(pathname,basename))
115                 return FALSE;
116
117         if (unlink(pathname) && errno!=ENOENT) {
118                 g_warning(_("Error removing the file \"%s\": %m"),pathname);
119                 return FALSE;
120                 }
121         
122         return TRUE;
123 }
124
125 gboolean bundle_util_file_write(const gchar *pathname,const gchar *basename,mode_t pathname_mode,gboolean pathname_backup)
126 {
127 const guint8 *data;
128 guint32 data_length;
129 int fd;
130
131         g_return_val_if_fail(pathname!=NULL,FALSE);
132         g_return_val_if_fail(basename!=NULL,FALSE);
133
134         if (!(data=bundle_util_file_retrieve(&data_length,basename)))
135                 return FALSE;
136
137         if (pathname_backup && !bundle_util_file_backup_conditional(pathname,basename))
138                 return FALSE;
139
140         if (-1==(fd=open(pathname,O_WRONLY|O_CREAT|O_TRUNC,pathname_mode))) {
141                 g_warning(_("Error opening the file \"%s\" for rewrite: %m"),pathname);
142                 return FALSE;
143                 }
144         if ((int)data_length!=write(fd,data,data_length)) {
145                 g_warning(_("Error writing the data of the file \"%s\" being overwritten: %m"),pathname);
146                 close(fd);      /* errors ignored */
147                 return FALSE;
148                 }
149         if (close(fd)) {
150                 g_warning(_("Error closing the file \"%s\" after its rewrite: %m"),pathname);
151                 return FALSE;
152                 }
153         return TRUE;
154 }