-
Notifications
You must be signed in to change notification settings - Fork 67
/
imageAccess.c
212 lines (154 loc) · 4.64 KB
/
imageAccess.c
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "pharovm/pharo.h"
#include "pharovm/imageAccess.h"
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "pharovm/stringUtilities.h"
#if !defined(_WIN32)
#include <unistd.h>
#endif
/*
* The read and write function uses a 128kb chunk size.
* It is based in the analysis of how cp, cat and other tools access the disk
* Check https://eklitzke.org/efficient-file-copying-on-linux
*/
/* 128 Kb */
#define CHUNK_SIZE 128 * 1024
#define BARLENGTH 50
static int showOutputInConsole = false;
static char* progressText = "";
sqInt basicImageFileClose(sqImageFile f){
return fclose((FILE*)f);
}
sqImageFile basicImageFileOpen(const char* fileName, char *mode){
#ifndef _WIN32
return fopen(fileName, mode);
#else
/*
* In Win32, the filename if using fopen only works with ANSI characters.
* We need to use the wide version.
*/
WCHAR * wideFileName;
WCHAR * wideMode;
FILE * f;
wideFileName = vm_string_convert_utf8_to_utf16(fileName);
wideMode = vm_string_convert_utf8_to_utf16(mode);
f = _wfopen(wideFileName, wideMode);
free(wideFileName);
free(wideMode);
return f;
#endif
}
long int basicImageFilePosition(sqImageFile f){
return ftell((FILE*)f);
}
size_t basicImageFileRead(void * initialPtr, size_t sz, size_t count, sqImageFile f){
size_t readBytes = 0;
size_t bytesToRead = sz * count;
size_t lastReadBytes = 0;
size_t chunkToRead = 0;
size_t remainingBytes = 0;
char* currentPtr = initialPtr;
if(bytesToRead <= CHUNK_SIZE){
return fread(initialPtr, sz, count, (FILE*)f);
}
#ifdef posix_fadvise
off_t initialPosition;
initialPosition = sqImageFilePosition(f);
if(posix_fadvise(fileno(f), initialPosition, bytesToRead, POSIX_FADV_SEQUENTIAL)){
logErrorFromErrno("posix_fadvise");
}
#endif
remainingBytes = bytesToRead;
do{
chunkToRead = remainingBytes < CHUNK_SIZE? remainingBytes : CHUNK_SIZE;
lastReadBytes = fread(currentPtr, 1, chunkToRead, (FILE*)f);
if(lastReadBytes < chunkToRead){
logErrorFromErrno("fread");
return lastReadBytes;
}
readBytes += lastReadBytes;
currentPtr += lastReadBytes;
remainingBytes -= lastReadBytes;
sqImageReportProgress(bytesToRead, readBytes);
} while(lastReadBytes > 0 && readBytes < bytesToRead);
if(bytesToRead != readBytes){
logError("Error reading expected to read: %lld actual read:%lld", (long long)bytesToRead, (long long)readBytes);
}
return readBytes;
}
int basicImageFileSeek(sqImageFile f, long int pos){
return fseek((FILE*)f, pos, SEEK_SET);
}
int basicImageFileSeekEnd(sqImageFile f, long int pos){
return fseek((FILE*)f, pos, SEEK_END);
}
size_t basicImageFileWrite(void* initialPtr, size_t sz, size_t count, sqImageFile f){
size_t wroteBytes = 0;
size_t remainingBytes = 0;
size_t bytesToWrite = sz * count;
size_t lastWriteBytes = 0;
size_t chunkToWrite = 0;
char* currentPtr = initialPtr;
if(bytesToWrite <= CHUNK_SIZE){
return fwrite(initialPtr, sz, count, (FILE*)f);
}
remainingBytes = bytesToWrite;
do{
chunkToWrite = remainingBytes < CHUNK_SIZE ? remainingBytes : CHUNK_SIZE;
lastWriteBytes = fwrite(currentPtr, 1, chunkToWrite, (FILE*)f);
if(lastWriteBytes != chunkToWrite){
logErrorFromErrno("fwrite");
return lastWriteBytes + wroteBytes;
}
wroteBytes += chunkToWrite;
currentPtr += lastWriteBytes;
remainingBytes -= lastWriteBytes;
sqImageReportProgress(bytesToWrite, wroteBytes);
} while(bytesToWrite > wroteBytes);
if(bytesToWrite != wroteBytes){
logError("Error reading expected to write: %lld actual wrote:%lld", (long long)bytesToWrite, (long long)wroteBytes);
}
return bytesToWrite;
}
int basicImageFileExists(const char* aPath){
struct stat st;
return stat(aPath, &st) == 0;
}
void basicImageReportProgress(size_t totalSize, size_t currentSize){
int i;
char bar[BARLENGTH + 1];
bar[BARLENGTH] = 0;
if(!showOutputInConsole)
return;
if(totalSize){
int percentage = currentSize * 100 / totalSize;
for(i = 0; i < BARLENGTH; i++){
bar[i] = percentage >= ((i+1) * (100/BARLENGTH)) ? '#' : '-';
}
printf("\r%s: [%s] %d%%", progressText, bar, percentage);
}else{
printf("\r%s...", progressText);
}
if(totalSize <= currentSize)
printf("\n");
fflush(stdout);
}
FileAccessHandler defaultFileAccessHandler = {
basicImageFileClose,
basicImageFileOpen,
basicImageFilePosition,
basicImageFileRead,
basicImageFileSeek,
basicImageFileSeekEnd,
basicImageFileWrite,
basicImageFileExists,
basicImageReportProgress
};
FileAccessHandler* fileAccessHandler = &defaultFileAccessHandler;
FileAccessHandler* currentFileAccessHandler(){
return fileAccessHandler;
}
void setFileAccessHandler(FileAccessHandler* aFileAccessHandler){
fileAccessHandler = aFileAccessHandler;
}