📄 write_files.c
字号:
/*
* +-------------------------------------------------------------------+
* | Copyright (c) 1995,2000 TriMedia Technologies Inc. |
* | |
* | This software is furnished under a license and may only be used |
* | and copied in accordance with the terms and conditions of such a |
* | license and with the inclusion of this copyright notice. This |
* | software or any other copies of this software may not be provided |
* | or otherwise made available to any other person. The ownership |
* | and title of this software is not transferred. |
* | |
* | The information in this software is subject to change without |
* | any prior notice and should not be construed as a commitment by |
* | TriMedia Technologies. |
* | |
* | This code and information is provided "as is" without any |
* | warranty of any kind, either expressed or implied, including but |
* | not limited to the implied warranties of merchantability and/or |
* | fitness for any particular purpose. |
* +-------------------------------------------------------------------+
*/
/*------------------------------ Includes ------------------------------------*/
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <dirent.h>
#include "unistd.h"
#include "errno.h"
#include "tmlib/tmFlash.h"
/*------------------------ Recursive directory printing ----------------------*/
typedef struct stat StatBuf;
static void print_file_info(String f)
{
StatBuf buf;
memset((Pointer)&buf,0,sizeof(buf));
if (stat(f,&buf)!=-1) {
printf(" * size= %4d dir=%d chr=%d reg=%d ino=%04d | %s\n",
buf.st_size, S_ISDIR(buf.st_mode),
S_ISCHR(buf.st_mode), S_ISREG(buf.st_mode), buf.st_ino, f
);
}
}
static void treewalk(String f)
{
DIR *dirp;
struct dirent *direntp;
dirp = opendir( f );
if (dirp) {
while ( (direntp = readdir( dirp )) != NULL ) {
Char buffer[1000];
sprintf(buffer,"%s/%s",f,direntp->d_name);
print_file_info(buffer);
if (strcmp(direntp->d_name,".") != 0
&& strcmp(direntp->d_name,"..") != 0) {
treewalk(buffer);
}
}
(void)closedir( dirp );
}
}
static void dump_flash() { treewalk("/flash"); }
/*------------------------ Recursive directory copying ----------------------*/
#define BLOCKSIZE 10000
static void copy_file(String path)
{
Int src = open(path+1, O_RDONLY | O_BINARY);
Int dest= open(path , O_WRONLY | O_BINARY | O_CREAT);
if (src == -1) {
printf("* F %s SOURCE FAILED\n", path);
} else
if (dest == -1) {
printf("* F %s DEST FAILED\n", path);
} else {
Int size;
Char buffer[BLOCKSIZE];
printf(" F %s \n", path);
do {
size= read (src, buffer, BLOCKSIZE);
write (dest, buffer, size);
} while (size > 0);
}
if (src != -1) { close(src ); }
if (dest != -1) { close(dest); }
}
static void copy_fs(String path)
{
DIR *dirp;
StatBuf buf;
struct dirent *direntp;
dirp = opendir( path+1 );
if (dirp) {
while ( (direntp = readdir( dirp )) != NULL ) {
if (strcmp(direntp->d_name,".") != 0
&& strcmp(direntp->d_name,"..") != 0) {
String buffer= (String)malloc( strlen(path)+strlen(direntp->d_name)+2);
sprintf(buffer,"%s/%s",path,direntp->d_name);
if (stat(buffer+1,&buf) == -1) printf("STAT ERROR \"%s\"\n", buffer);
if (S_ISDIR(buf.st_mode)) {
if (mkdir(buffer,0777) == 0) {
printf(" D %s\n", buffer);
copy_fs(buffer);
} else {
printf("* D %s FAILED\n", buffer);
}
} else {
copy_file(buffer);
}
free(buffer);
}
}
(void)closedir( dirp );
}
}
/*------------------------------ Event handler ------------------------------*/
static void event_handler(FlashEvent event);
static EventHandler old_event_handler= event_handler;
static void event_handler(FlashEvent event)
{
switch (event) {
case FlashFull:
old_event_handler(event);
exit(-1);
case FlashWriteError:
case FlashStaleSTTRemoved:
case FlashStaleRootRemoved:
case FlashStaleBootRemoved:
case FlashDangerousWriteError:
case FlashInterruptedMoveRepaired:
old_event_handler(event);
break;
}
}
/*------------------------------ Main program --------------------------------*/
static void dump_status()
{
printf("--------------------------------------------------\n");
dump_flash();
FlashUtil_print();
printf("--------------------------------------------------\n");
}
Int main()
{
old_event_handler= Flash_install_event_handler( event_handler );
atexit(dump_status);
copy_fs("/flash");
printf("-------------SUCCESSFULL TERMINATION ------------\n");
exit (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -