return -ENODEV;
msg = hello_message[iminor(ino)];
filp->private_data = msg;
+ if (filp->f_flags & O_TRUNC) {
+ memset(msg->text, 0, msg->len);
+ msg->len = 0;
+ printk(KERN_INFO "File opened with truncate flags\n");
+ }
+ if (filp->f_flags & O_APPEND) {
+ filp->f_pos = msg->len;
+ }
return 0;
}
int r;
struct message *msg = filp->private_data;
/* do not read pass through the size of the message */
- if (*pos >= msg->len)
+ if (*pos >= BUFFER_SIZE)
/* return end of file */
return 0;
/* if len is bigger than the rest of the message, clamp it */
- if (len > msg->len - *pos)
- len = msg->len - *pos;
+ if (len > BUFFER_SIZE - *pos)
+ len = BUFFER_SIZE - *pos;
+
/* copy message to user space and return error if it fails */
r = copy_from_user(msg->text + *pos, buf, len);
- if (r)
+ if (r) {
+ if (*pos + len > msg->len)
+ memset(msg->text + msg->len, 0, *pos + len - msg->len);
return -EFAULT;
+ }
/* update the file position */
*pos += len;
+ if (msg->len < *pos) {
+ printk(KERN_DEBUG "Updating size from %d to %lld\n", msg->len, *pos);
+ msg->len = *pos;
+ }
+
return len;
}