-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.c
303 lines (268 loc) · 6.41 KB
/
core.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
core.c
dispatcher and forks
this does the work.
FIXME close unneeded file descriptors (there are a lot)
*/
#include "ourdefs.h"
#include <sys/wait.h>
int
core (void)
{
int r = 0;
int flag;
int i, j, k; // local, global may exist
int status;
int counter;
int justwrite;
int filesprocessed = 0;
char infile[BUFFER]; // local, global may exist
char outfile[BUFFER]; // local, global may exist
char *string; // local, global may exist
char needle[255]; // null, just for validtype()
char *hillary; // null, just for validtype()
pid_t pids[cpus];
pid_t wpid;
int tochildfd[cpus][2];
int toparentfd[cpus][2];
int childcounter;
int height, width;
int nheight, nwidth;
// nanosleep does not work on old (2.x) kernels
struct timespec ctim, ctim2;
struct timespec ptim, ptim2;
ctim.tv_sec = 0;
ctim.tv_nsec = 5000L;
ptim.tv_sec = 0;
ptim.tv_nsec = 500000L;
//MagickWand *magick_wand;
// locals declared ...
//cpus = 1;
hillary = malloc (1024);
string = malloc (BUFFER);
childcounter=0;
//mainly for parent
for (i = 0; i < BUFFER; i++)
infile[i] = 0;
for (i = 0; i < cpus; i++)
if (pipe (tochildfd[i]) < 0)
{
printf ("pipe error\n");
exit (1);
}
for (i = 0; i < cpus; i++)
if (pipe (toparentfd[i]) < 0)
{
printf ("pipe error\n");
exit (1);
}
for (i = 0; i < cpus; ++i)
{
if ((pids[i] = fork ()) < 0)
{
perror ("fork");
exit (1);
}
else if (pids[i] == 0)
{
/* child */
for (j = 0; j < cpus; j++)
{
close(tochildfd[j][1]);
close(toparentfd[j][0]);
if (j!=i)
{
close(tochildfd[j][0]);
close(toparentfd[j][1]);
}
}
// printf ("child %i active...\n", i);
MagickWandGenesis();
while (1)
{
childcounter++;
// clean up from time to time
if ((childcounter%100)==27)
{
MagickWandTerminus();
MagickWandGenesis();
}
// wait for signal
for (j = 0; j < BUFFER; j++)
infile[j] = 0;
flag=0;
while (read (tochildfd[i][0], infile, BUFFER) == 0)
nanosleep(&ctim , &ctim2);
if (strncmp (infile, "stop", 4) == 0) // all done
{
// printf ("child %i has stopped\n", i);
MagickWandTerminus();
close(tochildfd[i][0]);
close(toparentfd[i][1]);
free(string);
exit (0);
}
// we have a filename, so
// printf ("child %i got %s\n", i, infile);
// check it is valid
string = strdup (infile);
needle[0] = '/';
needle[1] = 0;
strcat (needle, newdir);
if (strstr (string, needle) == NULL)
// printf ("invalid infile\n");
// else
{
// calculate outfile
// should do a function for this:
string = strdup (infile);
strcpy (outfile, dirname (string));
strcat (outfile, "/");
strcat (outfile, newdir);
strcat (outfile, "/");
string = strdup (infile);
strcat (outfile, basename (string));
string = strdup (infile);
outfile[strlen (outfile) - validtype (string, NULL, &hillary,1)] = 0;
strcat (outfile, newdir);
strcat (outfile, ".");
strcat (outfile, hillary);
//terrible fix but ... seems to work! - form batch
outfile[strlen (outfile) - 1] = infile[strlen (infile) - 1];
outfile[strlen (outfile) - 2] = infile[strlen (infile) - 2];
outfile[strlen (outfile) - 3] = infile[strlen (infile) - 3];
outfile[strlen (outfile) - 4] = infile[strlen (infile) - 4];
// printf ("(%s)(%s)\n", infile, outfile);
// make sure it does not exist
//if ((checkpointer = fopen (outfile, "r")) != NULL)
if (alreadythere(outfile))
{
// printf ("file exists\n");
flag=0;
// fclose (testpointer);
}
else
{
// printf ("opening..\n");
// open infile
m_wand = NewMagickWand ();
MagickReadImage (m_wand, infile);
width = MagickGetImageWidth (m_wand);
height = MagickGetImageHeight (m_wand);
// is it bigger than newsize in at least 1 diection
justwrite=0;
if ((width < newsize) && (height < newsize)&& (alwaysresize==0))
// {
// printf (" image (%i x %i)less than %i - unchanged\n", width,height,newsize);
justwrite=1;
// }
// and the other is not zero
//else
{
if ((width != 0) && (height != 0))
// printf (" image has zero size\n");
{
// ok, then resize and write.
if (width > height)
{
nwidth = newsize;
nheight = height * newsize;
nheight = nheight / width;
}
else
{
nheight = newsize;
nwidth = width * newsize;
nwidth = nwidth / height;
}
if (justwrite==0)
{
MagickResizeImage (m_wand, nwidth, nheight, FILTER);
MagickSetImageCompressionQuality (m_wand, 95);
}
// close
if (MagickWriteImage (m_wand, outfile) == MagickFalse)
flag=0;
else
flag=1;
// printf ("\n");
}
}
if(m_wand) m_wand = DestroyMagickWand(m_wand);
} // end of file exists
}
// tell parent we are done.
// printf ("telling parent we are done \n");
if (flag)
write (toparentfd[i][1], "done", 4);
else
write (toparentfd[i][1], "failed", 6);
}
MagickWandTerminus();
}
}
/* parent */
// you could let user up + down CPUs from here
for (j = 0; j < cpus; j++)
{
close(tochildfd[j][0]);
close(toparentfd[j][1]);
}
printf ("I am the parent \n");
//int flag = (2 << (cpus + 1)) - 1; // ??
counter = 0;
// send off first batch
for (i = 0; i < cpus; i++)
{
if (counter < filestoprocess) // you never know
write (tochildfd[i][1], filelist[counter], strlen (filelist[counter]));
else
write (tochildfd[i][1], "stop", 4);
counter++;
}
// loop
i = 0;
while (counter < (filestoprocess + cpus))
{
nanosleep(&ptim , &ptim2);
// check for children done
if ((k=read (toparentfd[i][0], infile, BUFFER)) != 0)
// more to do ? send another
{
if (strncmp(infile,"done",4)==0)
filesprocessed++;
if (counter >= filestoprocess)
{
write (tochildfd[i][1], "stop", 4);
printf ("\nchild %i has been sent stop ", i);
}
else
{
write (tochildfd[i][1], filelist[counter], strlen (filelist[counter]));
printf("\033[1A"); // go up 1 lines
printf("%c[2K", 27); // clear entire line
printf("processed %i, written %i of %i\n",counter,filesprocessed,filestoprocess);
}
counter++;
}
// else tell child to stop, and flag
i++;
if (i == cpus)
i = 0;
}
// make sure all finished
while ((wpid = wait (&status)) > 0)
;
printf ("\n\n%i files examined, %i converted\n", filestoprocess,
filesprocessed);
printf ("bye from parent \n");
// cleanup
free(hillary);
free(string);
for (j = 0; j < cpus; j++)
{
close(tochildfd[j][1]);
close(toparentfd[j][0]);
}
return (r);
}