Added unlink(2) and rm(1).

This commit is contained in:
Jonas 'Sortie' Termansen 2011-11-21 18:49:55 +01:00
parent 66f6055a13
commit 8b2b52b9f6
12 changed files with 79 additions and 2 deletions

View File

@ -144,7 +144,6 @@ int tcsetpgrp(int, pid_t);
int truncate(const char*, off_t);
char* ttyname(int);
int ttyname_r(int, char*, size_t);
int unlink(const char*);
int unlinkat(int, const char*, int);
#if __POSIX_OBSOLETE <= 200801
@ -169,6 +168,7 @@ unsigned sleep(unsigned);
#if __POSIX_OBSOLETE <= 200112
int usleep(useconds_t useconds);
#endif
int unlink(const char*);
ssize_t write(int, const void*, size_t);
__END_DECLS

View File

@ -57,6 +57,7 @@ namespace Maxsi
const int ENOTDIR = 22;
const int ENOMEM = 23;
const int ERANGE = 24;
const int EISDIR = 25;
extern int _errornumber;

View File

@ -40,6 +40,7 @@ namespace Maxsi
DEFN_SYSCALL3(int, SysReadDirEnts, 24, int, struct sortix_dirent*, size_t);
DEFN_SYSCALL1(int, SysChDir, 25, const char*);
DEFN_SYSCALL2(char*, SysGetCWD, 26, char*, size_t);
DEFN_SYSCALL1(int, SysUnlink, 27, const char*);
size_t Print(const char* Message)
{
@ -114,6 +115,11 @@ namespace Maxsi
{
return SysGetCWD(buf, size);
}
extern "C" int unlink(const char* pathname)
{
return SysUnlink(pathname);
}
#endif
}

View File

@ -53,6 +53,21 @@ namespace Sortix
return result;
}
bool Unlink(const char* path)
{
Process* process = CurrentProcess();
const char* wd = process->workingdir;
char* abs = Directory::MakeAbsolute(wd, path);
if ( !abs ) { Error::Set(Error::ENOMEM); return false; }
size_t pathoffset = 0;
DevFileSystem* fs = Mount::WhichFileSystem(abs, &pathoffset);
if ( !fs ) { delete[] abs; return false; }
bool result = fs->Unlink(abs + pathoffset);
delete[] abs;
return result;
}
int SysOpen(const char* path, int flags, mode_t mode)
{
Process* process = CurrentProcess();
@ -63,9 +78,15 @@ namespace Sortix
return fd;
}
int SysUnlink(const char* path)
{
return Unlink(path) ? 0 : -1;
}
void Init()
{
Syscall::Register(SYSCALL_OPEN, (void*) SysOpen);
Syscall::Register(SYSCALL_UNLINK, (void*) SysUnlink);
}
}

View File

@ -55,6 +55,7 @@ namespace Sortix
{
public:
virtual Device* Open(const char* path, int flags, mode_t mode) = 0;
virtual bool Unlink(const char* path) = 0;
public:
virtual bool IsType(unsigned type) { return type == Device::FILESYSTEM; }
@ -88,6 +89,7 @@ namespace Sortix
{
void Init();
Device* Open(const char* path, int flags, mode_t mode);
bool Unlink(const char* path);
}
}

View File

@ -228,5 +228,11 @@ namespace Sortix
return result;
}
bool DevInitFS::Unlink(const char* path)
{
Error::Set(Error::EROFS);
return false;
}
}

View File

@ -40,6 +40,7 @@ namespace Sortix
public:
virtual Device* Open(const char* path, int flags, mode_t mode);
virtual bool Unlink(const char* path);
};
}

View File

@ -302,6 +302,23 @@ namespace Sortix
return file;
}
bool DevRAMFS::Unlink(const char* path)
{
if ( *path == '\0' || ( *path++ == '/' && *path == '\0' ) )
{
Error::Set(Error::EISDIR);
return false;
}
size_t index = files->Search(LookupFile, path);
if ( index == SIZE_MAX ) { Error::Set(Error::ENOENT); return false; }
Device* dev = files->Remove(index);
ASSERT(dev);
dev->Unref();
return true;
}
size_t DevRAMFS::GetNumFiles()
{
if ( !files ) { return 0; }

View File

@ -40,6 +40,7 @@ namespace Sortix
public:
virtual Device* Open(const char* path, int flags, mode_t mode);
virtual bool Unlink(const char* path);
private:
Maxsi::SortedList<DevRAMFSFile*>* files;

View File

@ -52,7 +52,8 @@
#define SYSCALL_READDIRENTS 24
#define SYSCALL_CHDIR 25
#define SYSCALL_GETCWD 26
#define SYSCALL_MAX_NUM 27 /* index of highest constant + 1 */
#define SYSCALL_UNLINK 27
#define SYSCALL_MAX_NUM 28 /* index of highest constant + 1 */
#endif

View File

@ -7,6 +7,7 @@ LOCALBINARIES:=\
init \
cat \
cp \
rm \
sh \
mxsh \
clear \

20
utils/rm.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if ( argc < 2 ) { printf("usage: %s <file> ...\n"); return 0; }
int result = 0;
for ( int i = 1; i < argc; i++ )
{
if ( unlink(argv[i]) )
{
printf("%s: unable to unlink: %s\n", argv[0], argv[i]);
result = 1;
}
}
return result;
}