Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
/*
|
2017-10-26 11:12:07 -04:00
|
|
|
* Copyright (c) 2012, 2017 Jonas 'Sortie' Termansen.
|
Relicense Sortix to the ISC license.
I hereby relicense all my work on Sortix under the ISC license as below.
All Sortix contributions by other people are already under this license,
are not substantial enough to be copyrightable, or have been removed.
All imported code from other projects is compatible with this license.
All GPL licensed code from other projects had previously been removed.
Copyright 2011-2016 Jonas 'Sortie' Termansen and contributors.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2016-03-02 17:38:16 -05:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* fs/util.cpp
|
|
|
|
* Utility classes for kernel filesystems.
|
|
|
|
*/
|
2012-05-27 08:31:09 -04:00
|
|
|
|
2017-10-26 11:12:07 -04:00
|
|
|
#include <assert.h>
|
2012-09-22 10:44:50 -04:00
|
|
|
#include <errno.h>
|
2012-09-22 14:38:34 -04:00
|
|
|
#include <string.h>
|
2013-10-26 20:42:10 -04:00
|
|
|
|
|
|
|
#include <sortix/stat.h>
|
|
|
|
#include <sortix/seek.h>
|
|
|
|
|
|
|
|
#include <sortix/kernel/inode.h>
|
|
|
|
#include <sortix/kernel/interlock.h>
|
|
|
|
#include <sortix/kernel/ioctx.h>
|
|
|
|
#include <sortix/kernel/kernel.h>
|
|
|
|
#include <sortix/kernel/kthread.h>
|
|
|
|
#include <sortix/kernel/refcount.h>
|
|
|
|
|
2012-05-27 08:31:09 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
namespace Sortix {
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
UtilMemoryBuffer::UtilMemoryBuffer(dev_t dev, ino_t ino, uid_t owner,
|
|
|
|
gid_t group, mode_t mode, uint8_t* buf,
|
2017-10-26 11:12:07 -04:00
|
|
|
off_t bufsize, bool write, bool deletebuf)
|
2012-08-07 18:19:44 -04:00
|
|
|
{
|
2017-10-26 11:12:07 -04:00
|
|
|
assert(0 <= bufsize);
|
2012-08-07 18:19:44 -04:00
|
|
|
inode_type = INODE_TYPE_FILE;
|
|
|
|
this->filelock = KTHREAD_MUTEX_INITIALIZER;
|
|
|
|
this->stat_uid = owner;
|
|
|
|
this->stat_gid = group;
|
|
|
|
this->type = S_IFREG;
|
|
|
|
this->stat_mode = (mode & S_SETABLE) | this->type;
|
2017-10-26 11:12:07 -04:00
|
|
|
this->stat_size = bufsize;
|
2012-08-07 18:19:44 -04:00
|
|
|
this->dev = dev;
|
|
|
|
this->ino = ino ? ino : (ino_t) this;
|
2012-08-01 07:07:31 -04:00
|
|
|
this->buf = buf;
|
|
|
|
this->bufsize = bufsize;
|
|
|
|
this->write = write;
|
|
|
|
this->deletebuf = deletebuf;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
UtilMemoryBuffer::~UtilMemoryBuffer()
|
2012-08-01 07:07:31 -04:00
|
|
|
{
|
|
|
|
if ( deletebuf )
|
|
|
|
delete[] buf;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
int UtilMemoryBuffer::truncate(ioctx_t* /*ctx*/, off_t length)
|
2012-08-01 07:07:31 -04:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
ScopedLock lock(&filelock);
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( length != bufsize )
|
|
|
|
return errno = EPERM, -1;
|
2012-08-07 18:19:44 -04:00
|
|
|
return 0;
|
2012-08-01 07:07:31 -04:00
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
off_t UtilMemoryBuffer::lseek(ioctx_t* /*ctx*/, off_t offset, int whence)
|
2012-08-01 07:07:31 -04:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
ScopedLock lock(&filelock);
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( whence == SEEK_END && offset == 0 )
|
|
|
|
return bufsize;
|
|
|
|
return errno = EINVAL, -1;
|
2012-08-01 07:07:31 -04:00
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
ssize_t UtilMemoryBuffer::pread(ioctx_t* ctx, uint8_t* dest, size_t count,
|
|
|
|
off_t off)
|
2012-08-01 07:07:31 -04:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
ScopedLock lock(&filelock);
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( bufsize < off )
|
2012-08-07 18:19:44 -04:00
|
|
|
return 0;
|
2017-10-26 11:12:07 -04:00
|
|
|
off_t available = bufsize - off;
|
|
|
|
if ( (uintmax_t) available < (uintmax_t) count )
|
2012-08-07 18:19:44 -04:00
|
|
|
count = available;
|
|
|
|
if ( !ctx->copy_to_dest(dest, buf + off, count) )
|
|
|
|
return -1;
|
2012-08-01 07:07:31 -04:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-08-07 18:19:44 -04:00
|
|
|
ssize_t UtilMemoryBuffer::pwrite(ioctx_t* ctx, const uint8_t* src, size_t count,
|
|
|
|
off_t off)
|
2012-08-01 07:07:31 -04:00
|
|
|
{
|
2012-08-07 18:19:44 -04:00
|
|
|
ScopedLock lock(&filelock);
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( !write )
|
|
|
|
return errno = EROFS, -1;
|
|
|
|
if ( bufsize < off )
|
|
|
|
return errno = EFBIG, -1;
|
|
|
|
off_t available = bufsize - off;
|
|
|
|
if ( available == 0 && count )
|
|
|
|
return errno = EFBIG, -1;
|
|
|
|
if ( (uintmax_t) available < (uintmax_t) count )
|
2012-08-07 18:19:44 -04:00
|
|
|
count = available;
|
2017-10-26 11:12:07 -04:00
|
|
|
if ( !ctx->copy_from_src(buf + off, src, count) )
|
|
|
|
return -1;
|
2012-08-01 07:07:31 -04:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-05-27 08:31:09 -04:00
|
|
|
} // namespace Sortix
|