-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateoptionfns.c
More file actions
205 lines (177 loc) · 5.94 KB
/
createoptionfns.c
File metadata and controls
205 lines (177 loc) · 5.94 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
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include "createoptionfns.h"
#include "bitfilecreation.h"
void copyingfiles(char *source_file,char *filename){
char ch;
FILE *source;
FILE *target;
target = fopen (filename, "w+");
//printf("------------%s---%s\n",filename,source_file);
source = fopen(source_file, "r");
while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
}
char* getFileNameFromPath(char* path){
if(path == NULL){
return NULL;
}
char* filename = NULL;
int i;
for(i = 0; i<strlen(path);i++){
if(path[i]=='/'){
int j;
int temp = 0;
for(j = i+1; j<strlen(path);j++){
if(path[j]=='/'){
temp = 1;
break;
}
}
if(temp == 0){
int c = 0;
filename = (char *)malloc(sizeof(char)*(strlen(path)-i));
for(j = i+1; j<strlen(path); j++){
filename[c] = path[j];
//printf("%c",path[j]);
c+=1;
}
return filename;
}
}
}
if(filename == NULL){
printf("INVALID");
return NULL;
}
return NULL;
}
void listOFDirectoryFiles(char *path){
DIR* d;
struct dirent *dir;
//DESKTOP - /home/prerna2002/Desktop
d = opendir(path);
char *directory = getFileNameFromPath(path);
//printf("%s\n",directory);
char *tarfilename;
char *compressed_tarfile;
compressed_tarfile = (char *)malloc(sizeof(char)*(strlen(directory)+6));
tarfilename = (char *)malloc(sizeof(char)*(strlen(directory)+4));
memcpy(tarfilename,directory,strlen(directory));
memcpy(compressed_tarfile,directory,strlen(directory));
int i = strlen(directory);
tarfilename[i] = '.';
tarfilename[i+1] = 't';
tarfilename[i+2] = 'a';
tarfilename[i+3] = 'r';
compressed_tarfile[i] = '_';
compressed_tarfile[i+1] = 'c';
compressed_tarfile[i+2] = '.';
compressed_tarfile[i+3] = 't';
compressed_tarfile[i+4] = 'a';
compressed_tarfile[i+5] = 'r';
printf("%s\n",compressed_tarfile);
//CREATING NEW TAR FILE
FILE *fp;
fp = fopen (tarfilename, "w+");
FILE *fpc;
fpc = fopen(compressed_tarfile,"w+");
//printf("Directory - %s\n",dirname);
//printf("\n");
if(d){
while((dir=readdir(d)) != NULL){
//printf("%s\n",dir->d_name);
if(dir->d_name[0]!='.'){
char *currentfile = (char *)malloc(sizeof(char)*(strlen(path)+strlen(dir->d_name)+1));
memcpy(currentfile, path,strlen(path));
currentfile[strlen(path)] = '/';
int i;
int c = strlen(path)+1;
for(i = 0; i<strlen(dir->d_name);i++){
currentfile[c] = dir->d_name[i];
c+=1;
}
//printf("%s\n",currentfile);
copyingfiles(currentfile,dir->d_name);
char *bitfilename;
bitfilename = (char *)malloc(sizeof(char)*20);
for(i=0; i<strlen(dir->d_name);i++){
if(dir->d_name[i] != '.')
bitfilename[i] = dir->d_name[i];
else
break;
}
//printf("%s\n",bitfilename);
bitfilecreation(bitfilename,dir->d_name);
addToTar(fpc,bitfilename);
addToTar(fp,dir->d_name);
remove(dir->d_name);
remove(bitfilename);
free(bitfilename);
free(currentfile);
}
}
closedir(d);
}
if(d == NULL){
printf("INVALID INPUT");
fclose(fpc);
fclose(fp);
return;
}
fclose(fpc);
fclose(fp);
return;
}
void addToTar(FILE *tarFile, char *file){
FILE *new = fopen(file, "rb"); //open the new file for reading
if(new == NULL){
printf("No such file in directory");
return;
}
fseek(tarFile, 0L, SEEK_END); //size of tar file
long int lengthoftarfile = ftell(tarFile);
int startHeader = ftell(tarFile);
/*copying content of new file at the end of tar file*/
int contentstart = lengthoftarfile + 512;
fseek(tarFile, contentstart, SEEK_SET); //setting pointer at end of file
copyFile(new, tarFile, file);
lengthoftarfile = ftell(tarFile);
int contentend = lengthoftarfile - contentstart; //Length of newly inserted content
/*Adding header before the newly added content in tne 512 bytes space*/
struct tarHeader head;
/*memset - used to fill a block of memory with a particular value. eg. void *memset(void *ptr, int x, size_t n);*/
memset(&head, 0 , sizeof( struct tarHeader));
getHeaderStats(&head, file, contentend);
fseek(tarFile, startHeader, SEEK_SET);
fwrite(&head, 1, sizeof(struct tarHeader), tarFile);
fclose(new);
}
void getHeaderStats (struct tarHeader *head, char *file, int contentend) {
struct stat stats;
stat(file, &stats);
snprintf( head->name, 100, "%s", file );
snprintf( head->size, 12, "%d", contentend );
snprintf( head->mtime, 12, "%ld ", stats.st_mtime );
snprintf( head->mode, 8, "%o ", stats.st_mode );
}
void copyFile(FILE *new, FILE *tarFile, char *file){
/*
STAT functions return information about a file, in the buffer pointed to by statbuf.
*/
struct stat stats;
stat(file, &stats);
char *buffer = malloc(sizeof(char));
while( !feof(new) ){
buffer = (char*)malloc(stats.st_size * sizeof(char));
size_t read = fread( buffer, 1, stats.st_size, new );
fwrite( buffer, 1, read, tarFile);
}
free(buffer);
}