-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmap_read.c
More file actions
185 lines (154 loc) · 7.59 KB
/
Copy pathmmap_read.c
File metadata and controls
185 lines (154 loc) · 7.59 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<errno.h>
void error(char *s)
{ printf("ERROR:%s ERRNO:%s\n", s,strerror(errno));
exit(-1);
}
int main(int argc, char **argv)
{ struct for_send
{ char name[255];
mode_t mode;
uid_t uid;
gid_t gid;
off_t size;
time_t mtime;
} *send;
int i;
int datafd;
DIR *dirfd;
char *pathdir=argv[1];
char *path, *str_send;
struct dirent *file;
struct stat buf;
struct passwd *for_login;
struct group *for_group;
char *permission_dec;
permission_dec = (char *)calloc( 3, sizeof(char));
int size_send=0, length =0;
umask(0);
struct sembuf mybuf; //создаем семафоры
key_t key;
int semid;
char pathname[] = "manager.c";
if(( key = ftok( pathname, 0) ) < 0)
error("Can't generate key");
if ( (semid = semget(key, 4, 0666 | IPC_CREAT )) < 0)
error("Can't get semid");
mybuf.sem_op = 1;
mybuf.sem_flg = 0;
mybuf.sem_num = 1;
if((semop( semid, &mybuf, 1) )< 0) //bufer empty
error("Can't wait for condition");
mybuf.sem_op = 1;
mybuf.sem_flg = 0;
mybuf.sem_num = 0;
if((semop( semid, &mybuf, 1) )< 0) //critical section
error("Can't wait for condition");
mybuf.sem_op = 1;
mybuf.sem_flg = 0;
mybuf.sem_num = 3;
if((semop( semid, &mybuf, 1) )< 0) //permission for write.c
error("Can't wait for condition");
if ((dirfd=opendir(argv[1]))==NULL)
error("Can't open directory");
if((datafd=open("mapped.dat", O_RDWR | O_CREAT | O_TRUNC, 0666))<0)
error("Can't open file");
int len=strlen(pathdir); //длина имени директории
if (pathdir[len-1]!='/') //форматируем имя директории
{ path=(char*)calloc(len+2, sizeof(char));
strcpy(path, pathdir);
path[len]='/';
path[len+1]='\0';
len++;
pathdir=path;
}
length = sizeof(struct for_send);
while((file = readdir(dirfd))!=NULL)
{
if((strcmp(file->d_name, ".")!=0)&&(strcmp(file->d_name,"..")!=0))// если очередная запись соответсвует текущей директории или родительской, то не обрабатываем
{
path=(char*)calloc((strlen(file->d_name)+len),sizeof(char)); //путь к файлу
strcpy(path, pathdir);
strcpy(&(path[len]),file->d_name);
if((stat(path, &buf)) < 0) //читаем атрибуты этого файла
error( "Can't get file's data");
if ((S_ISDIR(buf.st_mode)) != 1) //есди директория, то
{
ftruncate( datafd, length); //увеличиваем размер файла, до размера строки+ 1 символ на \0
mybuf.sem_op = -1;
mybuf.sem_flg = 0;
mybuf.sem_num = 1;
if((semop( semid, &mybuf, 1) )< 0) //bufer not empty
error("Can't wait for condition");
mybuf.sem_op = -1;
mybuf.sem_flg = 0;
mybuf.sem_num = 0;
if((semop( semid, &mybuf, 1) )< 0) //critical section
error("Can't wait for condition");
send = (struct for_send*)mmap(NULL, length, PROT_WRITE | PROT_READ, MAP_SHARED, datafd, 0); //отображаем файл в память
if(send == MAP_FAILED)
error("Mapping failed!");
for(i=0;i<255;i++)send->name[i] = 0;
strcat(send->name,file->d_name);
send->mode = buf.st_mode;
send->uid = buf.st_uid;
send->gid = buf.st_gid;
send->size =buf.st_size;
send->mtime = buf.st_mtime;
munmap( (void*)send, length); //прекращаем отображать в память
mybuf.sem_op = 1;
mybuf.sem_flg = 0;
mybuf.sem_num = 0;
if((semop( semid, &mybuf, 1) )< 0) //out of critical section
error("Can't wait for condition");
mybuf.sem_op = 1;
mybuf.sem_flg = 0;
mybuf.sem_num = 2;
if((semop( semid, &mybuf, 1) )< 0) //bufer full
error("Can't wait for condition");
}
}
}
//после отправки последнего файла отправляем отсеянный ранее файл, чтобы wriet.c узнал о конце работы
mybuf.sem_op = -1;
mybuf.sem_flg = 0;
mybuf.sem_num = 1;
if((semop( semid, &mybuf, 1) )< 0) //bufer not empty
error("Can't wait for condition");
mybuf.sem_op = -1;
mybuf.sem_flg = 0;
mybuf.sem_num = 0;
if((semop( semid, &mybuf, 1) )< 0) //critical section
error("Can't wait for condition");
send = (struct for_send*) mmap(NULL, length, PROT_WRITE | PROT_READ, MAP_SHARED, datafd, 0);
if(send == MAP_FAILED)
error("Mapping failed!");
for(i=0;i<255;i++)send->name[i] = 0;
strcat(send->name, "..");
munmap( (void*)send, length);
mybuf.sem_op = 1;
mybuf.sem_flg = 0;
mybuf.sem_num = 0;
if((semop( semid, &mybuf, 1) )< 0) //out of critical section
error("Can't wait for condition");
mybuf.sem_op = 1;
mybuf.sem_flg = 0;
mybuf.sem_num = 2;
if((semop( semid, &mybuf, 1) )< 0) //bufer full
error("Can't wait for condition");
close(datafd);
return 0;
}