2010-01-26 00:18:02 +01:00
|
|
|
/* This file contains file metadata retrieval and manipulation routines.
|
|
|
|
*
|
|
|
|
* The entry points into this file are:
|
|
|
|
* get_mode return a file's mode
|
|
|
|
* do_stat perform the STAT file system call
|
|
|
|
* do_chmod perform the CHMOD file system call
|
|
|
|
* do_utime perform the UTIME file system call
|
|
|
|
*
|
|
|
|
* Created:
|
|
|
|
* April 2009 (D.C. van Moolenbroek)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "inc.h"
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* get_mode *
|
|
|
|
*===========================================================================*/
|
2014-05-01 16:19:28 +02:00
|
|
|
mode_t get_mode(struct inode *ino, int mode)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
2012-04-09 17:17:42 +02:00
|
|
|
/* Return the mode for an inode, given the inode and the retrieved mode.
|
2010-01-26 00:18:02 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
mode &= S_IRWXU;
|
|
|
|
mode = mode | (mode >> 3) | (mode >> 6);
|
|
|
|
|
|
|
|
if (IS_DIR(ino))
|
2012-04-09 18:08:26 +02:00
|
|
|
mode = S_IFDIR | (mode & sffs_params->p_dir_mask);
|
2010-01-26 00:18:02 +01:00
|
|
|
else
|
2012-04-09 18:08:26 +02:00
|
|
|
mode = S_IFREG | (mode & sffs_params->p_file_mask);
|
2010-01-26 00:18:02 +01:00
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
if (state.s_read_only)
|
2012-04-09 17:17:42 +02:00
|
|
|
mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_stat *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
int do_stat(void)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
2010-08-07 13:50:15 +02:00
|
|
|
/* Retrieve inode status.
|
2010-01-26 00:18:02 +01:00
|
|
|
*/
|
|
|
|
struct inode *ino;
|
2012-04-09 18:08:26 +02:00
|
|
|
struct sffs_attr attr;
|
2010-01-26 00:18:02 +01:00
|
|
|
struct stat stat;
|
|
|
|
char path[PATH_MAX];
|
2014-05-01 16:19:28 +02:00
|
|
|
ino_t ino_nr;
|
2010-01-26 00:18:02 +01:00
|
|
|
int r;
|
|
|
|
|
2014-05-01 10:05:07 +02:00
|
|
|
ino_nr = m_in.m_vfs_fs_stat.inode;
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
/* Don't increase the inode refcount: it's already open anyway */
|
2010-05-10 15:26:00 +02:00
|
|
|
if ((ino = find_inode(ino_nr)) == NULL)
|
2010-01-26 00:18:02 +01:00
|
|
|
return EINVAL;
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
attr.a_mask = SFFS_ATTR_MODE | SFFS_ATTR_SIZE | SFFS_ATTR_CRTIME |
|
|
|
|
SFFS_ATTR_ATIME | SFFS_ATTR_MTIME | SFFS_ATTR_CTIME;
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
if ((r = verify_inode(ino, path, &attr)) != OK)
|
|
|
|
return r;
|
|
|
|
|
2011-07-01 21:35:54 +02:00
|
|
|
memset(&stat, 0, sizeof(struct stat));
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
stat.st_dev = state.s_dev;
|
2010-01-26 00:18:02 +01:00
|
|
|
stat.st_ino = ino_nr;
|
|
|
|
stat.st_mode = get_mode(ino, attr.a_mode);
|
2012-04-09 18:08:26 +02:00
|
|
|
stat.st_uid = sffs_params->p_uid;
|
|
|
|
stat.st_gid = sffs_params->p_gid;
|
2010-01-26 00:18:02 +01:00
|
|
|
stat.st_rdev = NO_DEV;
|
2014-02-24 17:41:05 +01:00
|
|
|
stat.st_size = attr.a_size;
|
2012-03-30 01:39:12 +02:00
|
|
|
stat.st_atimespec = attr.a_atime;
|
|
|
|
stat.st_mtimespec = attr.a_mtime;
|
|
|
|
stat.st_ctimespec = attr.a_ctime;
|
|
|
|
stat.st_birthtimespec = attr.a_crtime;
|
2010-01-26 00:18:02 +01:00
|
|
|
|
2011-07-01 21:35:54 +02:00
|
|
|
stat.st_blocks = stat.st_size / S_BLKSIZE;
|
|
|
|
if (stat.st_size % S_BLKSIZE != 0)
|
|
|
|
stat.st_blocks += 1;
|
|
|
|
|
|
|
|
stat.st_blksize = BLOCK_SIZE;
|
|
|
|
|
2010-01-26 00:18:02 +01:00
|
|
|
/* We could make this more accurate by iterating over directory inodes'
|
|
|
|
* children, counting how many of those are directories as well.
|
|
|
|
* It's just not worth it.
|
|
|
|
*/
|
|
|
|
stat.st_nlink = 0;
|
2010-05-10 15:26:00 +02:00
|
|
|
if (ino->i_parent != NULL) stat.st_nlink++;
|
2010-01-26 00:18:02 +01:00
|
|
|
if (IS_DIR(ino)) {
|
|
|
|
stat.st_nlink++;
|
|
|
|
if (HAS_CHILDREN(ino)) stat.st_nlink++;
|
|
|
|
}
|
|
|
|
|
2014-05-01 10:05:07 +02:00
|
|
|
return sys_safecopyto(m_in.m_source, m_in.m_vfs_fs_stat.grant, 0,
|
2012-06-16 03:46:15 +02:00
|
|
|
(vir_bytes) &stat, sizeof(stat));
|
2010-01-26 00:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_chmod *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
int do_chmod(void)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Change file mode.
|
|
|
|
*/
|
|
|
|
struct inode *ino;
|
|
|
|
char path[PATH_MAX];
|
2012-04-09 18:08:26 +02:00
|
|
|
struct sffs_attr attr;
|
2010-01-26 00:18:02 +01:00
|
|
|
int r;
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
if (state.s_read_only)
|
2010-01-26 00:18:02 +01:00
|
|
|
return EROFS;
|
|
|
|
|
2014-04-30 20:37:50 +02:00
|
|
|
if ((ino = find_inode(m_in.m_vfs_fs_chmod.inode)) == NULL)
|
2010-01-26 00:18:02 +01:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if ((r = verify_inode(ino, path, NULL)) != OK)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* Set the new file mode. */
|
2012-04-09 18:08:26 +02:00
|
|
|
attr.a_mask = SFFS_ATTR_MODE;
|
2014-04-30 20:37:50 +02:00
|
|
|
attr.a_mode = m_in.m_vfs_fs_chmod.mode; /* no need to convert in this direction */
|
2010-01-26 00:18:02 +01:00
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
if ((r = sffs_table->t_setattr(path, &attr)) != OK)
|
2010-01-26 00:18:02 +01:00
|
|
|
return r;
|
|
|
|
|
|
|
|
/* We have no idea what really happened. Query for the mode again. */
|
|
|
|
if ((r = verify_path(path, ino, &attr, NULL)) != OK)
|
|
|
|
return r;
|
|
|
|
|
2014-04-30 20:37:50 +02:00
|
|
|
m_out.m_fs_vfs_chmod.mode = get_mode(ino, attr.a_mode);
|
2010-01-26 00:18:02 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* do_utime *
|
|
|
|
*===========================================================================*/
|
2014-02-24 14:00:17 +01:00
|
|
|
int do_utime(void)
|
2010-01-26 00:18:02 +01:00
|
|
|
{
|
|
|
|
/* Set file times.
|
|
|
|
*/
|
|
|
|
struct inode *ino;
|
|
|
|
char path[PATH_MAX];
|
2012-04-09 18:08:26 +02:00
|
|
|
struct sffs_attr attr;
|
2010-01-26 00:18:02 +01:00
|
|
|
int r;
|
|
|
|
|
2012-04-09 18:08:26 +02:00
|
|
|
if (state.s_read_only)
|
2010-01-26 00:18:02 +01:00
|
|
|
return EROFS;
|
|
|
|
|
2014-04-29 20:08:54 +02:00
|
|
|
if ((ino = find_inode(m_in.m_vfs_fs_utime.inode)) == NULL)
|
2010-01-26 00:18:02 +01:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if ((r = verify_inode(ino, path, NULL)) != OK)
|
|
|
|
return r;
|
|
|
|
|
2011-12-21 23:29:29 +01:00
|
|
|
attr.a_mask = 0;
|
|
|
|
|
2014-04-29 20:08:54 +02:00
|
|
|
switch(m_in.m_vfs_fs_utime.acnsec) {
|
2011-12-21 23:29:29 +01:00
|
|
|
case UTIME_OMIT: /* do not touch */
|
|
|
|
break;
|
|
|
|
case UTIME_NOW:
|
|
|
|
/* XXX VFS should have time() into ACTIME, for compat; we trust it! */
|
2014-04-29 20:08:54 +02:00
|
|
|
m_in.m_vfs_fs_utime.acnsec = 0;
|
2011-12-21 23:29:29 +01:00
|
|
|
/*FALLTHROUGH*/
|
|
|
|
default:
|
2014-04-29 20:08:54 +02:00
|
|
|
/* cases m_in.m_vfs_fs_utime.acnsec < 0 || m_in.m_vfs_fs_utime.acnsec >= 1E9
|
2011-12-21 23:29:29 +01:00
|
|
|
* are caught by VFS to cooperate with old instances of EXT2
|
|
|
|
*/
|
2014-04-29 20:08:54 +02:00
|
|
|
attr.a_atime.tv_sec = m_in.m_vfs_fs_utime.actime;
|
|
|
|
attr.a_atime.tv_nsec = m_in.m_vfs_fs_utime.acnsec;
|
2011-12-21 23:29:29 +01:00
|
|
|
attr.a_mask |= SFFS_ATTR_ATIME;
|
|
|
|
break;
|
|
|
|
}
|
2014-04-29 20:08:54 +02:00
|
|
|
switch(m_in.m_vfs_fs_utime.modnsec) {
|
2011-12-21 23:29:29 +01:00
|
|
|
case UTIME_OMIT: /* do not touch */
|
|
|
|
break;
|
|
|
|
case UTIME_NOW:
|
|
|
|
/* XXX VFS should have time() into MODTIME, for compat; we trust it! */
|
2014-04-29 20:08:54 +02:00
|
|
|
m_in.m_vfs_fs_utime.modnsec = 0;
|
2011-12-21 23:29:29 +01:00
|
|
|
/*FALLTHROUGH*/
|
|
|
|
default:
|
2014-04-29 20:08:54 +02:00
|
|
|
/* cases m_in.m_vfs_fs_utime.modnsec < 0 || m_in.m_vfs_fs_utime.modnsec >= 1E9
|
2011-12-21 23:29:29 +01:00
|
|
|
* are caught by VFS to cooperate with old instances
|
|
|
|
*/
|
2014-04-29 20:08:54 +02:00
|
|
|
attr.a_mtime.tv_sec = m_in.m_vfs_fs_utime.modtime;
|
|
|
|
attr.a_mtime.tv_nsec = m_in.m_vfs_fs_utime.modnsec;
|
2011-12-21 23:29:29 +01:00
|
|
|
attr.a_mask |= SFFS_ATTR_MTIME;
|
|
|
|
break;
|
|
|
|
}
|
2012-04-09 18:08:26 +02:00
|
|
|
return sffs_table->t_setattr(path, &attr);
|
2010-01-26 00:18:02 +01:00
|
|
|
}
|