X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=module%2Fp2pkp_file.c;h=213b8de7f969d6ed5114e8dccdb6d6d07daaa215;hb=71246fb0c8dead16cc33caf83020961c33f8efda;hp=48dc67ccc9c3963976a2875650981801cb3a78da;hpb=2d6f09bf6e183a2a889d52af3c0acc48c5998043;p=p2p-kernel-protocol.git diff --git a/module/p2pkp_file.c b/module/p2pkp_file.c index 48dc67c..213b8de 100644 --- a/module/p2pkp_file.c +++ b/module/p2pkp_file.c @@ -1,5 +1,6 @@ #include #include +#include #include "p2pkp_file.h" #include "p2pkp_debug.h" @@ -11,18 +12,58 @@ struct file* p2pkp_open_file_read(const char *filename) return IS_ERR_OR_NULL(file) ? NULL : file; } +struct file* p2pkp_open_file_write(const char *filename, mode_t mode) +{ + struct file * file; + file = filp_open(filename, O_CREAT|O_TRUNC, mode); + return IS_ERR_OR_NULL(file) ? NULL : file; +} + void p2pkp_close_file(struct file *file) { + file_fsync(file, 0); filp_close(file,NULL); } +int p2pkp_write_in_file(struct file *file, char *buffer, int len) +{ + // Create variables + int bytes_written, ret; + mm_segment_t fs; + + if(file == NULL) { + ERROR("filp_open error!!"); + return -EINVAL; + } + DEBUG("Changinig segment descriptor"); + // Get current segment descriptor + fs = get_fs(); + // Set segment descriptor associated to kernel space + set_fs(get_ds()); + DEBUG("Changed segment descriptor - buffer is %p", buffer); + + bytes_written = 0; + do { + ret = file->f_op->write(file, buffer + bytes_written, len, &file->f_pos); + if (ret < 0) { + ERROR("error while reading from file"); + break; + } + len -= ret; + bytes_written += ret; + } while (len > 0); + set_fs(fs); + return ret < 0 ? ret : bytes_written; +} + + + int p2pkp_read_from_file(struct file *file, char *buffer, int len) { // Create variables int read_bytes, offset; mm_segment_t fs; - // To see in /var/log/messages that the module is operating if(file == NULL) { ERROR("filp_open error!!"); return -EINVAL;