aboutsummaryrefslogtreecommitdiff
path: root/main/Unix/cDirectory.c
blob: 09b41bce68c23f2ed26e0a52b41c80e89e8ef389 (plain) (blame)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <dirent.h>
#include <unistd.h>
#include "Clean.h"

#ifndef NAME_MAX
#define NAME_MAX 500
#endif

#define FALSE 0
#define TRUE (!FALSE)

/* error codes: */
#define NoDirError           0
#define OtherDirError       -1
#define DoesntExist         -2
#define BadName             -3
#define NotEnoughSpace      -4
#define AlreadyExists       -5
#define NoPermission        -6
#define MoveIntoOffspring   -7
#define MoveAcrossDisks     -8
#define NotYetRemovable     -9

uid_t           gUserId;
gid_t           gGroupId;
struct DIR      *gpDir;
struct dirent   *gpDirent;
struct stat     gFileStat;
char            *gPath;
int             gPathLength;
CleanStringVariable(gFileName,NAME_MAX+1);

static int unix_error_to_clean_error(int errCode)
{
    switch(errCode) {
        case ENOENT:
        case ENOTDIR:       return DoesntExist;
        case ENAMETOOLONG:  return BadName;
        case EMLINK:
        case ENOSPC:        return NotEnoughSpace;
        case EISDIR:
        case EEXIST:        return AlreadyExists;
        case EPERM:
        case EACCES:
        case EROFS:         return NoPermission;
        case EINVAL:        return MoveIntoOffspring;
        case EXDEV:         return MoveAcrossDisks;
        case ENOTEMPTY:     return NotYetRemovable;
        default:            return OtherDirError;
        };
}

static int openSearch(const char *path, int length)
{
    int     i;
    
    gPathLength = length;
    gPath   = (char*) malloc(gPathLength+NAME_MAX+2);
    if (!gPath) {
        gpDir   = NULL;
        return 0;
        }
    else {
        memcpy(gPath, path, gPathLength);
        gPath[gPathLength]  = '\0';
        gpDir   = (struct DIR*) opendir(gPath);
        if (!gpDir)
            return unix_error_to_clean_error(errno);
        gPath[gPathLength]  = '/'; /* filename will be added later */
        gUserId = getuid();
        gGroupId    = getgid();
        return NoDirError;
        };
}

int findNextFileC (int);

int findFirstFileC(CleanString cs_path)
{
    int errCode;
    
    errCode = openSearch(CleanStringCharacters(cs_path), CleanStringLength(cs_path));
    if (errCode)
        return errCode;
    else
        return findNextFileC(0);
}

void getCommonFileInfoC(int also_get_file_name,
                        CleanString *pFileName, int *pFileSizeLow, int *pFileSizeHigh,
                        int *pYear, int *pMonth, int *pDay, int *pDayNr,
                        int *pHours, int *pMinutes, int *pSeconds,
                        int *pIsDirectory, int *pIsReadOnly)
/* requires gFileName and gFileStat to be initialized */
{
    struct tm   *pModificationTime;
    int         mask;
    static void *null = NULL; /* int 0 with the size of a Clean INT */

    *pFileName      = also_get_file_name ? (CleanString) gFileName : (CleanString) &null;
    *pFileSizeLow   = gFileStat.st_size;
    *pFileSizeHigh  = 0;
    pModificationTime= localtime(&gFileStat.st_mtime);
    *pYear          = pModificationTime->tm_year+1900;
    *pMonth         = pModificationTime->tm_mon+1;
    *pDay           = pModificationTime->tm_mday;
    *pDayNr         = pModificationTime->tm_wday+1;
    *pHours         = pModificationTime->tm_hour;
    *pMinutes       = pModificationTime->tm_min;
    *pSeconds       = pModificationTime->tm_sec % 60;
    *pIsDirectory   = (gFileStat.st_mode & S_IFDIR) != 0;
    mask            = gUserId==gFileStat.st_uid ? S_IWUSR
                        : gGroupId==gFileStat.st_gid ? S_IWGRP
                            : S_IWOTH;
    *pIsReadOnly    = (gFileStat.st_mode & mask) == 0;
}

int findNextFileC(int dummy)
/* return values: 0=ok, 1=no further files in directory */
{
    int i;

    gpDirent    = readdir((void*)gpDir);
    if (!gpDirent)
        return 1;

    for(i=0; gpDirent->d_name[i]!='\0' && i<NAME_MAX; i++) {
        CleanStringCharacters(gFileName)[i] = gpDirent->d_name[i];
        gPath[gPathLength+1+i]  = gpDirent->d_name[i];
        };
    CleanStringLength(gFileName)= i;
    gPath[gPathLength+1+i]      = '\0';
    gFileStat.st_size   = 0;
    gFileStat.st_mode   = 0;
    lstat(gPath,&gFileStat);
    return 0;
}

int getPlatformIdC(int dummy)
{
#define UnixPlatform 0 
    return UnixPlatform;
}

void getWindowsFileInfoC()
{}


void getMacFileInfoC()
{}

void getUnixFileInfoC(int *pModeBits, int *pOwnerUserId, int *pOwnerGroupId,
                        int *pLAYear, int *pLAMonth, int *pLADay, int *pLADayNr,    /* last access time */
                        int *pLAHours, int *pLAMinutes, int *pLASeconds)            /* dito */
{
    struct tm   *pModificationTime;

    *pModeBits      = gFileStat.st_mode;
    *pOwnerUserId   = gFileStat.st_uid;
    *pOwnerGroupId  = gFileStat.st_gid;
    pModificationTime= localtime(&gFileStat.st_atime);
    *pLAYear        = pModificationTime->tm_year+1900;
    *pLAMonth       = pModificationTime->tm_mon+1;
    *pLADay         = pModificationTime->tm_mday;
    *pLADayNr       = pModificationTime->tm_wday+1;
    *pLAHours       = pModificationTime->tm_hour;
    *pLAMinutes     = pModificationTime->tm_min;
    *pLASeconds     = pModificationTime->tm_sec % 60;
}


void closeSearchC()
{
    free(gPath);
    if (gpDir)
        closedir((void*)gpDir);
}

int findSingleFileC(CleanString cs_path)
{
    int err,i,length;
    char *path_chars;
    
    gFileStat.st_size   = 0;
    gFileStat.st_mode   = 0;

    path_chars = CleanStringCharacters(cs_path);
    if (lstat(path_chars,&gFileStat))
        return unix_error_to_clean_error(errno);
        
    i = CleanStringLength(cs_path)-2;
    while (i>=0 && path_chars[i]!='/')
        i--;
    /* the last path element ranges from path_chars[i+1] to path_chars[CleanStringLength(cs_path)-2] */

    length = CleanStringLength(cs_path)-2-i;
    CleanStringLength(gFileName) = length;
    memcpy(CleanStringCharacters(gFileName), path_chars+i+1, length);
        
    return NoDirError;
}

void closeSingleSearchC()
{}

int createDirectoryC(CleanString cs_path)
{
    int err;
    
    err = mkdir(CleanStringCharacters(cs_path),
                 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    /* access rights for newly created directory: rwx--x--x */
    if (err)
        return unix_error_to_clean_error(errno);
    else
        return NoDirError;
}

int fremoveC(CleanString cs_path)
{
    int err;
    
    err = remove(CleanStringCharacters(cs_path));
    if (err) {
        if (errno==EEXIST)
            return NotYetRemovable;
        else
            return unix_error_to_clean_error(errno);
        }
    else
        return NoDirError;
}

/* Try this code with slackware linux, if the testLinux program doesn't work
//int fremoveC(CleanString cs_path)
//{
//  int err;
//  
//  err = unlink(CleanStringCharacters(cs_path));
//  if (err) {
//      if (errno==EPERM) {
//          err = rmdir(CleanStringCharacters(cs_path));
//          if (err)
//              return unix_error_to_clean_error(errno);
//          else
//              return NoDirError;
//          }
//      else
//          return unix_error_to_clean_error(errno);
//      }
//  else
//      return NoDirError;
//}
*/

#define OK 0
#define STRING_TOO_SMALL 1

int getCurrentDirectory_SE(CleanString cs)
{
    if (getcwd(CleanStringCharacters(cs), CleanStringLength(cs))) {
        /* success. convert C String to Clean string */
        int i;
        i = 0;
        while(CleanStringCharacters(cs)[i]!='\0')
            i++;
        CleanStringLength(cs) = i;
        return OK;
        }
    else {
        /* failure */
        if (errno==EACCES) {
            /* the permission to read the current directory was denied (how ever)
               return root directory */
            CleanStringLength(cs) = 1;
            CleanStringCharacters(cs)[0] = '/';
            return OK;
            }
        else
            return STRING_TOO_SMALL;
        };
}       

void get_mac_dir_parent_and_name_C()
{
}

int setCurrentDirectoryC(CleanString csPath)
{
    int err;
    
    err = chdir(CleanStringCharacters(csPath));
    if (err)
        return unix_error_to_clean_error(errno);
    else
        return NoDirError;
}

void getMacDiskNameC()
{}

void get_windows_disk_available_bits_C()
{}

void macRenameC()
{}

void macMoveC()
{}

int fmoveC(int overwrite, CleanString from, CleanString to)
{
    int err,clean_err;
    
    if (overwrite) {
        err = rename(CleanStringCharacters(from), CleanStringCharacters(to));
        if (err && errno==ENOTDIR) {
            /* from is a directory and to a file
               try again after removing to */
            err = remove(CleanStringCharacters(to));
            if (!err)
                err = rename(CleanStringCharacters(from), CleanStringCharacters(to));
            };
        clean_err = err ? unix_error_to_clean_error(errno) : NoDirError;
        }
    else {
        struct stat fileStat;
        if (stat(CleanStringCharacters(to),&fileStat)) {
            err = rename(CleanStringCharacters(from), CleanStringCharacters(to));
            clean_err = err ? unix_error_to_clean_error(errno) : NoDirError;
            }
        else
            clean_err = AlreadyExists;
        };
    return clean_err;
}