This commit is contained in:
d0p1 🏳️‍⚧️ 2024-06-19 07:31:19 +02:00
parent de26533871
commit 9dcde5fa89
7 changed files with 165 additions and 7 deletions

View file

@ -62,6 +62,8 @@
\mainmatter \mainmatter
\chapter{File, file system, and memory size limits}
\chapter{Magic Numbers} \chapter{Magic Numbers}
\begin{center} \begin{center}
@ -70,15 +72,32 @@
\textbf{Flag} & \textbf{Hexadecimal} & \textbf{ASCII} & \textbf{Data structure} \\ \textbf{Flag} & \textbf{Hexadecimal} & \textbf{ASCII} & \textbf{Data structure} \\
\hline \hline
STPDFS\_SB\_MAGIC & 0x44505453 & STPD & Superblock \\ STPDFS\_SB\_MAGIC & 0x44505453 & STPD & Superblock \\
STDFS\_BLOCK\_SIZE & 512 & & Block size\\
\hline \hline
\end{tabular} \end{tabular}
\end{center} \end{center}
\chapter{Structures} \chapter{Structures}
\section{Superblocks} \section{Superblock}
\subsection{free list}
\begin{lstlisting}[language=C] \begin{lstlisting}[language=C]
struct stpdfs_free {
uint32_t free[100];
uint8_t nfree;
};
\end{lstlisting}
\subsection{superblock}
\begin{lstlisting}[language=C]
#define STPDFS_SB_MAGIC 0x44505453
#define STPDFS_REV STPDFS_REV_1
#define STPDFS_REV_1 0x1
struct stpdfs_sb { struct stpdfs_sb {
uint32_t magic; uint32_t magic;
uint32_t isize; uint32_t isize;
@ -86,7 +105,7 @@ struct stpdfs_sb {
uint32_t free[100]; uint32_t free[100];
uint8_t nfree; uint8_t nfree;
uint8_t revision; uint8_t revision;
uint8_t state; uint16_t state;
uint64_t time; uint64_t time;
}; };
\end{lstlisting} \end{lstlisting}
@ -106,6 +125,46 @@ struct inode {
uint64_t modtime; uint64_t modtime;
}; };
\end{lstlisting} \end{lstlisting}
\subsection{flags}
\begin{center}
\begin{tabular}{ |c|c|c|c| }
\hline
\textbf{flags} & \textbf{value} & \textbf{Description} \\
\hline
STPDFS\_INODE\_ALLOCATED & 1 << 15 & if set inode is allocated \\
STPDFS\_INODE\_LARGE\_FILE & 1 << 0 & \\
STPDFS\_INODE\_LZP & 1 << 1 & if set data are compressed \\
STPDFS\_INODE\_ENC & 1 << 2 & if set data are encrypted \\
\hline
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{ |c|c| }
zone 0-6 & direct \\
zone 7 & indirect \\
zone 8 & double indirect \\
zone 9 & triple indirect \\
\end{tabular}
\end{center}
\subsection{mode}
\begin{center}
\begin{tabular}{ |c|c|c|c| }
\hline
\textbf{mode} & \textbf{value} & \textbf{Description} \\
STPDFS\_S\_IFSOCK & 0xA000 & \\
STPDFS\_S\_IFLNK & 0xC000 & \\
STPDFS\_S\_IFREG & 0x8000 & \\
STPDFS\_S\_IFBLK & 0x6000 & \\
STPDFS\_S\_IFDIR & 0x4000 & \\
\hline
\end{tabular}
\end{center}
\appendix \appendix
\end{document} \end{document}

View file

@ -1,5 +1,7 @@
noinst_LIBRARIES = libstpdfs.a noinst_LIBRARIES = libstpdfs.a
libstpdfs_a_SOURCES = block.c \ libstpdfs_a_SOURCES = block.c \
inode.c \
dir.c
superblock.c \ superblock.c \
compression/lzp.c \ compression/lzp.c \
crypto/hchacha.c \ crypto/hchacha.c \

View file

@ -2,6 +2,7 @@
# define STPDFS_CRYPTO_CHACHA_H 1 # define STPDFS_CRYPTO_CHACHA_H 1
# include <stdint.h> # include <stdint.h>
# include <stddef.h>
# define CHACHA_CONST1 "expa" # define CHACHA_CONST1 "expa"
# define CHACHA_CONST2 "nd 3" # define CHACHA_CONST2 "nd 3"

16
lib/data.c Normal file
View file

@ -0,0 +1,16 @@
#include "stpdfs.h"
int
stpdfs_add_data(int fd, struct stpdfs_sb *sb, struct stpdfs_inode *inode, uint8_t *buffer, size_t size)
{
size_t idx;
size_t max_zone;
if (size > STPDFS_BLOCK_SIZE)
{
for (idx = 0; idx < size / STPDFS_BLOCK_SIZE; idx++)
{
stpdfs_add_data(fd, sb, inode, buffer, 512 * idx);
}
}
}

63
lib/dir.c Normal file
View file

@ -0,0 +1,63 @@
#include "stpdfs.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
int
stpdfs_dir_add_entry(int fd, uint8_t inonum, const char *name, uint32_t target)
{
struct stpdfs_inode inode;
struct stpdfs_dirent dirent[STPDFS_DIRENT_PER_BLOCK];
struct stpdfs_sb sb;
size_t max_zone;
size_t zone;
uint32_t blocknum;
stpdfs_read_superblock(fd, &sb);
stpdfs_read_inode(fd, inonum, &inode);
if (!(inode.mode & STPDFS_S_IFDIR))
{
return (-1);
}
max_zone = (inode.size + sizeof(struct stpdfs_dirent)) / STPDFS_DIRENT_PER_BLOCK;
zone = max_zone;
if (max_zone > 2113671)
{
}
if (max_zone > 16519)
{
}
else if (max_zone > 135)
{
}
else if (max_zone > 7)
{
}
else
{
if (inode.zones[max_zone] == 0)
{
inode.zones[max_zone] = stpdfs_alloc_block(fd, &sb);
}
blocknum = inode.zones[max_zone];
}
stpdfs_read(fd, blocknum, &dirent, sizeof(struct stpdfs_dirent) * STPDFS_DIRENT_PER_BLOCK);
dirent[inode.size % STPDFS_DIRENT_PER_BLOCK].inode = target;
memcpy(dirent[inode.size % STPDFS_DIRENT_PER_BLOCK].filename, name, STPDFS_NAME_MAX);
stpdfs_write(fd, blocknum, &dirent, sizeof(struct stpdfs_dirent) * STPDFS_DIRENT_PER_BLOCK);
inode.size += sizeof(struct stpdfs_dirent);
stpdfs_write_inode(fd, inonum, &inode);
stpdfs_write(fd, 1, &sb, sizeof(struct stpdfs_sb));
return (0);
}

View file

@ -9,14 +9,28 @@ stpdfs_locate_inode(uint32_t inode)
} }
int int
stpdfs_read_inode() stpdfs_read_inode(int fd, uint32_t inodenum, struct stpdfs_inode *ino)
{ {
struct stpdfs_inode inodes[STPDFS_INODES_PER_BLOCK];
stpdfs_read(fd, 2 + inodenum / STPDFS_INODES_PER_BLOCK, (uint8_t *)&inodes, sizeof(struct stpdfs_inode) * STPDFS_INODES_PER_BLOCK);
memcpy(ino, &inodes[inodenum % STPDFS_INODES_PER_BLOCK], sizeof(struct stpdfs_inode));
return (0); return (0);
} }
int int
stpdfs_write_inode() stpdfs_write_inode(int fd, uint32_t inodenum, const struct stpdfs_inode *inode)
{ {
struct stpdfs_inode inodes[STPDFS_INODES_PER_BLOCK];
stpdfs_read(fd, 2 + inodenum / STPDFS_INODES_PER_BLOCK, (uint8_t *)&inodes, sizeof(struct stpdfs_inode) * STPDFS_INODES_PER_BLOCK);
memcpy(&inodes[inodenum % STPDFS_INODES_PER_BLOCK], inode, sizeof(struct stpdfs_inode));
stpdfs_write(fd, 2 + inodenum / STPDFS_INODES_PER_BLOCK, (uint8_t *)&inodes, sizeof(struct stpdfs_inode) * STPDFS_INODES_PER_BLOCK);
return (0); return (0);
} }

View file

@ -16,6 +16,7 @@
# define STPDFS_NAME_MAX 28 # define STPDFS_NAME_MAX 28
# define STPDFS_INODES_PER_BLOCK (STPDFS_BLOCK_SIZE / (sizeof(struct stpdfs_inode))) # define STPDFS_INODES_PER_BLOCK (STPDFS_BLOCK_SIZE / (sizeof(struct stpdfs_inode)))
# define STPDFS_DIRENT_PER_BLOCK (STPDFS_BLOCK_SIZE / (sizeof(struct stpdfs_dirent)))
# define STPDFS_ROOT_INO 1 # define STPDFS_ROOT_INO 1
@ -96,5 +97,7 @@ int stpdfs_read_superblock(int fd, struct stpdfs_sb *sb);
/* inode.c */ /* inode.c */
uint32_t stpdfd_alloc_inode(int fd, struct stpdfs_sb *sb); uint32_t stpdfd_alloc_inode(int fd, struct stpdfs_sb *sb);
int stpdfs_read_inode(int fd, uint32_t inodenum, struct stpdfs_inode *ino);
int stpdfs_write_inode(int fd, uint32_t inodenum, const struct stpdfs_inode *inode);
#endif /* !STPDFS_H */ #endif /* !STPDFS_H */