summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/libc/include/stdio.h2
-rw-r--r--lib/libc/src/stdio/ftell.c37
-rw-r--r--sys/fs/tmpfs.c42
-rw-r--r--sys/include/fs/tmpfs.h4
-rw-r--r--sys/include/sys/vnode.h3
-rw-r--r--sys/kern/kern_descrip.c2
6 files changed, 81 insertions, 9 deletions
diff --git a/lib/libc/include/stdio.h b/lib/libc/include/stdio.h
index 7d8279d..d721e4e 100644
--- a/lib/libc/include/stdio.h
+++ b/lib/libc/include/stdio.h
@@ -74,6 +74,8 @@ __BEGIN_DECLS
size_t fread(void *__restrict ptr, size_t size, size_t n, FILE *__restrict stream);
size_t fwrite(const void *__restrict ptr, size_t size, size_t n, FILE *__restrict stream);
+
+long ftell(FILE *stream);
char *fgets(char *__restrict s, int size, FILE *__restrict stream);
FILE *fopen(const char *__restrict path, const char *__restrict mode);
diff --git a/lib/libc/src/stdio/ftell.c b/lib/libc/src/stdio/ftell.c
new file mode 100644
index 0000000..aa0f608
--- /dev/null
+++ b/lib/libc/src/stdio/ftell.c
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+
+long
+ftell(FILE *stream)
+{
+ return lseek(stream->fd, 0, SEEK_CUR);
+}
diff --git a/sys/fs/tmpfs.c b/sys/fs/tmpfs.c
index 9dce89a..a6e40e1 100644
--- a/sys/fs/tmpfs.c
+++ b/sys/fs/tmpfs.c
@@ -211,6 +211,8 @@ tmpfs_create(struct vop_create_args *args)
root_np = TAILQ_FIRST(&root);
np->dirvp = dirvp;
np->type = TMPFS_REG;
+ np->real_size = 0;
+ np->mode = 0700;
memcpy(np->rpath, pcp, strlen(pcp) + 1);
TAILQ_INSERT_TAIL(&root_np->dirents, np, link);
@@ -269,6 +271,14 @@ tmpfs_write(struct vnode *vp, struct sio_txn *sio)
}
/*
+ * Bring up the real size if we are writing
+ * more bytes.
+ */
+ if (sio->offset >= np->real_size) {
+ np->real_size = sio->offset;
+ }
+
+ /*
* If the length to be written exceeds the residual byte
* count. We will try to expand the buffer by the page
* size. However, if this fails, we will split the write
@@ -287,7 +297,6 @@ tmpfs_write(struct vnode *vp, struct sio_txn *sio)
buf = np->data;
memcpy(&buf[sio->offset], sio->buf, sio->len);
spinlock_release(&np->lock);
- kprintf("%d\n", sio->len);
return sio->len;
}
@@ -316,12 +325,9 @@ tmpfs_read(struct vnode *vp, struct sio_txn *sio)
spinlock_acquire(&np->lock);
- if (sio->offset > np->len - 1) {
+ if (sio->offset > np->real_size) {
return -EINVAL;
}
- if ((sio->offset + sio->len) > np->len) {
- sio->len = np->len;
- }
buf = np->data;
memcpy(sio->buf, &buf[sio->offset], sio->len);
@@ -329,6 +335,30 @@ tmpfs_read(struct vnode *vp, struct sio_txn *sio)
return sio->len;
}
+/*
+ * TMPFS get attribute callback for VFS
+ */
+static int
+tmpfs_getattr(struct vop_getattr_args *args)
+{
+ struct vnode *vp;
+ struct tmpfs_node *np;
+ struct vattr attr;
+
+ if ((vp = args->vp) == NULL) {
+ return -EIO;
+ }
+ if ((np = vp->data) == NULL) {
+ return -EIO;
+ }
+
+ memset(&attr, VNOVAL, sizeof(attr));
+ attr.size = np->real_size;
+ attr.mode = np->mode;
+ *args->res = attr;
+ return 0;
+}
+
static int
tmpfs_reclaim(struct vnode *vp)
{
@@ -378,7 +408,7 @@ tmpfs_init(struct fs_info *fip)
const struct vops g_tmpfs_vops = {
.lookup = tmpfs_lookup,
- .getattr = NULL,
+ .getattr = tmpfs_getattr,
.read = tmpfs_read,
.write = tmpfs_write,
.reclaim = tmpfs_reclaim,
diff --git a/sys/include/fs/tmpfs.h b/sys/include/fs/tmpfs.h
index b2a5bbe..acb5256 100644
--- a/sys/include/fs/tmpfs.h
+++ b/sys/include/fs/tmpfs.h
@@ -53,7 +53,9 @@ struct tmpfs_node;
* @rpath: /tmp/ relative path (for lookups)
* @type: The tmpfs node type [one-to-one to vtype]
* @len: Length of buffer
+ * @real_size: Actual size of file
* @data: The backing file data
+ * @mode: File permissions
* @dirvp: Vnode of the parent node
* @vp: Vnode of the current node
* @lock: Lock protecting this node
@@ -62,7 +64,9 @@ struct tmpfs_node {
char rpath[PATH_MAX];
uint8_t type;
size_t len;
+ size_t real_size;
void *data;
+ mode_t mode;
struct vnode *dirvp;
struct vnode *vp;
struct spinlock lock;
diff --git a/sys/include/sys/vnode.h b/sys/include/sys/vnode.h
index 1a6b2aa..b135433 100644
--- a/sys/include/sys/vnode.h
+++ b/sys/include/sys/vnode.h
@@ -35,9 +35,8 @@
#include <sys/vnode.h>
#include <sys/atomic.h>
#include <sys/sio.h>
-#include <vm/vm_obj.h>
-
#if defined(_KERNEL)
+#include <vm/vm_obj.h>
struct vops;
diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index d4c9885..0fb026f 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -369,7 +369,7 @@ fd_seek(int fildes, off_t offset, int whence)
return -EINVAL;
}
- return 0;
+ return tmp->offset;
}
/*