Finish up most of ext2 filesystem and close some bugs. Move to EXT2 but still a WIP.

This commit is contained in:
Enrico Fraccaroli
2021-12-16 22:34:44 +01:00
parent 33c02fe570
commit a0dbeb1527
11 changed files with 1064 additions and 560 deletions
+65 -45
View File
@@ -11,6 +11,7 @@
#include "string.h"
#include "stdio.h"
#include "fcntl.h"
#include "debug.h"
static inline void __parse_line(passwd_t *pwd, char *buf)
{
@@ -39,55 +40,72 @@ static inline void __parse_line(passwd_t *pwd, char *buf)
pwd->pw_shell = token;
}
static inline char *__search_entry(int fd, char *buf, int buflen, const char *name, uid_t uid)
ssize_t __readline(int fd, char *buffer, size_t buflen)
{
int ret;
char c;
int pos = 0;
while ((ret = read(fd, &c, 1U))) {
// Skip carriage return.
if (c == '\r')
continue;
if (pos >= buflen) {
errno = ERANGE;
return NULL;
}
// If we have found a newline or the EOF, parse the entry.
if ((c == '\n') || (ret == EOF)) {
// Close the buffer.
buf[pos] = 0;
// Check the entry.
if (name) {
if (strncmp(buf, name, strlen(name)) == 0)
return buf;
} else {
int uid_start = -1, col_count = 0;
for (int i = 0; i < pos; ++i) {
if (buf[i] == ':') {
if (++col_count == 2) {
uid_start = i + 1;
break;
}
}
}
if ((uid_start != -1) && (uid_start < pos)) {
// Parse the uid.
int found_uid = atoi(&buf[uid_start]);
// Check the uid.
if (found_uid == uid)
return buf;
}
memset(buffer, 0, buflen);
long num_read = read(fd, buffer, buflen);
if (num_read == 0) {
return 0;
}
char *newline = strchr(buffer, '\n');
if (newline == NULL) {
newline = strchr(buffer, EOF);
if (newline == NULL) {
newline = strchr(buffer, 0);
if (newline == NULL) {
return 0;
}
}
}
long newline_len = (int)(newline - buffer);
if (newline_len <= 0) {
return 0;
}
buffer[newline_len] = 0;
long rollback = newline_len - num_read + 1;
if (rollback > 1) {
return 0;
}
lseek(fd, rollback, SEEK_CUR);
return newline_len;
}
static inline char *__search_entry(int fd, char *buffer, int buflen, const char *name, uid_t uid)
{
while (__readline(fd, buffer, buflen)) {
if (name != NULL) {
char *name_end = strchr(buffer, ':');
if (name_end) {
*name_end = '\0';
if (strncmp(buffer, name, strlen(name)) == 0) {
*name_end = ':';
return buffer;
}
}
} else {
// Name
char *ptr = strchr(buffer, ':');
if (ptr == NULL)
continue;
// Password
++ptr;
char *uid_start = strchr(ptr, ':');
if (uid_start == NULL)
continue;
++uid_start;
ptr = strchr(uid_start, ':');
if (ptr == NULL)
continue;
*ptr = '\0';
// Parse the uid.
int found_uid = atoi(uid_start);
// Check the uid.
if (found_uid == uid) {
*ptr = ':';
return buffer;
}
// Reset the index.
pos = 0;
// If we have reached the EOF stop.
if (ret == EOF)
break;
} else {
buf[pos++] = c;
}
}
errno = ENOENT;
return NULL;
}
@@ -119,6 +137,7 @@ int getpwnam_r(const char *name, passwd_t *pwd, char *buf, size_t buflen, passwd
return 0;
int fd = open("/etc/passwd", O_RDONLY, 0);
if (fd == -1) {
pr_debug("Cannot open `/etc/passwd`\n");
errno = ENOENT;
*result = NULL;
return 0;
@@ -142,6 +161,7 @@ int getpwuid_r(uid_t uid, passwd_t *pwd, char *buf, size_t buflen, passwd_t **re
{
int fd = open("/etc/passwd", O_RDONLY, 0);
if (fd == -1) {
pr_debug("Cannot open `/etc/passwd`\n");
errno = ENOENT;
*result = NULL;
return 0;