Здравствуйте, необходимо вывести содержание бинарного файла mv
len = filesize("/bin/mv");
FILE * f;
f = fopen("/bin/mv", "r");
char* ptr = buf;
while((fread(ptr, 1, 1, f)) > 0)
{
ptr++;
}
fclose(f);
return len;
Вот весь код программы
#define FUSE_USE_VERSION 26
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
static const char *bar_path ="/bar";//papka
static const char *foo_path ="/foo";//papka
static const char *bin_path ="/bin";//papka
static const char *baz_path ="/baz";//papka
static const char *example_path ="/bar/example";//file
static const char *example_str = "Hello World\n";
static const char *readmetxt_path ="/bar/readme.txt";//file
static const char *readmetxt_str = "status name surname number \n";
static const char *testtxt_path ="/foo/test.txt";
static const char *testtxt_str = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n";//file
static const char *mv_path ="/foo/mv";//file
static char* tempFile = "";
static char* tempContent = "";
size_t filesize(const char *filename){
struct stat st;
size_t retval=0;
if(stat(filename,&st) ){
printf("cannot stat %s\n",filename);
}else{
retval=st.st_size;
}
return retval;
}
static int lab5_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0717;//755
stbuf->st_nlink = 2;
} else if (strcmp(path, bin_path) == 0) {//papka
stbuf->st_mode = S_IFDIR | 0511;
stbuf->st_nlink = 2;
} else if (strcmp(path, foo_path) == 0) {//papka
stbuf->st_mode = S_IFDIR | 0233;
stbuf->st_nlink = 2;
}else if (strcmp(path, bar_path) == 0) {//papka
stbuf->st_mode = S_IFDIR | 0511;
stbuf->st_nlink = 2;
} else if (strcmp(path, baz_path) == 0) {//papka
stbuf->st_mode = S_IFDIR | 0777;
stbuf->st_nlink = 2;
} else if (strcmp(path, "/bin") == 0) {//v papke papka а в папке папка а в папке еще папка)
stbuf->st_mode = S_IFDIR | 0007;//144
stbuf->st_nlink = 2;
} else if (strcmp(path, "/baz") == 0) {//v papke papka
stbuf->st_mode = S_IFDIR | 0577;
stbuf->st_nlink = 2;
} else if (strcmp(path, "/foo") == 0) {//v papke papka
stbuf->st_mode = S_IFDIR | 0007;//144
stbuf->st_nlink = 2;
} else if (strcmp(path, "/bar") == 0) {//v papke papka
stbuf->st_mode = S_IFDIR | 0007;//144
stbuf->st_nlink = 2;
}
else if (strcmp(path, example_path) == 0) {
stbuf->st_mode = S_IFREG | 0222;// file in foo/baz
stbuf->st_nlink = 1;
stbuf->st_size = strlen(example_str);
} else if (strcmp(path, readmetxt_path) == 0) {
stbuf->st_mode = S_IFREG | 0444;//644
stbuf->st_nlink = 1;
stbuf->st_size = strlen(readmetxt_str);
} else if (strcmp(path, testtxt_path) == 0) {
stbuf->st_mode = S_IFREG | 0707;//0707
stbuf->st_nlink = 1;
stbuf->st_size = strlen(testtxt_str);
} else if (strcmp(path, mv_path) == 0) {
stbuf->st_mode = S_IFREG | 0777;//head
stbuf->st_nlink = 1;
//stbuf->st_size = strlen(mv_path);
size_t size = filesize("bin/mv");
stbuf->st_size = size;
} else
res = -ENOENT;
return res;
}
static int lab5_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
if (strcmp(path, "/") == 0) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, bin_path + 1, NULL, 0);
filler(buf, bar_path + 1, NULL, 0);
filler(buf, baz_path + 1, NULL, 0);
filler(buf, foo_path + 1, NULL, 0);
}else if (strcmp(path, bar_path) == 0) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, "/readme.txt" + 1, NULL, 0);
filler(buf, "/example" + 1, NULL, 0);
}else if (strcmp(path, foo_path) == 0) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, "/mv" + 1, NULL, 0);
filler(buf, "/test.txt" + 1, NULL, 0);
}
else
return -ENOENT;
return 0;
}
static int lab5_open(const char *path, struct fuse_file_info *fi)
{
if ((strcmp(path, example_path) != 0)&&(strcmp(path, readmetxt_path) != 0)&&(strcmp(path, testtxt_path) != 0)&&(strcmp(path, mv_path) != 0))
return -ENOENT;
if ((fi->flags & 3) != O_RDONLY)
return -EACCES;
return 0;
}
static int lab5_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
size_t len;
(void) fi;
if(strcmp(path, example_path) == 0)
{
return -EACCES;
}
else if(strcmp(path, readmetxt_path) == 0)
{
len = strlen(readmetxt_str);
if (offset < len) {
if (offset + size > len)
size = len - offset;
memcpy(buf, readmetxt_str + offset, size);
} else
size = 0;
return size;
}
else if(strcmp(path, testtxt_path) == 0)
{
len = strlen(testtxt_str);
if (offset < len) {
if (offset + size > len)
size = len - offset;
memcpy(buf, testtxt_str + offset, size);
} else
size = 0;
return size;
}
else if(strcmp(path, mv_path) == 0)
{
len = filesize("/bin/mv");
FILE * f;
f = fopen("/bin/mv", "r");
char* ptr = buf;
while((fread(ptr, 1, 1, f)) > 0)
{
ptr++;
}
fclose(f);
return len;
}
else
return -ENOENT;
}
char* memcpu(char* source, char* buf)
{
int size = strlen(source) + strlen(buf) + 1;
char* result = (char*)malloc(sizeof(char)*size);
result[size - 1] = 0; // ставим отделяющий символ
int i = 0;
for(; i < strlen(source); i++) // переносим существующее значение
{
result[i] = source[i];
}
for(; i < size; i++) // добавляем значение буфера
{
result[i] = buf[i - strlen(source)];
}
return result; // возвращаем указатель на область памяти . содежращую старые данные и данные , переданные в буфере.
}
static int d_write(const char *path, const char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
tempFile = path;
tempContent = memcpu(tempContent,buf + offset);
return strlen(buf+offset);
}
static struct fuse_operations hello_oper = {
.getattr = lab5_getattr,
.readdir = lab5_readdir,
.open = lab5_open,
.write = d_write, //пишем данные в открытый файл.
.read = lab5_read,
};
int main(int argc, char *argv[])
{
return fuse_main(argc, argv, &hello_oper, NULL);
}