Add a "stat" command to test FileHandle::stat().

This commit is contained in:
Andreas Kling 2018-10-14 23:39:11 +02:00
parent c7c957966a
commit 0286b5ea48
6 changed files with 58 additions and 23 deletions

View file

@ -188,6 +188,8 @@ InodeMetadata Ext2FileSystem::inodeMetadata(InodeIdentifier inode) const
metadata.ctime = e2inode->i_ctime;
metadata.mtime = e2inode->i_mtime;
metadata.dtime = e2inode->i_dtime;
metadata.blockSize = blockSize();
metadata.blockCount = e2inode->i_blocks;
if (isBlockDevice(e2inode->i_mode) || isCharacterDevice(e2inode->i_mode)) {
unsigned dev = e2inode->i_block[0];

View file

@ -36,8 +36,9 @@ int FileHandle::stat(Unix::stat* buffer)
buffer->st_uid = metadata.uid;
buffer->st_gid = metadata.gid;
buffer->st_rdev = 0; // FIXME
buffer->st_blksize = 0; // FIXME
buffer->st_blocks = 0; // FIXME
buffer->st_size = metadata.size;
buffer->st_blksize = metadata.blockSize;
buffer->st_blocks = metadata.blockCount;
buffer->st_atime = metadata.atime;
buffer->st_mtime = metadata.mtime;
buffer->st_ctime = metadata.ctime;

View file

@ -1,17 +1,18 @@
#pragma once
#include "InodeIdentifier.h"
#include "UnixTypes.h"
inline bool isDirectory(word mode) { return (mode & 0170000) == 0040000; }
inline bool isCharacterDevice(word mode) { return (mode & 0170000) == 0020000; }
inline bool isBlockDevice(word mode) { return (mode & 0170000) == 0060000; }
inline bool isRegularFile(word mode) { return (mode & 0170000) == 0100000; }
inline bool isFIFO(word mode) { return (mode & 0170000) == 0010000; }
inline bool isSymbolicLink(word mode) { return (mode & 0170000) == 0120000; }
inline bool isSocket(word mode) { return (mode & 0170000) == 0140000; }
inline bool isSticky(word mode) { return mode & 01000; }
inline bool isSetUID(word mode) { return mode & 04000; }
inline bool isSetGID(word mode) { return mode & 02000; }
inline bool isDirectory(Unix::mode_t mode) { return (mode & 0170000) == 0040000; }
inline bool isCharacterDevice(Unix::mode_t mode) { return (mode & 0170000) == 0020000; }
inline bool isBlockDevice(Unix::mode_t mode) { return (mode & 0170000) == 0060000; }
inline bool isRegularFile(Unix::mode_t mode) { return (mode & 0170000) == 0100000; }
inline bool isFIFO(Unix::mode_t mode) { return (mode & 0170000) == 0010000; }
inline bool isSymbolicLink(Unix::mode_t mode) { return (mode & 0170000) == 0120000; }
inline bool isSocket(Unix::mode_t mode) { return (mode & 0170000) == 0140000; }
inline bool isSticky(Unix::mode_t mode) { return mode & 01000; }
inline bool isSetUID(Unix::mode_t mode) { return mode & 04000; }
inline bool isSetGID(Unix::mode_t mode) { return mode & 02000; }
struct InodeMetadata {
bool isValid() const { return inode.isValid(); }
@ -28,15 +29,17 @@ struct InodeMetadata {
bool isSetGID() const { return ::isSetGID(mode); }
InodeIdentifier inode;
dword size { 0 };
word mode { 0 };
dword uid { 0 };
dword gid { 0 };
dword linkCount { 0 };
time_t atime { 0 };
time_t ctime { 0 };
time_t mtime { 0 };
time_t dtime { 0 };
Unix::off_t size { 0 };
Unix::mode_t mode { 0 };
Unix::uid_t uid { 0 };
Unix::gid_t gid { 0 };
Unix::nlink_t linkCount { 0 };
Unix::time_t atime { 0 };
Unix::time_t ctime { 0 };
Unix::time_t mtime { 0 };
Unix::time_t dtime { 0 };
Unix::blkcnt_t blockCount { 0 };
Unix::blksize_t blockSize { 0 };
unsigned majorDevice { 0 };
unsigned minorDevice { 0 };
};

View file

@ -17,7 +17,7 @@ typedef dword gid_t;
typedef signed_qword off_t;
typedef dword blksize_t;
typedef dword blkcnt_t;
typedef dword time_t;
typedef long int time_t;
typedef dword size_t;
typedef signed_dword ssize_t;

View file

@ -263,7 +263,7 @@ void VirtualFileSystem::listDirectory(const String& path)
sprintf(buf, "%u, %u", metadata.majorDevice, metadata.minorDevice);
printf("%12s ", buf);
} else {
printf("%12u ", metadata.size);
printf("%12lld ", metadata.size);
}
printf("\033[30;1m");

View file

@ -132,6 +132,35 @@ int main(int c, char** v)
continue;
}
if (cmd == "stat" && parts.size() > 1) {
char buf[1024];
sprintf(buf, "%s/%s", currentDirectory.characters(), parts[1].characters());
auto handle = vfs.open(buf);
if (!handle) {
printf("Can't open '%s' :(\n", buf);
continue;
}
Unix::stat st;
int rc = handle->stat(&st);
if (rc < 0) {
printf("stat failed: %d\n", rc);
continue;
}
printf("st_dev: %u\n", st.st_dev);
printf("st_ino: %u\n", st.st_ino);
printf("st_mode: %u\n", st.st_mode);
printf("st_nlink: %u\n", st.st_nlink);
printf("st_uid: %u\n", st.st_uid);
printf("st_gid: %u\n", st.st_gid);
printf("st_rdev: %u\n", st.st_rdev);
printf("st_size: %lld\n", st.st_size);
printf("st_blksize: %u\n", st.st_blksize);
printf("st_blocks: %u\n", st.st_blocks);
printf("st_atime: %u - %s", st.st_atime, ctime(&st.st_atime));
printf("st_mtime: %u - %s", st.st_mtime, ctime(&st.st_mtime));
printf("st_ctime: %u - %s", st.st_ctime, ctime(&st.st_ctime));
continue;
}
if (cmd == "cat" && parts.size() > 1) {
char pathbuf[1024];