-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchFile.c
executable file
·82 lines (72 loc) · 1.54 KB
/
searchFile.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
#include "header.h"
BOOL searchFile( char* fileName[] , int filecount , int key)
{
BOOL ret;
FILEINFO obj;
relativePath = NULL;
for(int i = 0 ; i < filecount ; i++)
{
printf("%s\n",fileName[i]);
}
inputFd = open(fileName[0], O_RDONLY );
if(inputFd == -1)
{
printf("Error: File Not Found\n");
return ERR_ACCESS;
}
//Open existing file to read
ret = checkValidation(fileName[0] , key );
if(ret != SUCCESS ) return ret;
relativePath = dynamicConcat(relativePath,fileName[1]);
if(checkDirectoryExist( relativePath ) == FALSE)
{
if( mkdir(fileName[1] , 0777) == -1)
{
printf("Error: will creating directory\n");
return ERR_CREATE;
}
}
//open pack file
while( (read( inputFd , &obj , sizeof(obj) ) ) != 0 )
{
if( obj.size == DIRECTORY_OPEN )
{
continue;
}
else if( obj.size == DIRECTORY_CLOSE )
{
continue;
}
else
{
int i;
BOOL flag = FALSE;;
for( i = 2 ; i < filecount ; i++ )
{
printf("%s %s\n",fileName[i] , obj.name);
if ( strcmp( fileName[i] , obj.name ) == 0)
{
relativePath = dynamicConcat(relativePath,"/");
relativePath = dynamicConcat(relativePath,obj.name);
ret = writeFile( relativePath , obj.size );
if( ret != SUCCESS )
return ret;
relativePath = dynamicRemoveLast(relativePath , '/');
flag = TRUE;
break;
}
}
if ( flag == FALSE)
{
lseek(inputFd , obj.size , SEEK_CUR);
}
continue;
}
}
if( close(inputFd) == -1 )
{
printf("Error: Will Closing the output file \n");
return FALSE;
}
return TRUE;
}