📄 helper_cmds.c
字号:
/*
Copyright (C) 2005-2008 Ricky Zheng <ricky_gz_zheng@yahoo.co.nz>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* \file helper_cmds.c
* \brief helper commands for test uffs
* \author Ricky Zheng
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "uffs/uffs_config.h"
#include "uffs/uffs_public.h"
#include "uffs/uffs_fs.h"
#include "uffs/uffs_utils.h"
#include "uffs/uffs_core.h"
#include "cmdline.h"
#define PFX "cmd: "
BOOL cmdFormat(const char *tail)
{
URET ret;
const char *mount = "/";
uffs_Device *dev;
if (tail) {
mount = cli_getparam(tail, NULL);
}
uffs_Perror(UFFS_ERR_NORMAL, PFX"Formating %s ... ", mount);
dev = uffs_GetDevice(mount);
if (dev == NULL) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Can't get device from mount point.\n");
}
else {
ret = uffs_FormatDevice(dev);
if (ret != U_SUCC) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Format fail.\n");
}
else {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Format succ.\n");
}
}
return TRUE;
}
BOOL cmdMkf(const char *tail)
{
uffs_Object *f;
const char *name;
URET ret;
int oflags = UO_RDWR | UO_CREATE;
if (tail == NULL) {
return FALSE;
}
name = cli_getparam(tail, NULL);
f = uffs_GetObject();
if (f == NULL) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Fail to get object.\n");
return TRUE;
}
ret = uffs_CreateObject(f, name, oflags, US_IREAD|US_IWRITE);
if (ret == U_FAIL) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Create %s fail, err: %d\n", name, f->err);
uffs_PutObject(f);
return TRUE;
}
uffs_Perror(UFFS_ERR_NORMAL, PFX"Create %s succ.\n", name);
uffs_CloseObject(f);
uffs_PutObject(f);
return TRUE;
}
BOOL cmdMkdir(const char *tail)
{
uffs_Object *f;
const char *name;
URET ret;
int oflags = UO_RDWR | UO_CREATE | UO_DIR;
if (tail == NULL) {
return FALSE;
}
name = cli_getparam(tail, NULL);
f = uffs_GetObject();
if (f == NULL) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Fail to get object.\n");
return TRUE;
}
ret = uffs_CreateObject(f, name, oflags, US_IREAD|US_IWRITE);
if (ret == U_FAIL) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Create %s fail, err: %d\n", name, f->err);
uffs_PutObject(f);
return TRUE;
}
uffs_Perror(UFFS_ERR_NORMAL, PFX"Create %s succ.\n", name);
uffs_CloseObject(f);
uffs_PutObject(f);
return TRUE;
}
static int CountObjectUnder(const char *dir)
{
uffs_FindInfo find = {0};
int count = 0;
URET ret;
ret = uffs_OpenFindObject(&find, dir);
if (ret == U_SUCC) {
ret = uffs_FindFirstObject(NULL, &find);
while(ret == U_SUCC) {
count++;
ret = uffs_FindNextObject(NULL, &find);
}
uffs_CloseFindObject(&find);
}
return count;
}
BOOL cmdPwd(const char *tail)
{
uffs_Perror(UFFS_ERR_NORMAL, PFX"not supported since v1.2.0.\n");
return TRUE;
}
BOOL cmdCd(const char *tail)
{
uffs_Perror(UFFS_ERR_NORMAL, PFX"Not supported since v1.2.0\n");
return TRUE;
}
BOOL cmdLs(const char *tail)
{
uffs_FindInfo find = {0};
uffs_ObjectInfo info;
URET ret;
int count = 0;
char buf[MAX_PATH_LENGTH+2];
char *name = (char *)tail;
char *sub;
if (name == NULL) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Must provide file/dir name.\n");
return FALSE;
}
ret = uffs_OpenFindObject(&find, name);
if (ret == U_FAIL) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Can't open '%s'\n", name);
return TRUE;
}
uffs_Perror(UFFS_ERR_NORMAL, "------name----size----serial--\n");
ret = uffs_FindFirstObject(&info, &find);
while(ret == U_SUCC) {
uffs_Perror(UFFS_ERR_NORMAL, "%9s", info.info.name);
if (info.info.attr & FILE_ATTR_DIR) {
strcpy(buf, name);
sub = buf;
if (name[strlen(name)-1] != '/') sub = strcat(buf, "/");
sub = strcat(sub, info.info.name);
sub = strcat(sub, "/");
uffs_Perror(UFFS_ERR_NORMAL, "/ \t<%2d>\t", CountObjectUnder(sub));
}
else {
uffs_Perror(UFFS_ERR_NORMAL, " \t %2d \t", info.len);
}
uffs_Perror(UFFS_ERR_NORMAL, "%d\n", info.serial);
count++;
ret = uffs_FindNextObject(&info, &find);
}
uffs_CloseFindObject(&find);
uffs_Perror(UFFS_ERR_NORMAL, "Total: %d objects.\n", count);
return TRUE;
}
BOOL cmdRm(const char *tail)
{
const char *name = NULL;
if (tail == NULL) return FALSE;
name = cli_getparam(tail, NULL);
if (uffs_DeleteObject(name) == U_SUCC) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Delete '%s' succ.\n", name);
}
else {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Delete '%s' fail!\n", name);
}
return TRUE;
}
BOOL cmdRen(const char *tail)
{
const char *oldname;
char *newname;
if (tail == NULL) return FALSE;
oldname = cli_getparam(tail, &newname);
if (newname == NULL) return FALSE;
if (uffs_RenameObject(oldname, newname) == U_SUCC) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Rename from '%s' to '%s' succ.\n", oldname, newname);
}
else {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Rename from '%s' to '%s' fail!\n", oldname, newname);
}
return TRUE;
}
BOOL cmdSt(const char *tail)
{
uffs_Device *dev;
const char *mount = "/";
uffs_stat *s;
TreeNode *node;
if (tail) {
mount = cli_getparam(tail, NULL);
}
dev = uffs_GetDevice(mount);
if (dev == NULL) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Can't get device from mount point %s\n", mount);
return TRUE;
}
s = &(dev->st);
uffs_Perror(UFFS_ERR_NORMAL, PFX"-----------statistics-----------\n");
uffs_Perror(UFFS_ERR_NORMAL, PFX"Block Erased: %d\n", s->blockEraseCount);
uffs_Perror(UFFS_ERR_NORMAL, PFX"Write Page: %d\n", s->pageWriteCount);
uffs_Perror(UFFS_ERR_NORMAL, PFX"Write Spare: %d\n", s->spareWriteCount);
uffs_Perror(UFFS_ERR_NORMAL, PFX"Read Page: %d\n", s->pageReadCount);
uffs_Perror(UFFS_ERR_NORMAL, PFX"Read Spare: %d\n", s->spareReadCount);
uffs_Perror(UFFS_ERR_NORMAL, PFX"Disk total: %d\n", uffs_GetDeviceTotal(dev));
uffs_Perror(UFFS_ERR_NORMAL, PFX"Disk Used: %d\n", uffs_GetDeviceUsed(dev));
uffs_Perror(UFFS_ERR_NORMAL, PFX"Disk Free: %d\n", uffs_GetDeviceFree(dev));
uffs_Perror(UFFS_ERR_NORMAL, PFX"Block size: %d\n", dev->attr->block_data_size);
uffs_Perror(UFFS_ERR_NORMAL, PFX"Total blocks: %d of %d\n", (dev->par.end - dev->par.start + 1), dev->attr->total_blocks);
if (dev->tree.bad) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Bad blocks: ");
node = dev->tree.bad;
while(node) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"%d, ", node->u.list.block);
node = node->u.list.next;
}
uffs_Perror(UFFS_ERR_NORMAL, PFX"\n");
}
uffs_BufInspect(dev);
uffs_PutDevice(dev);
return TRUE;
}
BOOL cmdCp(const char *tail)
{
const char *src;
char *des;
char buf[100];
uffs_Object *f1 = NULL, *f2 = NULL;
int len;
BOOL src_local = FALSE, des_local = FALSE;
FILE *fp1 = NULL, *fp2 = NULL;
if (!tail) return FALSE;
src = cli_getparam(tail, &des);
if (!des) return FALSE;
if (memcmp(src, "::", 2) == 0) {
src += 2;
src_local = TRUE;
}
if (memcmp(des, "::", 2) == 0) {
des += 2;
des_local = TRUE;
}
f1 = uffs_GetObject();
f2 = uffs_GetObject();
if (!f1 || !f2) goto fail_2;
if (src_local) {
if ((fp1 = fopen(src, "rb")) == NULL) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Can't open %s for copy.\n", src);
goto fail_1;
}
}
else {
if (uffs_OpenObject(f1, src, UO_RDONLY, US_IREAD) != U_SUCC) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Can't open %s for copy.\n", src);
goto fail_1;
}
}
if (des_local) {
if ((fp2 = fopen(des, "wb")) == NULL) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Can't open %s for copy.\n", des);
goto fail_1;
}
}
else {
if (uffs_OpenObject(f2, des, UO_RDWR|UO_CREATE|UO_TRUNC, US_IREAD|US_IWRITE) != U_SUCC) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"Can't open %s for copy.\n", des);
goto fail_1;
}
}
while ( (src_local ? (feof(fp1) == 0) : (uffs_EndOfFile(f1) == 0)) ) {
if (src_local) {
len = fread(buf, 1, sizeof(buf), fp1);
}
else {
len = uffs_ReadObject(f1, buf, sizeof(buf));
}
if (len == 0) break;
if (len < 0) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"read file %s fail ?\n", src);
break;
}
if (des_local) {
if ((int)fwrite(buf, 1, len, fp2) != len) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"write file %s fail ? \n", des);
break;
}
}
else {
if (uffs_WriteObject(f2, buf, len) != len) {
uffs_Perror(UFFS_ERR_NORMAL, PFX"write file %s fail ? \n", des);
break;
}
}
}
fail_1:
uffs_CloseObject(f1);
uffs_CloseObject(f2);
if (fp1) fclose(fp1);
if (fp2) fclose(fp2);
fail_2:
if (f1) {
uffs_PutObject(f1);
}
if (f2) {
uffs_PutObject(f2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -