-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilePacker.c
executable file
·166 lines (139 loc) · 3.44 KB
/
filePacker.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
158
159
160
161
162
163
164
165
166
#include "header.h"
void InsertValidationCode( char *fileName , int key)
{
char *actualName;
strcpy ( authentication.hardcoded , "MARVALLOUS" );
if( key == 0 )
authentication.type = APPLICATION_DEF;
else
authentication.type = USER_DEF;
actualName = fileName;
for (int i = strlen(fileName); i >= 0; i--)
{
if( fileName[i] == '/' )
{
actualName = &fileName[i + 1] ;
break;
}
}
authentication.key = strlen(actualName) + key;
write( outputFd , &authentication , sizeof(authentication) );
}
void writeFileInfo( char* dirName , int size )
{
FILEINFO obj;
ssize_t count;
strcpy( obj.name , dirName );
obj.size = size;
count = write( outputFd , &obj , sizeof(obj) );
}
BOOL readFile()
{
ssize_t retread;
int temp;
char buffer[256]; //to store read data
//Open a file to read
inputFd = open( relativePath, O_RDONLY);
if( inputFd == -1 )
return ERR_CREATE;
while( (retread = read( inputFd , buffer , sizeof(buffer) ) ) != 0 )
{
temp = retread;
while(temp != 0)
{
buffer[temp] += authentication.key;
temp--;
}
write( outputFd , buffer , retread );
}
close(inputFd);
return SUCCESS;
}
BOOL readDirectory( char* dirName)
{
ssize_t count;
DIR* dir;
struct dirent *dirEntry; //output of readdir store complete info about file
struct stat status; // file status
BOOL ret;
//to open directory
dir = opendir(relativePath);
if( dir == 0)
{
printf("Error: Cannot open directory ( Give proper name of directory) \n");
return ERR_ACCESS;
}
//check if directory is open or not
if ( (dirEntry = readdir(dir) ) == NULL )
{
printf("%s Directory is Empty\n",relativePath);
return FALSE;
}
//write Directory info in file ( to indicate directory is open)
writeFileInfo( dirName , DIRECTORY_OPEN );
do
{
// skip '.' and '..' directory
if( strcmp(dirEntry->d_name , ".") == 0 || strcmp(dirEntry->d_name , "..") == 0 )
continue;
//concate file name to relativePath
relativePath = dynamicConcat( relativePath , "/" );
relativePath = dynamicConcat( relativePath , dirEntry->d_name );
//get status of file;
if(lstat(relativePath,&status) == -1 )
{
return FALSE;
}
//checking if file is regular or not
if(S_ISREG( status.st_mode))
{
writeFileInfo( dirEntry->d_name , status.st_size );
ret = readFile();
if( ret != SUCCESS )
{
return ret;
}
}
else if( S_ISDIR( status.st_mode) )
{
ret = readDirectory( dirEntry -> d_name);
if( ret != SUCCESS )
{
return ret;
}
}
//remove file name form path
relativePath = dynamicRemoveLast(relativePath , '/');
}while ( (dirEntry = readdir(dir) ) != NULL );
//write Directory info in file ( to indicate directory is close)
writeFileInfo( dirName , DIRECTORY_CLOSE );
return SUCCESS;
}
BOOL filePack( char* path , char* fileName ,int key )
{
BOOL ret;
char dirName[255];
//Open new or existing file to write
outputFd = creat( fileName , 0444 );
if(outputFd == -1)
return ERR_CREATE;
//VALIDATION CODE
InsertValidationCode( fileName , key );
// To get main Folder name eg( ExcuteData/Folder get "Folder" )
relativePath = ( char * ) malloc ( strlen(path) + 1);
strcpy( relativePath , path);
strcpy ( dirName ,relativePath);
for (int i = strlen(relativePath); i >= 0; i--)
{
if( relativePath[i] == '/' )
{
strcpy( dirName , &relativePath[i + 1]) ;
break;
}
}
//read given directory data
ret = readDirectory( dirName );
if(ret != SUCCESS) return ret;
close(outputFd);
return SUCCESS;
}