2beca41cc533cd0c414661da07bcf8cd11d26c05
[yaffs2.git] / direct / timothy_tests / EACCES_bug / error_handler.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Timothy Manning <timothy@yaffs.net>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 /*
15  * error_handler.c contains code for checking yaffs function calls for errors.
16  */
17 #include "error_handler.h"
18
19
20 typedef struct error_codes_template {
21   int code;
22   char * text;  
23 }error_entry;
24
25 const error_entry error_list[] = {
26         { ENOMEM , "ENOMEM" },
27         { EBUSY , "EBUSY"},
28         { ENODEV , "ENODEV"},
29         { EINVAL , "EINVAL"},
30         { EBADF , "EBADF"},
31         { EACCES , "EACCES"},
32         { EXDEV , "EXDEV" },
33         { ENOENT , "ENOENT"},
34         { ENOSPC , "ENOSPC"},
35         { ERANGE , "ERANGE"},
36         { ENODATA, "ENODATA"},
37         { ENOTEMPTY, "ENOTEMPTY"},
38         { ENAMETOOLONG,"ENAMETOOLONG"},
39         { ENOMEM , "ENOMEM"},
40         { EEXIST , "EEXIST"},
41         { ENOTDIR , "ENOTDIR"},
42         { EISDIR , "EISDIR"},
43         { 0, NULL }
44 };
45
46 const char * error_to_str(int err)
47 {
48         error_entry *e = error_list;
49         if (err < 0) 
50                 err = -err;
51         while(e->code && e->text){
52                 if(err == e->code)
53                         return e->text;
54                 e++;
55         }
56         return "Unknown error code";
57 }
58
59 void yaffs_check_for_errors(char output, buffer *message_buffer,char error_message[],char success_message[]){
60         char dummy[10];
61         unsigned int x=0;
62         int yaffs_error=-1;
63         char error_found=0;
64         if (output==-1)
65         {
66                 add_to_buffer(message_buffer, "\nerror##########",MESSAGE_LEVEL_ERROR,PRINT);
67                 add_to_buffer(message_buffer, error_message,MESSAGE_LEVEL_ERROR,PRINT);
68                 add_to_buffer(message_buffer, "error_code: ",MESSAGE_LEVEL_ERROR,PRINT);
69                 yaffs_error=yaffs_get_error();
70                 if (MESSAGE_LEVEL_ERROR<=DEBUG_LEVEL)   printf("%d\n",yaffs_error);     /*cannot yet add int types to buffer. this is a quick fix*/     
71                 add_to_buffer(message_buffer, error_to_str(yaffs_error),MESSAGE_LEVEL_ERROR,NPRINT);
72                 append_to_buffer(message_buffer, "\n\n",MESSAGE_LEVEL_ERROR,PRINT);     
73                 exit(1);                
74                 //scanf("%c",dummy);    /*this line causes a segmentation fault. Need a better way of waiting for a key press*/
75                 //print_buffer(message_buffer,PRINT_ALL);
76                 
77         }
78         else{
79                 add_to_buffer(message_buffer, success_message,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
80         }               
81 }
82
83