Skip to content
Draft
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ if(NEWLIB)
-DLLVM_INCLUDE_TESTS=OFF
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON
-DCOMPILER_RT_BUILD_BUILTINS=ON
-DCOMPILER_RT_BAREMETAL_BUILD=ON
-DCOMPILER_RT_BUILD_XRAY=OFF
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF
-DCOMPILER_RT_BUILD_MEMPROF=OFF
Expand Down
50 changes: 32 additions & 18 deletions file_system/file_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ D_File *create_directory(Path *name, uint32_t mode) {
new_file->type = DIRECTORY;
new_file->child = NULL;
new_file->hard_links = 0;
// directory where user has full permissions
new_file->mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
new_file->mode = mode | S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
return new_file;
}

Expand All @@ -60,6 +59,13 @@ int link_file_to_folder(D_File *folder, D_File *file) {
// TODO set proper error number
return -1;
};

// Check if file already exists in this directory to prevent duplicates
D_File *existing = find_file_in_dir(folder, (Path){.path = file->name, .length = namelen(file->name, FS_NAME_LENGTH)});
if (existing != NULL) {
return -1;
}

// set file parent to folder
file->parent = folder;
// increase the number of hard links to the file
Expand All @@ -69,18 +75,24 @@ int link_file_to_folder(D_File *folder, D_File *file) {
file->next = NULL;
} else {
D_File *current = folder->child;
if (namecmp(file->name, FS_NAME_LENGTH, current->name, FS_NAME_LENGTH) <
0) {
int cmp_first = namecmp(file->name, FS_NAME_LENGTH, current->name, FS_NAME_LENGTH);
if (cmp_first < 0) {
file->next = current;
folder->child = file;
} else if (cmp_first == 0) {
file->hard_links -= 1;
return -1;
} else {
// Find correct position to insert
while (current->next != NULL) {
if (namecmp(file->name, FS_NAME_LENGTH, current->next->name,
FS_NAME_LENGTH) < 0) {
int cmp_next = namecmp(file->name, FS_NAME_LENGTH, current->next->name, FS_NAME_LENGTH);
if (cmp_next < 0) {
break;
} else {
current = current->next;
} else if (cmp_next == 0) {
file->hard_links -= 1;
return -1;
}
current = current->next;
}
file->next = current->next;
current->next = file;
Expand All @@ -99,8 +111,9 @@ D_File *find_file_in_dir(D_File *directory, Path file) {
}
for (D_File *current = directory->child; current != NULL;
current = current->next) {
size_t stored_name_len = namelen(current->name, FS_NAME_LENGTH);
int cmp_result =
namecmp(current->name, FS_NAME_LENGTH, file.path, file.length);
namecmp(current->name, stored_name_len, file.path, file.length);
if (cmp_result == 0) {
return current;
} else if (cmp_result > 0) {
Expand Down Expand Up @@ -162,10 +175,12 @@ D_File *create_directories(D_File *directory, Path path, char prevent_up) {
directory = candidate_dir;
continue;
}
D_File *new_dir = dandelion_alloc(sizeof(D_File), _Alignof(D_File));
new_dir->type = DIRECTORY;
memcpy(new_dir->name, current_path.path, current_path.length);
new_dir->child = NULL;
uint32_t dir_mode = S_IRUSR | S_IWUSR | S_IXUSR;
D_File *new_dir = create_directory(&current_path, dir_mode);
if (new_dir == NULL) {
return NULL;
}

int error = link_file_to_folder(directory, new_dir);
if (error < 0) {
dandelion_free(new_dir);
Expand Down Expand Up @@ -414,17 +429,16 @@ int fs_initialize(int *argc, char ***argv, char ***environ) {
fs_root->parent = NULL;
fs_root->child = NULL;
fs_root->hard_links = 1;
// Set proper root directory mode
//fs_root->mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;

// create stdio folder and stdout/stderr file
D_File *stdio_folder = dandelion_alloc(sizeof(D_File), _Alignof(D_File));
Path stdio_path = path_from_string("stdio");
D_File *stdio_folder = create_directory(&stdio_path, S_IRUSR | S_IWUSR | S_IXUSR);
if (stdio_folder == NULL) {
dandelion_exit(ENOMEM);
return -1;
}
memcpy(stdio_folder->name, "stdio", 6);
stdio_folder->type = DIRECTORY;
stdio_folder->child = NULL;
stdio_folder->hard_links = 0;
if ((error = link_file_to_folder(fs_root, stdio_folder)) != 0) {
return error;
}
Expand Down
3 changes: 3 additions & 0 deletions newlib_shim/dirent.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ long int telldir(DIR *dir) {

extern void dandelion_seekdir(DIR *dir, long int index);
void seekdir(DIR *dir, long int index) { return dandelion_seekdir(dir, index); }
int dirfd(DIR *dirp) {
return -1;
}
64 changes: 64 additions & 0 deletions newlib_shim/mman.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* sys/mman.h

Copyright 1996, 1997, 1998, 2000, 2001 Red Hat, Inc.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */

#ifndef _SYS_MMAN_H_
#define _SYS_MMAN_H_

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <stddef.h>
#include <sys/types.h>

#define PROT_NONE 0
#define PROT_READ 1
#define PROT_WRITE 2
#define PROT_EXEC 4

#define MAP_FILE 0
#define MAP_SHARED 1
#define MAP_PRIVATE 2
#define MAP_TYPE 0xF
#define MAP_FIXED 0x10
#define MAP_ANONYMOUS 0x20
#define MAP_ANON MAP_ANONYMOUS
/* Non-standard flag */
#if 0 /* Not yet implemented */
#define MAP_NORESERVE 0x4000 /* Don't reserve swap space for this mapping.
Page protection must be set explicitely
to access page. */
#endif
#define MAP_AUTOGROW 0x8000 /* Grow underlying object to mapping size.
File must be opened for writing. */

#define MAP_FAILED ((void *)-1)

/*
* Flags for msync.
*/
#define MS_ASYNC 1
#define MS_SYNC 2
#define MS_INVALIDATE 4

#ifndef __INSIDE_CYGWIN__
extern void *mmap (void *__addr, size_t __len, int __prot, int __flags, int __fd, off_t __off);
#endif
extern int munmap (void *__addr, size_t __len);
extern int mprotect (void *__addr, size_t __len, int __prot);
extern int msync (void *__addr, size_t __len, int __flags);
extern int mlock (const void *__addr, size_t __len);
extern int munlock (const void *__addr, size_t __len);

#ifdef __cplusplus
};
#endif /* __cplusplus */

#endif /* _SYS_MMAN_H_ */
30 changes: 30 additions & 0 deletions newlib_shim/newlib-cygwin-3.5.3.patch
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,33 @@ index c99ad395d..109112630 100644
#endif /* defined(__rtems__) */
#endif /* __GNU_VISIBLE */

diff --git a/newlib/libc/include/limits.h a/newlib/libc/include/limits.h
index 893f108..f07076d 100644
--- a/newlib/libc/include/limits.h
+++ b/newlib/libc/include/limits.h
@@ -145,3 +145,7 @@
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
+
+#ifndef SSIZE_MAX
+#define SSIZE_MAX LONG_MAX
+#endif
+
diff --git a/newlib/libc/include/sys/time.h b/newlib/libc/include/sys/time.h
index 5a3dec7ab..980f3378d 100644
--- a/newlib/libc/include/sys/time.h
+++ b/newlib/libc/include/sys/time.h
@@ -440,6 +440,9 @@ int futimesat (int, const char *, const struct timeval [2]);
int _gettimeofday (struct timeval *__p, void *__tz);
#endif

+time_t time(time_t *tloc);
+int utime(const char *filename, const struct utimbuf *times);
+
__END_DECLS

#endif /* !_KERNEL */
--
2.50.1

1 change: 1 addition & 0 deletions newlib_shim/patch_script
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ cp $THIS_DIR/dirent.h $1/newlib/libc/sys/dandelion/sys/dirent.h
cp $THIS_DIR/statvfs.h $1/newlib/libc/sys/dandelion/sys/statvfs.h
cp $THIS_DIR/sched.h $1/newlib/libc/sys/dandelion/sys/sched.h
cp $THIS_DIR/cpuset.h $1/newlib/libc/sys/dandelion/sys/cpuset.h
cp $THIS_DIR/mman.h $1/newlib/libc/sys/dandelion/sys/mman.h
cd $1
autoreconf
cd newlib
Expand Down
59 changes: 53 additions & 6 deletions newlib_shim/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,48 @@ int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) { return EINVAL; }

int pthread_cond_init(pthread_cond_t *restrict cond,
const pthread_condattr_t *restrict attr) {
return EAGAIN;
(void)attr;
*cond = _PTHREAD_COND_INITIALIZER;
return 0;
}

int pthread_cond_destroy(pthread_cond_t *cond) {
pthread_cond_t current = *cond;
if ((current & DESTROYED) == 0)
return EINVAL;
*cond &= ~DESTROYED;
return 0;
}
int pthread_cond_destroy(pthread_cond_t *cond) { return EINVAL; }

int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex) {
return ENOTRECOVERABLE;
if ((*cond & DESTROYED) == 0 || (*mutex & DESTROYED) == 0)
return EINVAL;
if ((*mutex & LOCKED) != 0)
return EPERM;

int r = pthread_mutex_unlock(mutex);
if (r != 0) return r;
return pthread_mutex_lock(mutex);
}

int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstim) {
return ENOTRECOVERABLE;
(void)abstim;
return pthread_cond_wait(cond, mutex);
}

int pthread_cond_signal(pthread_cond_t *cond) {
return 0;
}
int pthread_cond_signal(pthread_cond_t *cond) { return EINVAL; }
int pthread_cond_broadcast(pthread_cond_t *cond) { return EINVAL; }

int pthread_cond_broadcast(pthread_cond_t *cond) {
if ((*cond & DESTROYED) == 0)
return EINVAL;
return 0;
}


int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)) {
if (once_control->init_executed == 0) {
Expand Down Expand Up @@ -196,4 +224,23 @@ int pthread_key_delete(pthread_key_t key) {
int pthread_atfork(void (*prepare)(void), void (*parent)(void),
void (*child)(void)) {
return ENOMEM;
}

int pthread_attr_init(pthread_attr_t *attr) {
return EINVAL;
}
int pthread_attr_destroy(pthread_attr_t *attr) {
return EINVAL;
}

int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) {
return EINVAL;
}
int pthread_attr_getstacksize(const pthread_attr_t *restrict attr,
size_t *restrict stacksize) {
return EINVAL;
}

void pthread_exit(void *retval) {
return;
}
11 changes: 11 additions & 0 deletions newlib_shim/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define _POSIX_MONOTONIC_CLOCK
#include <errno.h>
#include <time.h>
#include <utime.h>
#undef errno
extern int errno;

Expand Down Expand Up @@ -68,3 +69,13 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) {
// always pretend to sleep for that amount
return 0;
}

time_t time(time_t *t) {
errno = EINVAL;
return -1;
}

int utime(const char *filename, const struct utimbuf *buf) {
errno = EINVAL;
return -1;
}
Loading
Loading