-
Notifications
You must be signed in to change notification settings - Fork 2
/
writefile.c
159 lines (144 loc) · 4.53 KB
/
writefile.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
//License: Apache License 2.0
//See LICENSE for details
//Authors (in alphabet order of last name):
// - Qijiang Fan <[email protected]>
// - Bin He <[email protected]>
// - Cheng Zhang <[email protected]>
// - Shiwei Zhou <[email protected]>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <math.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
typedef struct fileStruct {
char fileName[256];
long int fileSize;
int fileMode;
struct fileStruct *next;
} fileStruct;
void switchMemory(int *currentMemory, void **writeMemory, void *memory1,
void *memory2, pthread_mutex_t *m1lockw, pthread_mutex_t *m1lockr, pthread_mutex_t *m2lockw, pthread_mutex_t *m2lockr)
{
*currentMemory = -(*currentMemory); //可以在这个函数中应用读写锁
switch (*currentMemory)
{
case -1:
pthread_mutex_unlock(m2lockr);
pthread_mutex_unlock(m2lockw);
pthread_mutex_lock(m1lockr);
*writeMemory = memory1 + sizeof(int);
break;
case 1:
pthread_mutex_unlock(m1lockr);
pthread_mutex_unlock(m1lockw);
pthread_mutex_lock(m2lockr);
*writeMemory = memory2 + sizeof(int);
break;
default:
fprintf(stderr, "switch mem error");
exit(1);
break;
}
//printf("Write Switch %d\n", *currentMemory);
}
int mkpath(char* file_path, mode_t mode) {
assert(file_path && *file_path);
char* p;
for (p=strchr(file_path+1, '/'); p; p=strchr(p+1, '/')) {
*p='\0';
if (mkdir(file_path, mode)==-1) {
if (errno!=EEXIST) { *p='/'; return -1; }
}
*p='/';
}
return 0;
}
void WriteFile(void *memory1, void *memory2, int bufSize, pthread_mutex_t *m1lockw, pthread_mutex_t *m1lockr, pthread_mutex_t *m2lockw, pthread_mutex_t *m2lockr)
{
bufSize -= sizeof(int);
long int remainMemory = bufSize;
int currentMemory = -1; //标志当前内存
void *writeMemory = memory1 + sizeof(int);
void *p = writeMemory;
pthread_mutex_lock(m1lockr);
while (*(int*)memory1 == 0) { pthread_mutex_unlock(m1lockr); usleep(100); pthread_mutex_lock(m1lockr); }
while (1) {
//printf("Write remain %d\n", remainMemory);
if (remainMemory < sizeof(fileStruct)) {
switchMemory(¤tMemory, &p, memory1, memory2, m1lockw, m1lockr, m2lockw, m2lockr);
remainMemory = bufSize;
}
fileStruct * nowfile = (fileStruct *) p;
char filename[256]="";
strcpy(filename,nowfile->fileName);
printf("will write %s",filename);
mkpath(filename,0755);
int fd = open(filename , O_CREAT, nowfile->fileMode);
if(!fd){
printf("create file error");
exit(1);
}
close(fd);
int f=open(filename,O_WRONLY);
printf("size:%dmode:%d",nowfile->fileSize,nowfile->fileMode);
fileStruct * endWrite = nowfile->next;
remainMemory -= sizeof(fileStruct);
if (nowfile->fileSize < remainMemory) {
write(f, nowfile + 1, nowfile->fileSize);
remainMemory -= nowfile->fileSize;
p += sizeof(fileStruct);
p += nowfile->fileSize;
close(f);
}
else if (nowfile->fileSize == remainMemory) {
write(f, nowfile + 1, nowfile->fileSize);
remainMemory = bufSize;
switchMemory(¤tMemory, &p, memory1, memory2, m1lockw, m1lockr, m2lockw, m2lockr);
close(f);
}
else if (nowfile->fileSize > remainMemory) {
write(f, nowfile + 1, remainMemory);
int fileSize = nowfile->fileSize;
switchMemory(¤tMemory, &p, memory1, memory2, m1lockw, m1lockr, m2lockw, m2lockr);
int writeTimes = ceil((nowfile->fileSize - remainMemory) / (float)bufSize); //改编译参数
int i = 1;
for (i = 1; i < writeTimes;i++)
{
write(f, p, bufSize);
switchMemory(¤tMemory, &p, memory1,
memory2, m1lockw, m1lockr, m2lockw, m2lockr);
}
int lastSize =
fileSize - remainMemory - (writeTimes -
1) * bufSize;
write(f, p, lastSize);
p += lastSize;
remainMemory = bufSize - lastSize;
if (!remainMemory) {
remainMemory = bufSize;
switchMemory(¤tMemory, &p, memory1,
memory2, m1lockw, m1lockr, m2lockw, m2lockr);
}
close(f);
}
if (!endWrite)
break;
}
if (currentMemory == -1) { pthread_mutex_unlock(m1lockr); pthread_mutex_unlock(m1lockw); }
else { pthread_mutex_unlock(m2lockr); pthread_mutex_unlock(m2lockw);}
/*while(((fileStruct *)readMemory)->next!=NULL)
{
currentFile=(fileStruct *)readMemory;
readMemory=readMemory+sizeof(fileStruct);
remainMemory=bufferSize-sizeof(fileStruct);
fp=fopen(ergodic->fileName,"r");
if(currentFile->fileSize<=remainMemory)
{
}
//} */
}