Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: de6a984c5e
Fetching contributors…

Cannot retrieve contributors at this time

423 lines (268 sloc) 8.137 kb
/*
Copyright (C) 2001-2012, Parrot Foundation.
=head1 NAME
src/pmc/os.pmc - Files and Directories PMC
=head1 DESCRIPTION
C<OS> is a PMC type which provides access to the filesystem
files and directories.
=head2 Usage
From PIR code:
$P0 = new 'OS'
From NQP:
my $os := pir::new__ps("OS");
From Winxed:
var os = new 'OS';
=head2 Methods
=over 4
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_CANNOT_RETURN_NULL
static PMC * stat_buf_to_array(PARROT_INTERP,
ARGIN(const Parrot_Stat_Buf *buf))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
#define ASSERT_ARGS_stat_buf_to_array __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(buf))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/*
=item C<static PMC * stat_buf_to_array(PARROT_INTERP, const Parrot_Stat_Buf
*buf)>
Converts a stat buffer to a Perl-like array.
=cut
*/
PARROT_CANNOT_RETURN_NULL
static PMC *
stat_buf_to_array(PARROT_INTERP, ARGIN(const Parrot_Stat_Buf *buf))
{
ASSERT_ARGS(stat_buf_to_array)
PMC * const array = Parrot_pmc_new(interp, enum_class_FixedPMCArray);
VTABLE_set_integer_native(interp, array, 13);
VTABLE_set_integer_keyed_int(interp, array, 0, buf->dev);
VTABLE_set_integer_keyed_int(interp, array, 1, buf->inode);
VTABLE_set_integer_keyed_int(interp, array, 2, buf->mode);
VTABLE_set_integer_keyed_int(interp, array, 3, buf->n_links);
VTABLE_set_integer_keyed_int(interp, array, 4, buf->uid);
VTABLE_set_integer_keyed_int(interp, array, 5, buf->gid);
VTABLE_set_integer_keyed_int(interp, array, 6, 0);
VTABLE_set_integer_keyed_int(interp, array, 7, buf->size);
VTABLE_set_integer_keyed_int(interp, array, 8, buf->access_time.tv_sec);
VTABLE_set_integer_keyed_int(interp, array, 9, buf->modify_time.tv_sec);
VTABLE_set_integer_keyed_int(interp, array, 10, buf->change_time.tv_sec);
VTABLE_set_integer_keyed_int(interp, array, 11, buf->block_size);
VTABLE_set_integer_keyed_int(interp, array, 12, buf->blocks);
return array;
}
pmclass OS pmc {
/*
=item C<STRING *cwd()>
Returns the current working directory.
=cut
*/
METHOD cwd() {
STRING * const cwd = Parrot_file_getcwd(INTERP);
RETURN(STRING *cwd);
}
/*
=item C<void chdir(STRING *path)>
Changes the current working directory to the one specified by C<path>.
=cut
*/
METHOD chdir(STRING *path) {
Parrot_file_chdir(INTERP, path);
}
/*
=item C<void unlink(STRING *file)>
Removes file C<file> from the file system.
=cut
*/
METHOD unlink(STRING *file) {
Parrot_file_unlink(INTERP, file);
}
/*
=item C<void rmdir(STRING *path)>
Removes directory C<path> from the file system, if it is empty.
=cut
*/
METHOD rmdir(STRING *path) {
Parrot_file_rmdir(INTERP, path);
}
/*
/*
=item C<void rm(STRING *path)>
Calls C<remove> to remove the file or empty directory specified by
C<path>.
=cut
*/
METHOD rm(STRING *path) {
if (Parrot_file_stat_intval(INTERP, path, STAT_ISDIR)) {
Parrot_file_rmdir(INTERP, path);
}
else {
Parrot_file_unlink(INTERP, path);
}
}
/*
=item C<void mkdir(STRING *path, STRING *mode)>
Creates a directory specified by C<path> with mode C<mode>.
=cut
*/
METHOD mkdir(STRING *path, INTVAL mode) {
Parrot_file_mkdir(INTERP, path, mode);
}
/*
=item C<fixedpmcarray *stat(STRING *path)>
Stats a file, and returns a 13 position array as in Perl:
0 dev device number of filesystem
1 ino inode number
2 mode file mode (type and permissions)
3 nlink number of (hard) links to the file
4 uid numeric user ID of file's owner
5 gid numeric group ID of file's owner
6 rdev the device identifier (special files only)
7 size total size of file, in bytes
8 atime last access time in seconds since the epoch
9 mtime last modify time in seconds since the epoch
10 ctime inode change time in seconds since the epoch (*)
11 blksize preferred block size for file system I/O
12 blocks actual number of blocks allocated
11 and 12 are not available under Windows.
=cut
TT #849: Provide a mechanism for setting 'mtime' and 'atime':
https://trac.parrot.org/parrot/ticket/849
*/
METHOD stat(STRING *path) {
Parrot_Stat_Buf buf;
PMC *array;
Parrot_file_stat(interp, path, &buf);
array = stat_buf_to_array(interp, &buf);
RETURN(PMC *array);
}
/*
=item C<fixedpmcarray *lstat(STRING *path)>
Lstats a file, and returns a 13 position array as in Perl. See C<stat>.
=cut
*/
METHOD lstat(STRING *path) {
Parrot_Stat_Buf buf;
PMC *array;
Parrot_file_stat(interp, path, &buf);
array = stat_buf_to_array(interp, &buf);
RETURN(PMC *array);
}
/*
=item C<void symlink(STRING *from, STRING *to)>
Creates a symlink
=cut
*/
METHOD symlink(STRING *from, STRING *to) {
Parrot_file_symlink(INTERP, from, to);
}
/*
=item C<void link(STRING *from, STRING *to)>
Creates a hard link
=cut
*/
METHOD link(STRING *from, STRING *to) {
Parrot_file_link(INTERP, from, to);
}
/*
=item C<INTVAL umask(INTVAL mask)>
umask sets the process's file mode creation mask (and returns the
previous one).
=cut
*/
METHOD umask(INTVAL mask) {
const INTVAL old = Parrot_file_umask(INTERP, mask);
RETURN(INTVAL old);
}
/*
=item C<INTVAL chroot(STRING *path)>
it makes the named directory the new root directory for all further
pathnames that begin with a "/" by your process and all its children.
B<NOTE>: perl restricts this operation to superusers. It might be a good
idea to do the same with parrot.
=cut
*/
METHOD chroot(STRING *path) {
Parrot_file_chroot(INTERP, path);
}
/*
=item C<PMC *readdir(STRING *path)>
Reads entries from a directory.
=cut
*/
METHOD readdir(STRING *path) {
PMC * const array = Parrot_file_readdir(INTERP, path);
RETURN(PMC *array);
}
/*
=item C<rename(STRING *oldpath, STRING *newpath)>
This method is a wrapper for rename(2). On error a SystemError exception is
thrown.
=cut
*/
METHOD rename(STRING *oldpath, STRING *newpath) {
Parrot_file_rename(INTERP, oldpath, newpath);
}
/*
=item C<chmod(STRING *path, INTVAL mode)>
=cut
*/
METHOD chmod(STRING *path, INTVAL mode) {
Parrot_file_chmod(INTERP, path, mode);
}
/*
=item C<get_user_id()>
Returns the ID number of the current user. This is platform-dependent.
=cut
*/
METHOD get_user_id() {
const UINTVAL uid = Parrot_get_user_id();
RETURN(INTVAL uid);
}
/*
=item C<can_execute(STRING *filename)>
=item C<can_read(STRING *filename)>
=item C<can_write(STRING *filename)>
=cut
*/
METHOD can_execute(STRING *filename) {
const INTVAL can = Parrot_file_can_execute(INTERP, filename);
RETURN(INTVAL can);
}
METHOD can_read(STRING *filename) {
const INTVAL can = Parrot_file_can_read(INTERP, filename);
RETURN(INTVAL can);
}
METHOD can_write(STRING *filename) {
const INTVAL can = Parrot_file_can_write(INTERP, filename);
RETURN(INTVAL can);
}
/*
=item C<exists(STRING *filename)>
Determine if the file exists. Return C<1> if it exists. Return C<0> otherwise.
=cut
*/
METHOD exists(STRING *filename) {
const INTVAL e = Parrot_file_stat_intval(INTERP, filename, STAT_EXISTS);
RETURN(INTVAL e);
}
}
/*
=back
=head1 SEE ALSO
chdir(2), getcwd(3), unlink(2), mkdir(2), stat(2), lstat(2),
symlink(2), link(2), umask(2), chroot(2)
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/
Jump to Line
Something went wrong with that request. Please try again.