source: umon/main/common/tfslog.c @ 87db514

Last change on this file since 87db514 was 87db514, checked in by Amar Takhar <amar@…>, on 04/16/15 at 19:26:21

Initial commit of the umon repository.

Prior to this three changes were made:

  • Remove umon_ prefix from parent directories.
  • Collapse main/target/ into main/
  • Remove ports/template/flashtest.scr.ucon script.
  • Property mode set to 100644
File size: 4.7 KB
Line 
1/**************************************************************************
2 *
3 * Copyright (c) 2013 Alcatel-Lucent
4 *
5 * Alcatel Lucent licenses this file to You under the Apache License,
6 * Version 2.0 (the "License"); you may not use this file except in
7 * compliance with the License.  A copy of the License is contained the
8 * file LICENSE at the top level of this repository.
9 * You may also obtain a copy of the License at:
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 **************************************************************************
20 *
21 * tfslog.c:
22 *
23 * Optional facility for TFS that supports the ability to log all
24 * tfs actions that affect flash, to a log file.
25 *
26 * This function is called by tfsadd(), tfsunlink() and tfsipmod() to
27 * write to a log file indicating that something in tfs has been changed.
28 * Note that the function can be called at any user level, but it must be
29 * able to modify the TFS_CHANGELOG_FILE that has "u3" flags.  The
30 * user level must be temporarily forced to MAXUSRLEVEL for this.
31 *
32 * Original author:     Ed Sutter (ed.sutter@alcatel-lucent.com)
33 *
34 */
35#include "config.h"
36#include "stddefs.h"
37#include "genlib.h"
38#include "tfs.h"
39#include "tfsprivate.h"
40#include "cli.h"
41
42#if TFS_CHANGELOG_SIZE
43static int      tfsLogging;
44#endif
45
46void
47tfslog(int action, char *string)
48{
49#if TFS_CHANGELOG_SIZE
50        static char *tfslogaction[] = { "ADD", "DEL", "IPM", " ON", "OFF" };
51       
52        extern  void *setTmpMaxUsrLvl();
53        static  char buf[TFS_CHANGELOG_SIZE];
54        TFILE   *tfp;
55        int             (*fptr)();
56        char    *eol, *eob, *logaction, tbuf[32];
57        int             newfsize, fsize, lsize, tfd, err, len, tbuflen;
58
59        switch(action) {
60                case TFSLOG_ADD:                /* Return here if logging is off,       */
61                case TFSLOG_DEL:                /* or this tfslog() call is on the      */
62                case TFSLOG_IPM:                /* TFS_CHANGELOG_FILE itself.           */
63                        if (!tfsLogging || !strcmp(string,TFS_CHANGELOG_FILE))
64                                return;
65                        break;
66                case TFSLOG_ON:
67                        if (tfsLogging == 1)
68                                return;
69                        tfsLogging = 1;
70                        break;
71                case TFSLOG_OFF:
72                        if (tfsLogging == 0)
73                                return;
74                        tfsLogging = 0;
75                        break;
76        }
77
78        /* Force the getUsrLvl() function to return MAX: */
79        fptr = (int(*)())setTmpMaxUsrLvl();
80
81        logaction = tfslogaction[action];
82        tfp = tfsstat(TFS_CHANGELOG_FILE);
83        tfsGetAtime(0,tbuf,sizeof(tbuf));
84        tbuflen = strlen(tbuf);
85
86        if (tfp) {
87                tfd = tfsopen(TFS_CHANGELOG_FILE,TFS_RDONLY,0);
88                fsize = tfsread(tfd,buf,TFS_CHANGELOG_SIZE);
89                tfsclose(tfd,0);
90
91                newfsize = (fsize+strlen(logaction)+strlen(string)+3);
92                if (tbuflen)
93                        newfsize += tbuflen + 3;
94
95                eob = buf + fsize;
96
97                /* If newfsize is greater than the maximum size the file is
98                 * allowed to grow, then keep removing the first line
99                 * (oldest entry) until new size is within the limit...
100                 */
101                if (newfsize > TFS_CHANGELOG_SIZE) {
102                        lsize = 0;
103                        eol = buf;
104                        while ((newfsize-lsize) > TFS_CHANGELOG_SIZE) {
105                                while((*eol != '\r') && (*eol != '\n')) eol++;
106                                while((*eol == '\r') || (*eol == '\n')) eol++;
107                                lsize = eol-buf;
108                        }
109                        fsize -= lsize;
110                        newfsize -= lsize;
111                        eob -= lsize;
112                        memcpy(buf,eol,fsize);
113                }
114                if (tbuflen)
115                        sprintf(eob,"%s: %s @ %s\n",logaction,string,tbuf);
116                else
117                        sprintf(eob,"%s: %s\n",logaction,string);
118                err = _tfsunlink(TFS_CHANGELOG_FILE);
119                if (err < 0)
120                        printf("%s: %s\n",TFS_CHANGELOG_FILE,
121                                (char *)tfsctrl(TFS_ERRMSG,err,0));
122                err = tfsadd(TFS_CHANGELOG_FILE,0,"u3",buf,newfsize);
123                if (err < 0)
124                        printf("%s: %s\n",TFS_CHANGELOG_FILE,
125                                (char *)tfsctrl(TFS_ERRMSG,err,0));
126        }
127        else {
128                if (tbuflen)
129                        len = sprintf(buf,"%s: %s @ %s\n",logaction,string,tbuf);
130                else
131                        len = sprintf(buf,"%s: %s\n",logaction,string);
132                err = tfsadd(TFS_CHANGELOG_FILE,0,"u3",buf,len);
133                if (err < 0)
134                        printf("%s: %s\n",TFS_CHANGELOG_FILE,
135                                (char *)tfsctrl(TFS_ERRMSG,err,0));
136        }
137
138        /* Restore the original getUsrLvl() functionality: */
139        clrTmpMaxUsrLvl(fptr);
140#endif
141}
142
143int
144tfsLogCmd(int argc,char *argv[], int optind)
145{
146#if TFS_CHANGELOG_SIZE
147        int     status;
148        int     retval = 0;
149
150        if (getUsrLvl() < MAXUSRLEVEL) {
151                status = showTfsError(TFSERR_USERDENIED,0);
152        }
153        else {
154                if (argc == optind + 3) {
155                        if (!strcmp(argv[optind+1],"on"))
156                                tfslog(TFSLOG_ON,argv[optind+2]);
157                        else if (!strcmp(argv[optind+1],"off"))
158                                tfslog(TFSLOG_OFF,argv[optind+2]);
159                        else
160                                retval = CMD_PARAM_ERROR;
161                }
162                else if (argc == optind + 1)
163                        printf("TFS logging %sabled\n",tfsLogging ? "en" : "dis");
164                else
165                        retval = CMD_PARAM_ERROR;
166        }
167        return(retval);
168#else
169        return(CMD_FAILURE);
170#endif
171}
Note: See TracBrowser for help on using the repository browser.