1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/*----------------------------------------------------------------------------
libtunepimp -- The MusicBrainz tagging library.
Let a thousand taggers bloom!
Copyright (C) Robert Kaye 2003
This file is part of libtunepimp.
libtunepimp is free software you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation either version 2 of the License, or
(at your option) any later version.
libtunepimp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libtunepimp if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: fileio.cpp 1416 2005-07-03 06:48:17Z robert $
----------------------------------------------------------------------------*/
#include <assert.h>
#include <errno.h>
#include <string>
#ifndef WIN32
#include <unistd.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
using namespace std;
#include "utf8/utf8util.h"
#include "fileio.h"
static int uniqueId = 0;
#define DB printf("%s:%d\n", __FILE__, __LINE__);
#ifdef __cplusplus
extern "C"
{
#endif
static const char *dirSep = "/";
static const char dirSepChar = '/';
static int copyAndDelete(const char *from, const char *to, const char *encoding);
#define BUF_SIZE 4096
TFILE *topen(const char *path, const char *mode, const char *encoding)
{
return (TFILE *)fopen(utf8ToEncoding(path, encoding).c_str(), mode);
}
size_t tread(void *ptr, size_t size, size_t nmemb, TFILE *stream)
{
return fread(ptr, size, nmemb, (FILE *)stream);
}
size_t twrite(const void *ptr, size_t size, size_t nmemb, TFILE *stream)
{
return fwrite(ptr, size, nmemb, (FILE *)stream);
}
int tseek(TFILE *stream, long offset, int whence)
{
return fseek((FILE *)stream, offset, whence);
}
long ttell(TFILE *stream)
{
return ftell((FILE *)stream);
}
int tclose(TFILE *stream)
{
return fclose((FILE*)stream);
}
int tflush(TFILE *stream)
{
return fflush((FILE *)stream);
}
int tunlink(const char *pathname, const char *encoding)
{
return unlink(utf8ToEncoding(pathname, encoding).c_str());
}
int trename(const char *oldpath, const char *newpath, const char *encoding)
{
int ret;
ret = rename(utf8ToEncoding(oldpath, encoding).c_str(), utf8ToEncoding(newpath, encoding).c_str());
if (ret && errno == EXDEV)
return copyAndDelete(oldpath, newpath, encoding);
return ret;
}
int tmkdir(const char *pathname, const char *encoding)
{
return mkdir(utf8ToEncoding(pathname, encoding).c_str(), 0755);
}
int trmdir(const char *pathname, const char *encoding)
{
return rmdir(utf8ToEncoding(pathname, encoding).c_str());
}
int taccess(const char *pathname, int mode, const char *encoding)
{
assert(mode == F_OK);
return access(utf8ToEncoding(pathname, encoding).c_str(), mode);
}
void tmktempname(const char *path, char *newPath, int newPathLen)
{
char *ptr, *temp;
temp = (char *)malloc(strlen(path) + 32);
ptr = strrchr(path, dirSepChar);
if (ptr)
{
int len = (int)(ptr - path);
strncpy(temp, path, len);
temp[len] = 0;
}
else
strcpy(temp, ".");
strcat(temp, dirSep);
sprintf(temp + strlen(temp), "libtp%d%d.temp", (int)getpid(), uniqueId++);
strncpy(newPath, temp, newPathLen - 1);
newPath[newPathLen - 1] = 0;
free(temp);
}
//---------------------------------------------------------------------------
static int copyAndDelete(const char *from, const char *to, const char *encoding)
{
TFILE *in, *out;
int ret = 0;
errno = 0;
in = topen(from, "rb", encoding);
if (in == NULL)
return -1;
out = topen(to, "wb", encoding);
if (out == NULL)
{
tclose(in);
return -1;
}
char *buf = new char[BUF_SIZE];
for(;;)
{
int numRead = tread(buf, sizeof(char), BUF_SIZE, in);
if (numRead <= 0)
break;
int numWritten = twrite(buf, sizeof(char), numRead, out);
if (numWritten != numRead)
{
//err = string("Could not write file to destination directory. Disk full?");
ret = -1;
break;
}
}
tclose(in);
tclose(out);
delete [] buf;
if (ret == 0)
{
ret = tunlink(from, encoding);
if (ret < 0)
{
//err = string("Could remove old file: '") + from + string("'.");
tunlink(to, encoding);
}
}
return ret;
}
#ifdef __cplusplus
}
#endif
|