summaryrefslogtreecommitdiffstats
path: root/uisimulator/x11/io.c
blob: 6a19dd07b14211362f3b0dfe0662471437f11e96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 Daniel Stenberg
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <dirent.h>

#include <fcntl.h>
#include "debug.h"

#define DIRFUNCTIONS_DEFINED /* prevent those prototypes */
#define dirent x11_dirent
#include "../../firmware/common/dir.h"
#undef dirent

#define SIMULATOR_ARCHOS_ROOT "archos"

struct mydir {
  DIR *dir;
  char *name;
};

typedef struct mydir MYDIR;

MYDIR *x11_opendir(char *name)
{
  char buffer[256]; /* sufficiently big */
  DIR *dir;

  if(name[0] == '/') {
    sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name);    
    dir=(DIR *)opendir(buffer);
  }
  else
    dir=(DIR *)opendir(name);

  if(dir) {
    MYDIR *my = (MYDIR *)malloc(sizeof(MYDIR));
    my->dir = dir;
    my->name = (char *)strdup(name);

    return my;
  }
  /* failed open, return NULL */
  return (MYDIR *)0;
}

struct x11_dirent *x11_readdir(MYDIR *dir)
{
  char buffer[512]; /* sufficiently big */
  static struct x11_dirent secret;
  struct stat s;
  struct dirent *x11 = (readdir)(dir->dir);

  if(!x11)
    return (struct x11_dirent *)0;

  strcpy(secret.d_name, x11->d_name);

  /* build file name */
  sprintf(buffer, SIMULATOR_ARCHOS_ROOT "%s/%s",
          dir->name, x11->d_name);
  stat(buffer, &s); /* get info */

  secret.attribute = S_ISDIR(s.st_mode)?ATTR_DIRECTORY:0;
  secret.size = s.st_size;

  return &secret;
}

void x11_closedir(MYDIR *dir)
{
  free(dir->name);
  (closedir)(dir->dir);

  free(dir);
}


int x11_open(char *name, int opts)
{
  char buffer[256]; /* sufficiently big */

  if(name[0] == '/') {
    sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name);

    debugf("We open the real file '%s'\n", buffer);
    return open(buffer, opts);
  }
  return open(name, opts);
}

int x11_creat(char *name, int mode)
{
  char buffer[256]; /* sufficiently big */

  if(name[0] == '/') {
    sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name);

    debugf("We create the real file '%s'\n", buffer);
    return creat(buffer, mode);
  }
  return creat(name, mode);
}

int x11_remove(char *name)
{
  char buffer[256]; /* sufficiently big */

  if(name[0] == '/') {
    sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name);

    debugf("We remove the real file '%s'\n", buffer);
    return remove(buffer);
  }
  return remove(name);
}

int x11_rename(char *oldpath, char* newpath)
{
  char buffer1[256];
  char buffer2[256];

  if(oldpath[0] == '/') {
      sprintf(buffer1, "%s%s", SIMULATOR_ARCHOS_ROOT, oldpath);
      sprintf(buffer2, "%s%s", SIMULATOR_ARCHOS_ROOT, newpath);

      debugf("We rename the real file '%s' to '%s'\n", buffer1, buffer2);
      return rename(buffer1, buffer2);
  }
  return -1;
}

void fat_size(unsigned int* size, unsigned int* free)
{
    struct statfs fs;

    if (!statfs(".", &fs)) {
        DEBUGF("statfs: bsize=%d blocks=%d free=%d\n",
               fs.f_bsize, fs.f_blocks, fs.f_bfree);
        if (size)
            *size = fs.f_blocks * (fs.f_bsize / 1024);
        if (free)
            *free = fs.f_bfree * (fs.f_bsize / 1024);
    }
    else {
        if (size)
            *size = 0;
        if (free)
            *free = 0;
    }
}