-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender-diskio.c
107 lines (94 loc) · 1.91 KB
/
sender-diskio.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
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "log.h"
#include "fifo.h"
#include "udp-sender.h"
#include "udpcast.h"
#include "udpc_process.h"
#define BLOCKSIZE 4096
#ifdef O_BINARY
#include <io.h>
#define HAVE_O_BINARY
#endif
#ifndef O_BINARY
# define O_BINARY 0
#endif
int openFile(struct disk_config *config)
{
if(config->fileName != NULL) {
int in = open(config->fileName, O_RDONLY | O_BINARY, 0);
if (in < 0) {
#ifdef NO_BB
#ifndef errno
extern int errno;
#endif
#endif
udpc_fatal(1, "Could not open file %s: %s\n", config->fileName,
strerror(errno));
}
return in;
} else {
#ifdef __MINGW32__
setmode(0, O_BINARY);
#endif
return 0;
}
}
int openPipe(struct disk_config *config, int in, int *pidp)
{
/**
* Open the pipe
*/
*pidp=0;
if(config->pipeName != NULL) {
/* pipe */
char *arg[256];
int filedes[2];
udpc_parseCommand(config->pipeName, arg);
if(pipe(filedes) < 0) {
perror("pipe");
exit(1);
}
#ifdef HAVE_O_BINARY
setmode(filedes[0], O_BINARY);
setmode(filedes[1], O_BINARY);
#endif
*pidp=open2(in, filedes[1], arg, filedes[0]);
close(filedes[1]);
in = filedes[0];
}
return in;
}
/**
* This file is reponsible for reading the data to be sent from disk
*/
int localReader(struct fifo *fifo, int in)
{
while(1) {
int pos = pc_getConsumerPosition(fifo->freeMemQueue);
int bytes =
pc_consumeContiguousMinAmount(fifo->freeMemQueue, BLOCKSIZE);
if(bytes > (pos + bytes) % BLOCKSIZE)
bytes -= (pos + bytes) % BLOCKSIZE;
if(bytes == 0)
/* net writer exited? */
break;
bytes = read(in, fifo->dataBuffer + pos, bytes);
if(bytes < 0) {
perror("read");
exit(1);
}
if (bytes == 0) {
/* the end */
pc_produceEnd(fifo->data);
break;
} else {
pc_consumed(fifo->freeMemQueue, bytes);
pc_produce(fifo->data, bytes);
}
}
return 0;
}