-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuseftp.cpp
179 lines (136 loc) · 3.18 KB
/
fuseftp.cpp
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
#include "fuseftp.hpp"
#define FUSE_USE_VERSION 26
#define _FILE_OFFSET_BITS 64
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#ifndef WIN32
#include <sys/time.h>
#endif
#include <stdlib.h>
#include<bits/stdc++.h>
#include <curl/curl.h>
#include "ftpclient.hpp"
using namespace std;
URL_FILE * handle;
FTPClient Fc;
class options {
public:
const char *urlname;
const char *contents;
} options;
bool isDirectory(string url) //Check if URL is a Directory or not
{
if(url[url.length()-1]=='/')
return true;
return false;
}
bool isFile(string url) //Check if URL is a file or not
{
if(url[url.length()-1]=='*')
return true;
return false;
}
int FuseFTP::fuse_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
char * last_char = strrchr((char *)path,'*');
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0 || last_char!=NULL) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
} else {
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = strlen(options.contents);
}
return res;
}
int FuseFTP::fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
string url = options.urlname;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
if(isDirectory(url))
{
filler(buf, options.contents, NULL, 0);
}
else
{
string err="Error:The URL is a file not a directory";
filler(buf, err.c_str() ,NULL,0);
}
return 0;
}
int FuseFTP::fuse_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
size_t len;
(void) fi;
string url = options.urlname;
if(isFile(url)){
len = strlen(options.contents);
if (offset < len) {
if (offset + size > len)
size = len - offset;
memcpy(buf, options.contents + offset, size);
} else
size = 0;
}
else
{
string err="Error:The URL is a directory not a file";
strcpy(buf,err.c_str());
size = err.length();
}
return size;
}
FuseFTP fs;
static struct fuse_operations fuse_oper = {
.getattr = fs.fuse_getattr,
.read = fs.fuse_read,
.readdir = fs.fuse_readdir,
};
int CreateFTPConnection(const char* path)
{
handle = url_fopen(path, "r");
if(!handle) {
cout<<"Couldn't url_fopen()\n";
return 2;
}
return 1;
}
void CloseFTPConnection()
{
Fc.url_fclose(handle);
}
int main(int argc, char *argv[])
{
string url;
url = argv[1];
options.urlname = argv[1];
url = url.substr(0,url.length()-2); //Delete last 2 charachters of URL
const char* path = url.c_str();
int conn=CreateFTPConnection(path);
if(conn!=1)
{
return conn;
}
//Read Contents of URL
char buffer[256];
string cont;
while(!Fc.url_feof(handle)) {
Fc.url_fgets(buffer, sizeof(buffer), handle);
cont += buffer;
}
options.contents = cont.c_str() ;
CloseFTPConnection();
char *argv1[] ={argv[0],argv[2]};
argc =2;
return fuse_main(argc, argv1, &fuse_oper, NULL);
}