83057b2161c6efde4428e5b44f60e6358d35db8d
[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
36
37 static const guint8 *bundle_util_file_retrieve(guint32 *data_length_return,const gchar *pathname,const gchar *basename)
38 {
39 const guint8 *data;
40
41         g_return_val_if_fail(data_length_return!=NULL,NULL);
42         g_return_val_if_fail(pathname!=NULL,NULL);
43         g_return_val_if_fail(basename!=NULL,NULL);
44
45         data=g_hash_table_lookup(bundle_hash_new(),basename);
46         g_return_val_if_fail(data!=NULL,NULL);
47         *data_length_return=GUINT32_FROM_BE(*(guint32 *)data);
48         data+=sizeof(*data_length_return);
49
50         return data;
51 }
52
53 /* Returns TRUE if it safe to overwrite/unlink the file.
54  */
55 static gboolean bundle_util_file_backup_conditional(const gchar *pathname,const gchar *basename)
56 {
57 const guint8 *data;
58 guint32 data_length;
59 int fd;
60 guint8 *data_found;
61 int got;
62 char strftime_buffer[LINE_MAX];
63 const gchar *destination;
64 time_t time_current;
65
66         g_return_val_if_fail(pathname!=NULL,FALSE);
67         g_return_val_if_fail(basename!=NULL,FALSE);
68
69         if (!(data=bundle_util_file_retrieve(&data_length,pathname,basename)))
70                 return FALSE;
71
72         if (-1==(fd=open(pathname,O_RDONLY))) {
73                 if (errno==ENOENT)
74                         return TRUE;
75                 else {
76                         g_warning(_("Error checking file modifications of \"%s\": %m"),pathname);
77                         return FALSE;
78                         }
79                 }
80
81         /* WARNING: 'data_found' allocated in this block! */
82         data_found=g_malloc(data_length+1);
83         if (-1==(got=read(fd,data_found,data_length+1)))
84                 g_warning(_("Error reading during the check of file modifications of \"%s\": %m"),pathname);
85         if (close(fd))
86                 g_warning(_("Error closing the file \"%s\" during the check of its modifications: %m"),pathname);
87         g_free(data_found);
88
89         if (got==(int)data_length && !memcmp(data_found,data,data_length))
90                 return TRUE;
91
92         time_current=time(NULL);        /* It is segfault to gmtime(NULL). */
93         if (!strftime(strftime_buffer,sizeof(strftime_buffer),"GMT%FT%T",gmtime(&time_current))) {
94                 g_warning("strftime(3): %m");   /* shouldn't happen */
95                 return FALSE;
96                 }
97         destination=udpgate_printf_alloca("%s-%s-%d",pathname,strftime_buffer,(int)getpid());
98         if (rename(pathname,destination)) {
99                 g_warning(_("Error renaming your modified file \"%s\" to the backup \"%s\", giving up: %m"),
100                                 pathname,destination);
101                 return FALSE;
102                 }
103
104         return TRUE;
105 }
106
107 gboolean bundle_util_file_remove(const gchar *pathname,const gchar *basename)
108 {
109         g_return_val_if_fail(pathname!=NULL,FALSE);
110         g_return_val_if_fail(basename!=NULL,FALSE);
111
112         if (!bundle_util_file_backup_conditional(pathname,basename))
113                 return FALSE;
114
115         if (unlink(pathname) && errno!=ENOENT) {
116                 g_warning(_("Error removing the file \"%s\": %m"),pathname);
117                 return FALSE;
118                 }
119         
120         return TRUE;
121 }
122
123 gboolean bundle_util_file_write(const gchar *pathname,const gchar *basename,mode_t pathname_mode)
124 {
125 const guint8 *data;
126 guint32 data_length;
127 int fd;
128
129         g_return_val_if_fail(pathname!=NULL,FALSE);
130         g_return_val_if_fail(basename!=NULL,FALSE);
131
132         if (!(data=bundle_util_file_retrieve(&data_length,pathname,basename)))
133                 return FALSE;
134
135         if (!bundle_util_file_backup_conditional(pathname,basename))
136                 return FALSE;
137
138         if (-1==(fd=open(pathname,O_WRONLY|O_CREAT|O_TRUNC,pathname_mode))) {
139                 g_warning(_("Error opening the file \"%s\" for rewrite: %m"),pathname);
140                 return FALSE;
141                 }
142         if ((int)data_length!=write(fd,data,data_length)) {
143                 g_warning(_("Error writing the data of the file \"%s\" being overwritten: %m"),pathname);
144                 close(fd);      /* errors ignored */
145                 return FALSE;
146                 }
147         if (close(fd)) {
148                 g_warning(_("Error closing the file \"%s\" after its rewrite: %m"),pathname);
149                 return FALSE;
150                 }
151         return TRUE;
152 }