dfs_win32.c 14.6 KB
Newer Older
M
Ming, Bai 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * File      : rtthread.h
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006-2012, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-11-27     prife        the first version
13
 * 2013-03-03     aozima       add dfs_win32_stat st_mtime support.
Y
yygg_you 已提交
14
 * 2017-10-20     urey         support rt-thread 3.0
M
Ming, Bai 已提交
15 16
 */
#include <rtthread.h>
Y
yygg_you 已提交
17
#include <rtlibc.h>
M
Ming, Bai 已提交
18 19

#include <dfs_fs.h>
Y
yygg_you 已提交
20
#include <dfs_file.h>
M
Ming, Bai 已提交
21 22 23 24 25 26 27 28 29 30
#include <rtdevice.h>

#include <io.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
#include <WinError.h>
#include  <windows.h>

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#if defined(__MINGW32__) && defined(_NO_OLDNAMES)
#define O_RDONLY    _O_RDONLY
#define O_WRONLY    _O_WRONLY
#define O_RDWR      _O_RDWR
#define O_ACCMODE   _O_ACCMODE
#define O_APPEND    _O_APPEND
#define O_CREAT     _O_CREAT
#define O_TRUNC     _O_TRUNC
#define O_EXCL      _O_EXCL
#define O_TEXT      _O_TEXT
#define O_BINARY    _O_BINARY
#define O_TEMPORARY _O_TEMPORARY
#define O_NOINHERIT _O_NOINHERIT
#define O_SEQUENTIAL   _O_SEQUENTIAL
#define O_RANDOM    _O_RANDOM
#endif
M
Ming, Bai 已提交
47 48 49 50 51
/*
 * RT-Thread DFS Interface for win-directory as an disk device
 */
#define FILE_PATH_MAX           256  /* the longest file path */

Y
yygg_you 已提交
52
#define WIN32_DIRDISK_ROOT  "./disk" /* "F:\\Project\\svn\\rtt\\trunk\\bsp\\simulator_test" */
M
Ming, Bai 已提交
53

P
prife 已提交
54 55 56 57 58 59 60 61
typedef struct {
    HANDLE handle;
    char *start;
    char *end;
    char *curr;
    struct _finddata_t finddata;
} WINDIR;

M
Ming, Bai 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74
/* There are so many error codes in windows, you'd better google for details.
 * google  "System Error Codes (Windows)"
 * http://msdn.microsoft.com/ZH-CN/library/windows/desktop/ms681381(v=vs.85).aspx
 */

struct _errcode_map
{
    rt_uint16_t dfserr;
    rt_uint16_t win32err;
};

static const struct _errcode_map errcode_table[] =
{
Y
yygg_you 已提交
75 76 77 78 79 80 81
    {ENOENT, ERROR_FILE_NOT_FOUND },
    {ENOENT, ERROR_PATH_NOT_FOUND },
    {EEXIST, ERROR_FILE_EXISTS },
    {EEXIST, ERROR_ALREADY_EXISTS },
    {ENOTEMPTY, ERROR_DIR_NOT_EMPTY },
    {EBUSY, ERROR_PATH_BUSY },
    {EINVAL, ERROR_ACCESS_DENIED },
M
Ming, Bai 已提交
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

#if 0 /* TODO: MORE NEED BE ADDED */
    {DFS_STATUS_EISDIR, ERROR_FILE_EXISTS },
    {DFS_STATUS_ENOTDIR, ERROR_FILE_EXISTS },
    {DFS_STATUS_EBADF, ERROR_FILE_EXISTS },
    {DFS_STATUS_EBUSY, ERROR_FILE_EXISTS },
    {DFS_STATUS_ENOMEM, ERROR_FILE_EXISTS },
    {DFS_STATUS_ENOSPC, ERROR_FILE_EXISTS },
#endif
};
static int win32_result_to_dfs(DWORD res)
{
    int i;
    int err = 0;
    for (i = 0; i < sizeof(errcode_table) / sizeof(struct _errcode_map); i++)
    {
        if (errcode_table[i].win32err == (res & 0xFFFF))
        {
            err = -errcode_table[i].dfserr;
            return err;
        }
    }

    /* unknown error */
    rt_kprintf("dfs win32 error not supported yet: %d\n", res);
    return -1;
}

static int dfs_win32_mount(
    struct dfs_filesystem *fs,
    unsigned long rwflag,
    const void *data)
{
    return 0;
}

static int dfs_win32_unmount(struct dfs_filesystem *fs)
{
    return 0;
}

P
prife 已提交
123
static int dfs_win32_mkfs(rt_device_t devid)
M
Ming, Bai 已提交
124
{
Y
yygg_you 已提交
125
    return -ENOSYS;
M
Ming, Bai 已提交
126 127 128 129 130
}

static int dfs_win32_statfs(struct dfs_filesystem *fs,
                            struct statfs *buf)
{
Y
yygg_you 已提交
131
    return -ENOSYS;
M
Ming, Bai 已提交
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
}

static char *winpath_dirdup(char *des, const char *src)
{
    char *path;
    int i = 0;

    path = rt_malloc(FILE_PATH_MAX);
    if (path == RT_NULL)
        return RT_NULL;

    strcpy(path, des);
    strcat(path, src);

    while (1)
    {
        if (path[i] == 0)
            break;

        if (path[i] == '/')
            path[i] = '\\';

        i++;
    }

    return path;
}

160 161 162 163 164 165 166 167
/* This function can convert the path in rt-thread/dfs to the path in windows */
char * dfs_win32_dirdup(char * path)
{
    char * file_path;
    file_path = winpath_dirdup(WIN32_DIRDISK_ROOT, path);
    return file_path;
}

M
Ming, Bai 已提交
168 169 170
static int dfs_win32_open(struct dfs_fd *file)
{
    int fd;
Y
yygg_you 已提交
171
    uint32_t oflag, mode;
M
Ming, Bai 已提交
172 173 174 175
    char *file_path;
    int res;

    oflag = file->flags;
Y
yygg_you 已提交
176
    if (oflag & O_DIRECTORY)   /* operations about dir */
M
Ming, Bai 已提交
177
    {
P
prife 已提交
178 179
        WINDIR *wdirp;
        HANDLE handle;
M
Ming, Bai 已提交
180 181 182 183
        int len;

        file_path = winpath_dirdup(WIN32_DIRDISK_ROOT, file->path);

Y
yygg_you 已提交
184
        if (oflag & O_CREAT)   /* create a dir*/
M
Ming, Bai 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        {
            res = CreateDirectory(file_path, NULL);
            if (res == 0)
            {
                rt_free(file_path);
                return win32_result_to_dfs(GetLastError());
            }
        }

        len = strlen(file_path);
        if (file_path[len - 1] != '\\')
        {
            file_path[len] = '\\';
            file_path[len + 1] = 0;
        }

        strcat(file_path, "*.*");
        /* _findfirst will get '.' */
P
prife 已提交
203 204 205 206
        /* save this pointer,will used by  dfs_win32_getdents*/
        wdirp = rt_malloc(sizeof(WINDIR));
        RT_ASSERT(wdirp != NULL);
        if ((handle = _findfirst(file_path, &wdirp->finddata)) == -1)
M
Ming, Bai 已提交
207
        {
P
prife 已提交
208
            rt_free(wdirp);
M
Ming, Bai 已提交
209 210 211
            rt_free(file_path);
            goto __err;
        }
P
prife 已提交
212 213 214 215 216 217 218 219 220
        len = strlen(wdirp->finddata.name) + 1;
        wdirp->handle = handle;
        //wdirp->nfiles = 1;
        wdirp->start = malloc(len); //not rt_malloc!
        wdirp->end = wdirp->curr = wdirp->start;
        wdirp->end += len;
        strncpy(wdirp->curr, wdirp->finddata.name, len);

        file->data = (void *)wdirp;
M
Ming, Bai 已提交
221
        rt_free(file_path);
Y
yygg_you 已提交
222
        return 0;
M
Ming, Bai 已提交
223 224 225
    }
    /* regular file operations */
    mode = O_BINARY;
Y
yygg_you 已提交
226 227 228
    if (oflag & O_RDONLY) mode |= O_RDONLY;
    if (oflag & O_WRONLY) mode |= O_WRONLY;
    if (oflag & O_RDWR)   mode |= O_RDWR;
M
Ming, Bai 已提交
229
    /* Opens the file, if it is existing. If not, a new file is created. */
Y
yygg_you 已提交
230
    if (oflag & O_CREAT) mode |= O_CREAT;
M
Ming, Bai 已提交
231
    /* Creates a new file. If the file is existing, it is truncated and overwritten. */
Y
yygg_you 已提交
232
    if (oflag & O_TRUNC) mode |= O_TRUNC;
M
Ming, Bai 已提交
233
    /* Creates a new file. The function fails if the file is already existing. */
Y
yygg_you 已提交
234
    if (oflag & O_EXCL) mode |= O_EXCL;
M
Ming, Bai 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248

    file_path = winpath_dirdup(WIN32_DIRDISK_ROOT, file->path);
    fd = _open(file_path, mode, 0x0100 | 0x0080); /* _S_IREAD | _S_IWRITE */
    rt_free(file_path);

    if (fd < 0)
        goto __err;

    /* save this pointer, it will be used when calling read(), write(),
     * flush(), seek(), and will be free when calling close()*/
    file->data = (void *)fd;
    file->pos  = 0;
    file->size = _lseek(fd, 0, SEEK_END);

Y
yygg_you 已提交
249
    if (oflag & O_APPEND)
M
Ming, Bai 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    {
        file->pos = file->size;
    }
    else
        _lseek(fd, 0, SEEK_SET);

    return 0;

__err:
    res = GetLastError();
    return win32_result_to_dfs(res);
}

static int dfs_win32_close(struct dfs_fd *file)
{
Y
yygg_you 已提交
265
    if (file->flags & O_DIRECTORY)
M
Ming, Bai 已提交
266
    {
P
prife 已提交
267 268 269 270 271 272 273 274 275 276 277 278
        WINDIR *wdirp = (WINDIR*)(file->data);
        RT_ASSERT(wdirp != RT_NULL);
        if (_findclose((intptr_t)wdirp->handle) == 0) {
            free(wdirp->start); //NOTE: here we don't use rt_free!
            rt_free(wdirp);
            return 0;
        }
    }
    else /* regular file operations */
    {
        if (_close((int)(file->data)) == 0)
            return 0;
M
Ming, Bai 已提交
279 280 281 282 283 284 285
    }

    return win32_result_to_dfs(GetLastError());
}

static int dfs_win32_ioctl(struct dfs_fd *file, int cmd, void *args)
{
Y
yygg_you 已提交
286
    return -ENOSYS;
M
Ming, Bai 已提交
287 288
}

Y
yygg_you 已提交
289
static int dfs_win32_read(struct dfs_fd *file, void *buf, size_t len)
M
Ming, Bai 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303
{
    int fd;
    int char_read;

    fd = (int)(file->data);
    char_read = _read(fd, buf, len);
    if (char_read < 0)
        return win32_result_to_dfs(GetLastError());

    /* update position */
    file->pos = _lseek(fd, 0, SEEK_CUR);
    return char_read;
}

Y
yygg_you 已提交
304
static int dfs_win32_write(struct dfs_fd *file, const void *buf, size_t len)
M
Ming, Bai 已提交
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
{
    int fd;
    int char_write;

    fd = (int)(file->data);

    char_write = _write(fd, buf, len);
    if (char_write < 0)
        return win32_result_to_dfs(GetLastError());

    /* update position */
    file->pos = _lseek(fd, 0, SEEK_CUR);
    return char_write;
}

static int dfs_win32_flush(struct dfs_fd *file)
{
    return 0;
}

static int dfs_win32_seek(struct dfs_fd *file,
                          rt_off_t offset)
{
    int result;

    /* set offset as current offset */
    if (file->type == FT_DIRECTORY)
    {
P
prife 已提交
333 334 335 336
        WINDIR* wdirp = (WINDIR*)(file->data);
        RT_ASSERT(wdirp != RT_NULL);
        wdirp->curr = wdirp->start + offset;
        return offset;
M
Ming, Bai 已提交
337
    }
P
prife 已提交
338
    else //file->type == FT_REGULAR)
M
Ming, Bai 已提交
339 340 341 342
    {
        result = _lseek((int)(file->data), offset, SEEK_SET);
        if (result >= 0)
            return offset;
P
prife 已提交
343 344
        else
            return win32_result_to_dfs(GetLastError());
M
Ming, Bai 已提交
345 346 347 348
    }
}

/* return the size of struct dirent*/
Y
yygg_you 已提交
349
static int dfs_win32_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
M
Ming, Bai 已提交
350
{
P
prife 已提交
351 352
    WINDIR *wdirp;
    struct dirent *d = dirp;
M
Ming, Bai 已提交
353 354
    int result;

P
prife 已提交
355 356
    /* make integer count */
    if (count / sizeof(struct dirent) != 1)
Y
yygg_you 已提交
357
        return -EINVAL;
M
Ming, Bai 已提交
358

P
prife 已提交
359 360 361 362
    wdirp = (WINDIR*)(file->data);
    RT_ASSERT(wdirp != RT_NULL);
    if (wdirp->curr == NULL) //no more entries in this directory
        return 0;
M
Ming, Bai 已提交
363

P
prife 已提交
364 365
    /* get the current entry */
    if (wdirp->finddata.attrib & _A_SUBDIR)
Y
yygg_you 已提交
366
        d->d_type = DT_DIR;
P
prife 已提交
367
    else
Y
yygg_you 已提交
368
        d->d_type = DT_REG;
P
prife 已提交
369 370 371 372 373 374 375 376
    d->d_namlen = strlen(wdirp->curr);
    strncpy(d->d_name, wdirp->curr, DFS_PATH_MAX);
    d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
    wdirp->curr += (strlen(wdirp->curr) + 1);
    file->pos = wdirp->curr - wdirp->start + sizeof(struct dirent);//NOTE!

    /* now set up for the next call to readdir */
    if (wdirp->curr >= wdirp->end)
M
Ming, Bai 已提交
377
    {
P
prife 已提交
378 379 380 381 382 383 384 385 386
        if (_findnext(wdirp->handle, &wdirp->finddata) == 0)
        {
            char* old_start = wdirp->start;
            long name_len = strlen(wdirp->finddata.name) + 1;
            wdirp->start = realloc(wdirp->start, wdirp->end - wdirp->start + name_len);
            wdirp->curr = wdirp->start + (wdirp->curr - old_start);
            wdirp->end = wdirp->curr + name_len;
            strcpy(wdirp->curr, wdirp->finddata.name);
        }
M
Ming, Bai 已提交
387
        else
P
prife 已提交
388 389 390 391 392 393
        {
            if ((result = GetLastError()) == ERROR_NO_MORE_FILES)
                wdirp->curr = NULL;
            else
                return win32_result_to_dfs(result);
        }
M
Ming, Bai 已提交
394 395
    }

P
prife 已提交
396
    return sizeof(struct dirent);
M
Ming, Bai 已提交
397 398 399 400 401 402 403 404 405 406
}

static int dfs_win32_unlink(struct dfs_filesystem *fs, const char *path)
{
    int result;
    char *fp;
    fp = winpath_dirdup(WIN32_DIRDISK_ROOT, path);
    if (fp == RT_NULL)
    {
        rt_kprintf("out of memory.\n");
Y
yygg_you 已提交
407
        return -ENOMEM;
M
Ming, Bai 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    }

    result = GetFileAttributes(fp);
    if (result == INVALID_FILE_ATTRIBUTES)
        goto __err;

    if (result & FILE_ATTRIBUTE_DIRECTORY)//winnt.h
    {
        if (RemoveDirectory(fp) == RT_FALSE)
            goto __err;
    }
    else //(result & FILE_ATTRIBUTE_NORMAL)
    {
        if (_unlink(fp) < 0)
            goto __err;
    }

    rt_free(fp);
    return 0;
__err:
    rt_free(fp);
    return win32_result_to_dfs(GetLastError());
}

static int dfs_win32_rename(
    struct dfs_filesystem *fs,
    const char *oldpath,
    const char *newpath)
{
    int result;
    char *op, *np;
    op = winpath_dirdup(WIN32_DIRDISK_ROOT, oldpath);
    np = winpath_dirdup(WIN32_DIRDISK_ROOT, newpath);
    if (op == RT_NULL || np == RT_NULL)
    {
        rt_kprintf("out of memory.\n");
Y
yygg_you 已提交
444
        return -ENOMEM;
M
Ming, Bai 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    }

    /* If the function fails, the return value is zero. */
    result = MoveFile(op, np);

    rt_free(op);
    rt_free(np);

    if (result == 0)
        return win32_result_to_dfs(GetLastError());

    return 0;
}

static int dfs_win32_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
{
    WIN32_FIND_DATA fileInfo;
    HANDLE hFind;
    char *fp;
    fp = winpath_dirdup(WIN32_DIRDISK_ROOT, path);
    if (fp == RT_NULL)
    {
        rt_kprintf("out of memory.\n");
Y
yygg_you 已提交
468
        return -ENOMEM;
M
Ming, Bai 已提交
469 470 471 472 473 474 475 476
    }

    hFind = FindFirstFile(fp, &fileInfo);
    rt_free(fp);

    if (hFind == INVALID_HANDLE_VALUE)
        goto __err;

Y
yygg_you 已提交
477 478
    st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
                  S_IWUSR | S_IWGRP | S_IWOTH;
M
Ming, Bai 已提交
479 480 481 482

    /* convert file info to dfs stat structure */
    if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
Y
yygg_you 已提交
483 484
        st->st_mode &= ~S_IFREG;
        st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
M
Ming, Bai 已提交
485 486 487 488
    }

    st->st_dev  = 0;
    st->st_size = fileInfo.nFileSizeLow;
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

    /* get st_mtime. */
    {
        LARGE_INTEGER time_tmp;
        time_tmp.LowPart = fileInfo.ftLastWriteTime.dwLowDateTime;
        time_tmp.HighPart = fileInfo.ftLastWriteTime.dwHighDateTime;

        /* removes the diff between 1970 and 1601. */
        time_tmp.QuadPart -= 11644473600000 * 10000;
        /* converts back from 100-nanoseconds to seconds. */
        time_tmp.QuadPart /= 10UL * 1000 * 1000;

        st->st_mtime = time_tmp.QuadPart;
    }

M
Ming, Bai 已提交
504 505 506 507 508 509 510 511
    FindClose(hFind);

    return 0;

__err:
    return win32_result_to_dfs(GetLastError());
}

Y
yygg_you 已提交
512
static const struct dfs_file_ops dfs_win32_file_ops =
M
Ming, Bai 已提交
513 514 515 516 517 518 519 520 521
{
    dfs_win32_open,
    dfs_win32_close,
    dfs_win32_ioctl,
    dfs_win32_read,
    dfs_win32_write,
    dfs_win32_flush,
    dfs_win32_seek,
    dfs_win32_getdents,
Y
yygg_you 已提交
522 523 524 525 526 527 528 529 530 531 532
};

static const struct dfs_filesystem_ops dfs_win32_ops =
{
    "wdir", /* file system type: dir */
    DFS_FS_FLAG_DEFAULT,
    &dfs_win32_file_ops,
    dfs_win32_mount,
    dfs_win32_unmount,
    dfs_win32_mkfs,
    dfs_win32_statfs,
M
Ming, Bai 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    dfs_win32_unlink,
    dfs_win32_stat,
    dfs_win32_rename,
};

int dfs_win32_init(void)
{
    /* register uffs file system */
    dfs_register(&dfs_win32_ops);

    return 0;
}

static rt_err_t nop_init(rt_device_t dev)
{
    return RT_EOK;
}

static rt_err_t nop_open(rt_device_t dev, rt_uint16_t oflag)
{
    return RT_EOK;
}

static rt_err_t nop_close(rt_device_t dev)
{
    return RT_EOK;
}

static rt_size_t nop_read(rt_device_t dev,
                          rt_off_t    pos,
                          void       *buffer,
                          rt_size_t   size)
{
    return size;
}

static rt_size_t nop_write(rt_device_t dev,
                           rt_off_t    pos,
                           const void *buffer,
                           rt_size_t   size)
{
    return size;
}

B
bernard 已提交
577
static rt_err_t nop_control(rt_device_t dev, int cmd, void *args)
M
Ming, Bai 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
{
    return RT_EOK;
}

static struct rt_device win_sharedir_dev;
rt_err_t rt_win_sharedir_init(const char *name)
{
    rt_device_t dev;

    dev = &win_sharedir_dev;
    RT_ASSERT(dev != RT_NULL);

    /* set device class and generic device interface */
    dev->type        = RT_Device_Class_Block;
    dev->init        = nop_init;
    dev->open        = nop_open;
    dev->read        = nop_read;
    dev->write       = nop_write;
    dev->close       = nop_close;
    dev->control     = nop_control;

    dev->rx_indicate = RT_NULL;
    dev->tx_complete = RT_NULL;

    /* register to RT-Thread device system */
    return rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
}