-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.cpp
More file actions
689 lines (576 loc) · 20.4 KB
/
FileManager.cpp
File metadata and controls
689 lines (576 loc) · 20.4 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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*Author: Michael Giancola
*Date: 03/10/2019
*Description: This file contains the implementation for my FileManager class. It is used to manipulate a file in many ways.
*Data fom this file is extracted and used to create C++ utlilites that help manage files on a Linux system.
**/
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <cmath>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include "FileManager.h"
using namespace std;
/*This is a default constructor that is not used but is implemented if neccessary.*/
FileManager::FileManager(){}
/*Function name: FileManager constructor
*Parameters: The constructor of FileManager has a char* argument which is where the name of the file
*that is to be manipulated is passed through.
*Description:This constructor will use multiple Linux System functions to extract data and contents from the file
*that will be used too help create the C++ utilities. The constructor is where all of the member data is initialized.
*Return: This constructor returns a new instance of the FileManager class so that the object can be used to manipulate
*the specifed file.
**/
FileManager::FileManager(char *file_name)
{
this->file_name = file_name;
struct stat buf;
this->error_num = 0;
if (stat(file_name, &buf) == -1)
{
this->error_num = errno;
this->user_name = "Destroy Me";
//do something here to indicate that this object should be destroyed
}
else
{
this->type = buf.st_mode;
this->size = buf.st_size;
this->user_id = buf.st_uid;
if (getpwuid(this->user_id) == NULL)
{
this->error_num = errno;
}
else
{
this->user_name = getpwuid(this->user_id)->pw_name;
this->group_id = buf.st_gid;
if (getgrgid(this->group_id) == NULL)
{
this->error_num = errno;
}
else
{
this->group_name = getgrgid(this->group_id)->gr_name;
this->permissons = buf.st_mode;
this->access = ctime(&buf.st_atime);
this->modifcation = ctime(&buf.st_mtime);
this->status_change = ctime(&buf.st_ctime);
this->block_size = buf.st_blksize;
this->error_num = 0;
}
}
}
}
/*Function name: FileManager destructor
*Parameters: No parameters.
*Description: The destructor is used to deallocate any dynamic memory that is being allocated in this file and cleans up any objects
*that must be destroyed after going out of scope.
*Return: No return type.
**/
FileManager::~FileManager(){}
/*Function name: getName()
*Parameters: No parameters.
*Description: This function is a getter function that retreives the file's name that was initlized and stored
*in a private attribute. This getter allows you to retrieve the private member outside of the class.
*Return: This function returns a char* which is the name of the file.
**/
char* FileManager::getName()
{
//cout << "the test final: " << this->file_name << endl;
return this->file_name;
}
/*Function name: getType()
*Parameters: No parameters.
*Description: This function is a getter function that retreives the file's type that was initlized and stored
*in a private attribute. This getter allows you to retrieve the private member outside of the class. This type member
*will eventually be used to retrieve more specific data from the file.
*Return: This getter returns a mode_t type which many of the system Linux functions require as arguments.
**/
mode_t FileManager::getType()
{
return this->type;
}
/*Function name: getSize()
*Parameters: No parameters.
*Description: This function is a getter function that retreives the file's size in bytes that was initlized and stored
*in a private attribute. This getter allows you to retrieve the private member outside of the class. This size member
*tells us what the size of the file we are working with is in bytes and will be important to us in the program so that
*we can later read and write to other files.
*Return: This getter returns a mode_t type and stores the number of bytes in the file.
**/
mode_t FileManager::getSize()
{
return this->size;
}
/*Function name: getUserID()
*Parameters: No parameters.
*Description: This function is a getter function that retreives the User ID of the file that was initlized and stored
*in a private attribute. This getter allows you to retrieve the private member outside of the class. This User ID member
*helps us retreive the name of the owner of the file.
*Return: This getter returns a uid_t type and contains the user id number.
**/
uid_t FileManager::getUserID()
{
return this->user_id;
}
/*Function name: getUserName()
*Parameters: No parameters.
*Description: This function is a getter function that retreives the User Name or Owner name of the file that was initlized and stored
*in a private attribute. This getter allows you to retrieve the private member outside of the class. This user name member
*will be used in some of the utlilites created to provide extra information to the user about the file.
*Return: This getter returns a string type of the name of the owner.
**/
string FileManager::getUserName()
{
return this->user_name;
}
/*Function name: getGroupID()
*Parameters: No parameters.
*Description: This function is a getter function that retreives the Group ID of the file that was initlized and stored
*in a private attribute. This getter allows you to retrieve the private member outside of the class. This group ID member
*helps us retreive the name of the group of the file.
*Return: This getter returns a gid_t type and contains the user id number.
**/
gid_t FileManager::getGroupID()
{
return this->group_id;
}
/*Function name: getGroupName()
*Parameters : No parameters.
*Description : This function is a getter function that retreives the Group Name of the file that was initlized and stored
*in a private attribute.This getter allows you to retrieve the private member outside of the class. This group name member
*will be used in some of the utlilites created to provide extra information to the user about the file.
*Return : This getter returns a string type of the name of the group name.
**/
string FileManager::getGroupName()
{
return this->group_name;
}
/*Function name: getPermissions()
*Parameters : No parameters.
*Description : This function is a getter function that retreives the permissions of the file that was initlized and stored
*in a private attribute. This getter allows you to retrieve the private member outside of the class. This permissions attribute
*will be used to get an easily readable string version so that a user can easily see what actions can be performed on the file
*and by who.
*Return : This getter returns a mode_t type of the file permissions.
**/
mode_t FileManager::getPermissions()
{
return this->permissons;
}
/*Function name: getAccessTime()
*Parameters : No parameters.
*Description : This function is a getter function that retreives the time that the file was last accessed. This
*is used to display to a user to provide extra information about the file.
*Return : This getter returns a string type of the file access time.
**/
string FileManager::getAccessTime()
{
return this->access;
}
/*Function name: getModification()
*Parameters : No parameters.
*Description : This function is a getter function that retreives the time that the file was last modified. This
*is used to display to a user to provide extra information about the file.
*Return : This getter returns a string type of the file modification time.
**/
string FileManager::getModification()
{
return this->modifcation;
}
/*Function name: getStatusChange()
*Parameters : No parameters.
*Description : This function is a getter function that retreives the time that the file's status was changed. This
*is used to display to a user to provide extra information about the file.
*Return : This getter returns a string type of the file status change time.
**/
string FileManager::getStatusChange()
{
return this->status_change;
}
/*Function name: getBlockSize()
*Parameters : No parameters.
*Description : This function is a getter function that retreives the block size of the file. This
*attribute is used for reading and writing files in blocks to make the program more efficient.
*Return : This getter returns a blksize_t type that is the Block size of the file in bytes.
**/
blksize_t FileManager::getBlockSize()
{
return this->block_size;
}
/*Function name: getChildren()
*Parameters : No parameters.
*Description : This function is a getter function that gives a vector of a
*directory file in the system. This attribute is used quite abit in the program
*so that we can get access to the information about the child files in a file.
*Return : This getter returns a vector of FileManager objects.
**/
vector<FileManager> FileManager::getChildren()
{
return this->children;
}
/*Function name: getErrorNum()
*Parameters : No parameters.
*Description : This function is a getter function that retreives the error number
*associated with the FileManager object. This error number will help to see where
*something went wrong during an operation.
*Return : This getter returns an integer type which is the error number.
**/
int FileManager::getErrorNum()
{
return this->error_num;
}
/*Function name: getErrorNumStr()
*Parameters : No parameters.
*Description : This function is a getter function that retrieves
*the same error number as above however this time in a string.
*Return : This getter returns a string of the error number.
**/
string FileManager::getErrorNumStr()
{
string error = strerror(this->error_num);
return error;
}
/*Function name: setName
*Parameters : This function is a setter function that takes one char* parameter that
*the function uses to rename a file.
*Description : This function takes the new name of the file as a an argument and calls another
*function called rename which actually renames the file in the system.
*Return : No return type
**/
void FileManager::setName(char *name)
{
rename_(name);
}
/*Function name: getPermissionsString
*Parameters : This function takes one mode_t parameter that
*the function uses to understand which permissions are for which user.
*Description : This function provides the permissions in an
*easily readable fashion for a user to read. The function uses bitwise
*operations to understand what permissions are set for the file.
*The permissions include for the Owner, Group, and Others.
*Return : This function returns a string.
**/
string FileManager::getPermissionsString(mode_t permission)
{
string file_permissions;
if (permission & S_IRUSR)
file_permissions += 'r';
else
file_permissions += '-';
if (permission & S_IWUSR)
file_permissions += 'w';
else
file_permissions += '-';
if (permission & S_IXUSR)
file_permissions += 'x';
else
file_permissions += '-';
if (permission & S_IRGRP)
file_permissions += 'r';
else
file_permissions += '-';
if (permission & S_IWGRP)
file_permissions += 'w';
else
file_permissions += '-';
if (permission & S_IXGRP)
file_permissions += 'x';
else
file_permissions += '-';
if (permission & S_IROTH)
file_permissions += 'r';
else
file_permissions += '-';
if (permission & S_IWOTH)
file_permissions += 'w';
else
file_permissions += '-';
if (permission & S_IXOTH)
file_permissions += 'x';
else
file_permissions += '-';
return file_permissions;
}
/*Function name: getFileType
*Parameters : This function takes in a mode_t parameter which represents the type.
*This type was retreived in the constructor and is used in coordination with some
*sys functions to establish what type of file we are working with.
*Description : This function is used alot in the program when checking to see what
*type of file it is since certain utilites have different operations depending on what
*type of file it is.
*Return : This function returns a string clearly indicating what type of file it is.
**/
string FileManager::getFileType(mode_t type)
{
if (S_ISREG(type))
{
return "regular file";
}
else if (S_ISDIR(type))
{
return "directory";
}
else if (S_ISCHR(type))
{
return "character device";
}
else if (S_ISBLK(type))
{
return "block device";
}
else if (S_ISFIFO(type))
{
return "FIFO";
}
else if (S_ISLNK(type))
{
return "Symbolic Link";
}
else if (S_ISSOCK(type))
{
return "Socket";
}
}
/*Function name: dump
*Parameters : This function takes in an output stream as a parameter which will either be
*a ofstream or cout in our program.
*Description : This function is used to dump the contents of one file into another. The function uses
*dynamically allocated arrays as buffers to allow for reading and writing in specified blocks. It reads
*the objects file contents and writes the contents into the output stream whether it being another file or cout.
*Return : This function returns an integer which is the error number. The function will return 0 if the operation
*is successful or else its corresponding error number if it encounters an issue.
**/
int FileManager::dump(ostream& stream)
{
if ((stream.bad())){
this->error_num = EIO;
return this->error_num;
}
if (this->getFileType(this->getType()) != "regular file")
{
this->error_num = ENOTSUP;
cout << "Error! This is not a regular file!" << endl;
return this->error_num;
}
ifstream file;
file.open(this->file_name);
if (file.fail())
{
cout << "Error Opening File!" << endl;
return this->error_num = EIO;
}
char *buffer = new char[this->block_size];
double iterations = ceil(double(this->size) / double(this->block_size)); //dividing the size of the file with the block size and taking the ceiling of it will let us know how many block sizes we are reading and writing
int i = 0;
while (i < iterations)
{
file.read(buffer, this->block_size);
if (file.bad())
{
this->error_num = EIO;
}
if (i == iterations - 1) //on the last iteration we do not want to write everthing that is left in the buffer if we have already written it so we use the modulus operator to get the remainder
{
stream.write(buffer, ((this->size) % (this->block_size))); //this writes the remaining contents to the ostream while leaving out content that has already been written
i++;
continue;
}
stream.write(buffer, (this->block_size));
i++;
}
file.close();
delete[] buffer; //delete dynamically allocated buffer
this->error_num = 0;
return this->error_num;
}
/*Function name: rename_
*Parameters : This function takes in a char* which is the desired new name of the file.
*Description : This function physically renames the file by using a system function. The
*sys function rename takes in the existing name and the desired name and renames the file
*in the system.
*Return : This function returns an integer value which is the error number.
**/
int FileManager::rename_(char* name)
{
if (rename(this->file_name, name) == -1) //error checking since rename returns -1 if an issue occurs in the operations
{
this->error_num = errno;
return this->error_num;
}
this->file_name = name;
this->error_num = 0;
return this->error_num;
}
/*Function name: remove
*Parameters : No parameters
*Description : This function is used to remove a file from the system. First all the
*retrieved information in the attributes are cleared and then using the sys function
*unlink(), the file is automatically removed from the system. Unlink takes in the file name.
*Return : This function returns an integer value which is the error number.
**/
int FileManager::remove()
{
this->type = NULL;
this->size = NULL;
this->user_id = NULL;
this->user_name.clear();
this->group_id = NULL;
this->group_name.clear();
this->permissons = NULL;
this->access.clear();
this->modifcation.clear();
this->status_change.clear();
this->block_size = NULL;
children.clear();
this->error_num = NULL;
if (unlink(this->file_name) == -1)
{
this->error_num = errno;
return this->error_num;
}
this->error_num = 0;
return this->error_num;
}
/*Function name: compare
*Parameters : This function takes a fileManager object that holds the file that we want to compare with.
*Description : This function is used to compare the contents of a file with another file. It does so by reading
*the content in blocks using dynamically allocated buffers, and each iteration, the blocks are compared using a
*built in string function.
*Return : This function returns a string that indicates if the files are the same or different.
**/
string FileManager::compare(FileManager obj)
{
if (this->getFileType(this->getType()) != "regular file")
{
this->error_num = ENOTSUP;
cout << "Error! This is not a regular file!" << endl;
return this->getErrorNumStr();
}
if (obj.getFileType(obj.getType()) != "regular file")
{
this->error_num = ENOTSUP;
cout << "Error! This is not a regular file!" << endl;
return this->getErrorNumStr();
}
char *buffer_one = new char[this->block_size];
char *buffer_two = new char[obj.getBlockSize()];
string content_one;
string content_two;
if (this->getSize() != obj.getSize())
{
this->error_num = 0;
return "different!";
}
double iterations = ceil(double(this->size) / double(this->block_size));
int i = 0;
ifstream file_one;
ifstream file_two;
file_one.open(this->file_name);
if (file_one.fail())
{
cout << "Error Opening File!" << endl;
this->error_num = EIO;
delete[] buffer_one;
delete[] buffer_two;
return this->getErrorNumStr();
}
file_two.open(obj.getName());
if (file_two.fail())
{
cout << "Error Opening File!" << endl;
this->error_num = EIO;
delete[] buffer_one;
delete[] buffer_two;
return this->getErrorNumStr();
}
while (i < iterations)
{
file_one.read(buffer_one, this->block_size); //reads block of content from file one
if (file_one.bad())
{
this->error_num = EIO;
}
file_two.read(buffer_two, this->block_size); //reads block of content from file two
if (file_two.bad())
{
this->error_num = EIO;
}
content_one = buffer_one;
content_two = buffer_two;
if (content_one.compare(content_two) != 0)
{
delete[] buffer_one;
delete[] buffer_two;
file_one.close();
file_two.close();
this->error_num = 0;
return "different!";
}
i++;
}
delete[] buffer_one;
delete[] buffer_two;
file_one.close();
file_two.close();
this->error_num = 0;
return "the same!";
}
/*Function name: expand
*Parameters : No parameters
*Description : This function is used to populate the children vector with the contents
*in a directory. This funtion only works on directories and will create FileManager objects
*for each file in the directory.
*Return : This function returns a string that specifies if the files were added to the vector,
*or will return the error number if the operation fails.
**/
string FileManager::expand()
{
if (this->getFileType(this->getType()) != "directory")
{
this->error_num = ENOTSUP;
return this->getErrorNumStr();
}
DIR *dir_stream = opendir(this->file_name);
if (dir_stream == NULL)
{
this->error_num = errno;
return this->getErrorNumStr();
}
struct dirent *next = readdir(dir_stream);
errno = 0;
while (next != NULL)
{
if ((strcmp(next->d_name, ".") == 0) || (strcmp(next->d_name, "..") == 0))
{
next = readdir(dir_stream);
continue;
}
string path = get_current_dir_name();
string name = next->d_name;
string new_path = path + "/" + name; //this concatonates the paths and file to create a new path
char *new_name = new char[new_path.length() + 1]; //turns the string into a char * since that is what my FileManager constructor execpts as an argument
strcpy(new_name, new_path.c_str());
FileManager file(new_name);
this->children.push_back(file);
next = readdir(dir_stream);
}
if (errno != 0)
{
this->error_num = errno;
return this->getErrorNumStr();
}
if (closedir(dir_stream) == -1)
{
this->error_num = errno;
return this->getErrorNumStr();
}
this->error_num = 0;
return "Added Children";
}