Commit 6a257d04 authored by Tom Niget's avatar Tom Niget

Add os module

parent 895a8413
//
// Created by Tom on 04/07/2023.
//
#ifndef TYPON_OS_HPP
#define TYPON_OS_HPP
#include "builtins.hpp"
#include <dirent.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#undef st_atime
#undef st_mtime
#undef st_ctime
int no_special(const struct dirent *d) {
return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
}
namespace py_os {
struct os_t {
FUNCTION(
auto, fsdecode, (std::string s) { return s; })
struct {
struct type {
int st_mode;
unsigned long long st_ino;
dev_t st_dev;
unsigned int st_nlink;
unsigned int st_uid;
unsigned int st_gid;
unsigned long long st_size;
double st_atime;
double st_mtime;
double st_ctime;
unsigned long long st_blocks;
unsigned int st_blksize;
dev_t st_rdev;
int st_gen;
double st_birthtime;
};
} Stat_Result;
struct {
struct type {
PyStr name;
PyStr path;
};
} DirEntry;
struct {
struct type {
using value_type = decltype(DirEntry)::type;
using reference = decltype(DirEntry)::type;
METHOD(type, py_enter, (), { return *self; })
METHOD(void, py_exit, (), {
if (self->namelist) {
free(self->namelist);
}
})
METHOD(auto, begin, (), { return *self; })
METHOD(auto, end, (),
{ return type(self->basepath, self->namelist, self->n, self->n); })
decltype(DirEntry)::type operator*() {
auto name = PyStr(this->namelist[this->current]->d_name);
return decltype(DirEntry)::type{name, this->basepath + name};
}
type(const PyStr &basepath, struct dirent **namelist, int n,
int current = 0)
: basepath(basepath), namelist(namelist), n(n), current(current) {
if (this->basepath[this->basepath.size() - 1] != '/') {
this->basepath += '/';
}
}
type() {}
bool operator!=(const type &other) {
return this->current != other.current;
}
void operator++() { this->current++; }
PyStr basepath;
struct dirent **namelist;
int n;
int current;
};
} _Scandiriterator;
FUNCTION(typon::Task<decltype(Stat_Result)::type>, stat, (const PyStr &path),
{
const char *path_c = path.c_str();
struct statx statxbuf;
if (int err = co_await typon::io::statx(AT_FDCWD, path_c, 0,
STATX_SIZE, &statxbuf)) {
system_error(-err, "statx()");
}
co_return decltype(Stat_Result)::type{
statxbuf.stx_mode,
statxbuf.stx_ino,
makedev(statxbuf.stx_dev_major, statxbuf.stx_dev_minor),
statxbuf.stx_nlink,
statxbuf.stx_uid,
statxbuf.stx_gid,
statxbuf.stx_size,
statxbuf.stx_atime.tv_sec + statxbuf.stx_atime.tv_nsec / 1e9f,
statxbuf.stx_mtime.tv_sec + statxbuf.stx_mtime.tv_nsec / 1e9f,
statxbuf.stx_ctime.tv_sec + statxbuf.stx_ctime.tv_nsec / 1e9f,
statxbuf.stx_blocks,
statxbuf.stx_blksize,
makedev(statxbuf.stx_rdev_major, statxbuf.stx_rdev_minor),
0,
statxbuf.stx_btime.tv_sec + statxbuf.stx_btime.tv_nsec / 1e9};
})
FUNCTION(decltype(_Scandiriterator)::type, scandir, (const PyStr &path), {
const char *path_c = path.c_str();
struct dirent **namelist;
int n = ::scandir(path_c, &namelist, no_special, alphasort);
if (n < 0) {
system_error(-n, "scandir()");
}
return decltype(_Scandiriterator)::type(path, namelist, n);
});
FUNCTION(PyStr, readlink, (const PyStr &path), {
const char *path_c = path.c_str();
char buf[PATH_MAX];
ssize_t nbytes = ::readlink(path_c, buf, sizeof(buf));
if (nbytes < 0) {
system_error(-nbytes, "readlink()");
}
return PyStr(buf, nbytes);
})
} all;
auto &get_all() { return all; }
} // namespace py_os
using PyStat_Result = decltype(py_os::all.Stat_Result)::type;
using Py_Scandiriterator = decltype(py_os::all.scandir({}));
#endif // TYPON_OS_HPP
# coding: utf-8
from typing import Self
class stat_result:
st_mode: int
st_ino: int
st_dev: int
st_nlink: int
st_uid: int
st_gid: int
st_size: int
st_atime: float
st_mtime: float
st_ctime: float
st_blocks: int
st_blksize: int
st_rdev: int
st_flags: int
st_gen: int
st_birthtime: float
class DirEntry:
name: str
path: str
class _ScandirIterator:
def __enter__(self) -> Self: ...
def __exit__(self) -> bool: ...
def __iter__(self) -> Self: ...
def __next__(self) -> DirEntry: ...
def scandir(path: str = ".") -> _ScandirIterator: ...
def fsdecode(filename: str) -> str: ...
def stat(path: str) -> Task[stat_result]: ...
def readlink(path: str) -> str: ...
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment