Repository navigation
Expand file tree
/
Copy pathtask.cpp
More file actions
483 lines (359 loc) · 12.1 KB
/
Copy pathtask.cpp
File metadata and controls
483 lines (359 loc) · 12.1 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* Author: Nikoloz Robakidze
* Email: nika42568@gmail.com
* Console application in which the user can perform activities on the file system
* using process abstraction. Namely, creating a file/directory, copying/moving data,
* changing rights, viewing contents.
* Program work scenario:
* 1. The user interacts with the main process of the program (main function) and
* selects the appropriate action (creating a file/directory,
* copying/moving a file/directory to another directory, copying file data to another file,
* writing accounting data to a file (for example, information about the contents of a directory),
* for a file Change permissions for /directory, view contents of file/directory)
* 2. The appropriate action performed by the user must be transmitted
* in the form of a command (text) to the process created for this purpose.
* Use the pipe channel to transmit the command, and use the fifo channel to specify
* the exact amount of data to be transferred (to_string() - converting an integer value
* into a string and stoi() - converting a string into an integer value,
* a function on the child process side).
*/
#include <iostream>
#include <string>
#include <unistd.h> // read
#include <sys/wait.h> // wait
#include <sys/stat.h> // umask mkfifo
#include <fcntl.h> // open
using namespace std;
/*
* Commander - class for working with terminal command.
* Printing list of commands to generate and execute in future.
* For command executing using std::system.
* Overred input >> and output << operators.
*/
class Commander
{
string command; // Terminal command.
public:
Commander() : command("echo \"Invalid value.\""){};
Commander(string usrCommand) : command(usrCommand){};
void printCommandList() const
{
// Create interface for application.
cout << endl
<< "Make the appropriate selection:" << endl
<< " 1. Create directory" << endl
<< " 2. Create file" << endl
<< " 3. Copy the file" << endl
<< " 4. Copy directory" << endl
<< " 5. Moving a file / directory" << endl
<< " 6. Copying file data to another file" << endl
<< " 7. Changing rights for a file / directory" << endl
<< " 8. Viewing file contents" << endl
<< " 9. Browse the contents of the directory" << endl
<< " 10. Recording accounting data in the file" << endl
<< " 11. Exit the program" << endl;
cout << endl
<< "Your choice: ";
}
void setCommand(int commandNumber)
{
// commandNumber - number from interface to generate terminal command.
// Set commands to numbers and set value to command key.
switch (commandNumber)
{
case 1:
{
cout << endl
<< "Enter directory name: ";
string newDirectoryName;
cin >> newDirectoryName;
this->command = "mkdir " + newDirectoryName;
break;
}
case 2:
{
cout << endl
<< "Enter filename: ";
string newFileName;
cin >> newFileName;
this->command = "touch " + newFileName;
break;
}
case 3:
{
cout << endl
<< "Enter filename: ";
string fileName, destinationDirectory;
cin >> fileName;
cout << endl
<< "Enter the name of the destination directory for the file copy: ";
cin >> destinationDirectory;
this->command = "cp \"" + fileName + "\" \"" + destinationDirectory + "/" + fileName + "\"";
break;
}
case 4:
{
cout << endl
<< "Enter directory name: ";
string directoryName, destinationDirectory;
cin >> directoryName;
cout << endl
<< "Enter the directory copy destination name: ";
cin >> destinationDirectory;
this->command = "cp -r \"" + directoryName + "\" \"" + destinationDirectory + "\"";
break;
}
case 5:
{
cout << endl
<< "Enter file/directory name: ";
string objectName, newDirectoryName;
cin >> objectName;
cout << endl
<< "Enter the new directory name of the file / directory: ";
cin >> newDirectoryName;
this->command = "mv \"" + objectName + "\" \"" + newDirectoryName + "\"";
break;
}
case 6:
{
cout << endl
<< "Enter filename: ";
string fileName, copyFileName;
cin >> fileName;
cout << endl
<< "Import a copy of the file filename: ";
cin >> copyFileName;
this->command = "cp \"" + fileName + "\" \"" + copyFileName + "\"";
break;
}
case 7:
{
cout << endl
<< "Enter file/directory name: ";
string objectName, modeCode;
cin >> objectName;
cout << endl
<< "Enter file/directory permissions: ";
cin >> modeCode;
this->command = "chmod " + modeCode + " \"" + objectName + "\"";
break;
}
case 8:
{
cout << endl
<< "Enter filename: ";
string fileName;
cin >> fileName;
this->command = "cat \"" + fileName + "\"";
break;
}
case 9:
{
cout << endl
<< "Enter directory name: ";
string directoryName;
cin >> directoryName;
this->command = "ls -al \"" + directoryName + "\"";
break;
}
case 10:
{
cout << endl
<< "Enter file name to record accounting data: ";
string fileName, directoryName;
cin >> fileName;
cout << endl
<< "Enter the name of the accounting directory: ";
cin >> directoryName;
// echo "$(pwd) - $(ls -l)" > information.txt
this->command = "echo \"" + directoryName + " - $(ls -l \"" + directoryName + "\")\" > " + "\"" + fileName + "\"";
break;
}
case 11:
{
cout << endl
<< "Successfully exited the program." << endl;
exit(0);
}
default:
{
cout << endl
<< "Such action is not defined. Try again." << endl;
break;
}
}
}
void setCommandFromBuffer(char *buff, size_t size)
{
/*
* Convert C string to C++ standart and set value to command key.
* It neccessary for reading data from PIPE.
*/
this->command = "";
for (int i = 0; i < size; i++)
{
this->command += buff[i];
}
}
string getCommand() const { return command; }
void executeCommand() const { system(this->command.c_str()); }
friend istream &operator>>(istream &in, Commander &obj)
{
int commandNumber;
in >> commandNumber;
obj.setCommand(commandNumber);
return in;
}
friend ostream &operator<<(ostream &out, Commander &obj)
{
out << obj.getCommand();
return out;
}
};
/*
* myPipe - using PIPE to move string data between processes.
* In this case PIPE using to transfer terminal command string from main
* to created process for command executing. Input command in main and
* executing it in child process.
*/
class myPipe
{
// Declare keys to save stream value of data transfer.
int rd_stream, // Read stream.
wr_stream; // Write stream.
public:
myPipe()
{
int tmp[2]; // Array for data trasfering.
// Try to create PIPE chain.
if (pipe(tmp) == -1)
{
cout << "Pipe chanel create unabled." << endl;
exit(-1); // Stop application if PIPE chain not created.
}
// Set data array for streams.
rd_stream = tmp[0];
wr_stream = tmp[1];
}
ssize_t write_data(char *data, size_t size) const
{
/* Write data into stream. */
return write(wr_stream, data, size);
}
ssize_t write_data_from_str(string data, size_t size) const
{
/* Convert C++ string to C standart then write data into stream. */
char buff[size];
for (int i = 0; i < data.size(); i++)
{
buff[i] = data[i];
}
return write_data(buff, size);
}
ssize_t read_data(char *data, size_t size) const
{
/* Reading data from stream. */
return read(rd_stream, data, size);
}
~myPipe()
{
/* Close transfer stream at object destoying. */
close(rd_stream);
close(wr_stream);
}
};
/*
* myFifo - using FIFO to move size (count of char) of string of terminal command
* to get correct size of data transfered by PIPE chain. Size of data must be converted
* to C++ string standart to move it through FIFO chain.
*/
class myFifo
{
int fd; // Key to save descriptor of rendered FIFO file. (open)
char *file; // Name of rendered FIFO file.
public:
myFifo(char *fileName) : file(fileName)
{
umask(0); // Change the access permissions of rendered FIFO file.
mkfifo(file, S_IFIFO | 0660); // Create FIFO channel.
fd = open(file, O_RDWR); // Open rendered FIFO file.
}
ssize_t write_data(char *data)
{
/* Write data to transfer it. */
return write(fd, data, sizeof(data));
}
ssize_t read_data(char *data)
{
/* Write data to receive it. */
return read(fd, data, sizeof(data));
}
ssize_t write_data_from_str(string data)
{
/* Convert C++ string to C standart then write data to transfer it. */
char buff[data.size()];
for (int i = 0; i < data.size(); i++)
{
buff[i] = data[i];
}
return write_data(buff);
}
~myFifo()
{
close(fd); /* Close opened in FIFO file at object destoying. */
unlink(file); /* Deleting rendered FIFO file. */
}
};
int main()
{
char file[] = "size.fifo";
myFifo mf(file); // Initialize FIFO channel.
myPipe *pp = new myPipe(); // Initialize PIPE channel.
// Fork proccess for applications working.
switch (fork())
{
case -1:
{
cout << "Child process could not be created." << endl;
break;
}
case 0:
{
/*
* Child process.
* For executing of selected by user terminal command.
*/
wait(nullptr);
// Block process, waiting while main process will end.
char data[1024]; // Array to save data recieved from FIFO channel.
mf.read_data(data); // Saving data recieved from FIFO channel.
string str(data); // Convert recieved data to string.
const size_t N = stoi(str); // Parse string to integer.
char buff[N]; // Array to save data recieved from PIPE channel.
pp->read_data(buff, N); // Saving data recieved from PIPE channel.
Commander *commander = new Commander();
commander->setCommandFromBuffer(buff, N);
commander->executeCommand();
break;
}
default:
{
/*
* Parent process. (main)
* To input and generate terminal command.
*/
Commander *comander = new Commander();
(*comander).printCommandList();
cin >> *comander; // Input number of command.
// Convert generated command C++ string to C standart.
string data = (*comander).getCommand().c_str();
ssize_t size = data.size(); // Count of chars int string.
// Write stringified size into FIFO channel.
mf.write_data_from_str(to_string(size));
// Write generated terminal command into PIPE channel.
pp->write_data_from_str(data, size);
break;
}
}
}