1 |
/**
|
2 |
* Navit, a modular navigation system.
|
3 |
* Copyright (C) 2005-2011 Navit Team
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
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
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
#define _FILE_OFFSET_BITS 64
|
20 |
#define _LARGEFILE_SOURCE
|
21 |
#define _LARGEFILE64_SOURCE
|
22 |
#include <unistd.h>
|
23 |
#include "maptool.h"
|
24 |
#include "debug.h"
|
25 |
|
26 |
char *
|
27 |
tempfile_name(char *suffix, char *name)
|
28 |
{
|
29 |
return g_strdup_printf("%s_%s.tmp", name, suffix);
|
30 |
}
|
31 |
|
32 |
FILE *
|
33 |
tempfile(char *suffix, char *name, int mode)
|
34 |
{
|
35 |
char *buffer = tempfile_name(suffix, name);
|
36 |
FILE *ret = NULL;
|
37 |
switch (mode)
|
38 |
{
|
39 |
case 0:
|
40 |
ret = fopen(buffer, "rb");
|
41 |
break;
|
42 |
case 1:
|
43 |
ret = fopen(buffer, "wb+");
|
44 |
break;
|
45 |
case 2:
|
46 |
ret = fopen(buffer, "ab");
|
47 |
break;
|
48 |
case 13:
|
49 |
ret = fopen(buffer, "rb+");
|
50 |
break;
|
51 |
}
|
52 |
|
53 |
if (debug_itembin(6))
|
54 |
{
|
55 |
fprintf(stderr, "== tempfile == open == FILENAME: %s FILEPOINTER: %p ==\n", buffer, ret);
|
56 |
}
|
57 |
g_free(buffer);
|
58 |
|
59 |
return ret;
|
60 |
}
|
61 |
|
62 |
void tempfile_unlink(char *suffix, char *name)
|
63 |
{
|
64 |
char buffer[4096];
|
65 |
sprintf(buffer, "%s_%s.tmp", name, suffix);
|
66 |
|
67 |
//fprintf(stderr, "== tempfile == unlink == FILENAME: %s ==\n", buffer);
|
68 |
|
69 |
unlink(buffer);
|
70 |
}
|
71 |
|
72 |
void tempfile_rename(char *suffix, char *from, char *to)
|
73 |
{
|
74 |
char buffer_from[4096], buffer_to[4096];
|
75 |
sprintf(buffer_from, "%s_%s.tmp", from, suffix);
|
76 |
sprintf(buffer_to, "%s_%s.tmp", to, suffix);
|
77 |
|
78 |
//fprintf(stderr, "== tempfile == rename == FILENAME FROM: %s FILENAME TO: %s ==\n", buffer_from, buffer_to);
|
79 |
|
80 |
dbg_assert(rename(buffer_from, buffer_to) == 0);
|
81 |
}
|
82 |
|
83 |
|
84 |
#include <fcntl.h>
|
85 |
#include <errno.h>
|
86 |
int tempfile_cp(const char *to, const char *from)
|
87 |
{
|
88 |
int fd_to, fd_from;
|
89 |
char buf[4096];
|
90 |
ssize_t nread;
|
91 |
int saved_errno;
|
92 |
|
93 |
fd_from = open(from, O_RDONLY);
|
94 |
if (fd_from < 0)
|
95 |
{
|
96 |
return -1;
|
97 |
}
|
98 |
|
99 |
fd_to = open(to, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
100 |
if (fd_to < 0)
|
101 |
{
|
102 |
goto out_error;
|
103 |
}
|
104 |
|
105 |
while (nread = read(fd_from, buf, sizeof buf), nread > 0)
|
106 |
{
|
107 |
char *out_ptr = buf;
|
108 |
ssize_t nwritten;
|
109 |
|
110 |
do {
|
111 |
nwritten = write(fd_to, out_ptr, nread);
|
112 |
|
113 |
if (nwritten >= 0)
|
114 |
{
|
115 |
nread -= nwritten;
|
116 |
out_ptr += nwritten;
|
117 |
}
|
118 |
else if (errno != EINTR)
|
119 |
{
|
120 |
goto out_error;
|
121 |
}
|
122 |
} while (nread > 0);
|
123 |
}
|
124 |
|
125 |
if (nread == 0)
|
126 |
{
|
127 |
if (close(fd_to) < 0)
|
128 |
{
|
129 |
fd_to = -1;
|
130 |
goto out_error;
|
131 |
}
|
132 |
close(fd_from);
|
133 |
|
134 |
/* Success! */
|
135 |
return 0;
|
136 |
}
|
137 |
|
138 |
out_error:
|
139 |
saved_errno = errno;
|
140 |
|
141 |
close(fd_from);
|
142 |
if (fd_to >= 0)
|
143 |
{
|
144 |
close(fd_to);
|
145 |
}
|
146 |
|
147 |
errno = saved_errno;
|
148 |
return -1;
|
149 |
}
|
150 |
|
151 |
void tempfile_copyrename(char *suffix, char *from, char *to)
|
152 |
{
|
153 |
char buffer_from[4096], buffer_to[4096];
|
154 |
sprintf(buffer_from, "%s_%s.tmp", from, suffix);
|
155 |
sprintf(buffer_to, "%s_%s.tmp", to, suffix);
|
156 |
|
157 |
//fprintf(stderr, "== tempfile == copyrename == FILENAME FROM: %s FILENAME TO: %s ==\n", buffer_from, buffer_to);
|
158 |
|
159 |
tempfile_cp(buffer_to, buffer_from);
|
160 |
unlink(buffer_from);
|
161 |
}
|
162 |
|