Subversion Repositories HelenOS

Rev

Rev 3505 | Rev 3634 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup fs
  30.  * @{
  31.  */
  32.  
  33. /**
  34.  * @file    fat_dentry.c
  35.  * @brief   Functions that work with FAT directory entries.
  36.  */
  37.  
  38. #include "fat_dentry.h"
  39. #include <ctype.h>
  40.  
  41. #define FAT_PAD         ' '
  42.  
  43. #define FAT_DENTRY_UNUSED   0x00
  44. #define FAT_DENTRY_E5_ESC   0x05
  45. #define FAT_DENTRY_DOT      0x2e
  46. #define FAT_DENTRY_ERASED   0xe5
  47.  
  48. static bool is_d_char(const char ch)
  49. {
  50.     if (isalnum(ch) || ch == '_')
  51.         return true;
  52.     else
  53.         return false;
  54. }
  55.  
  56. bool fat_dentry_name_verify(const char *name)
  57. {
  58.     unsigned i, dot;
  59.     bool dot_found = false;
  60.    
  61.  
  62.     for (i = 0; name[i]; i++) {
  63.         if (name[i] == '.') {
  64.             if (dot_found) {
  65.                 return false;
  66.             } else {
  67.                 dot_found = true;
  68.                 dot = i;
  69.             }
  70.         } else {
  71.             if (!is_d_char(name[i]))
  72.                 return false;
  73.         }
  74.     }
  75.  
  76.     if (dot_found) {
  77.         if (dot > FAT_NAME_LEN)
  78.             return false;
  79.         if (i - dot > FAT_EXT_LEN + 1)
  80.             return false;
  81.     } else {
  82.         if (i > FAT_NAME_LEN)
  83.             return false;
  84.     }
  85.  
  86.     return true;
  87. }
  88.  
  89. void fat_dentry_name_get(const fat_dentry_t *d, char *buf)
  90. {
  91.     int i;
  92.  
  93.     for (i = 0; i < FAT_NAME_LEN; i++) {
  94.         if (d->name[i] == FAT_PAD)
  95.             break;
  96.         if (d->name[i] == FAT_DENTRY_E5_ESC)
  97.             *buf++ = 0xe5;
  98.         else
  99.             *buf++ = d->name[i];
  100.     }
  101.     if (d->ext[0] != FAT_PAD)
  102.         *buf++ = '.';
  103.     for (i = 0; i < FAT_EXT_LEN; i++) {
  104.         if (d->ext[i] == FAT_PAD) {
  105.             *buf = '\0';
  106.             return;
  107.         }
  108.         if (d->ext[i] == FAT_DENTRY_E5_ESC)
  109.             *buf++ = 0xe5;
  110.         else
  111.             *buf++ = d->ext[i];
  112.     }
  113.     *buf = '\0';
  114. }
  115.  
  116. void fat_dentry_name_set(fat_dentry_t *d, const char *name)
  117. {
  118.     int i;
  119.     const char fake_ext[] = "   ";
  120.  
  121.  
  122.     for (i = 0; i < FAT_NAME_LEN; i++) {
  123.         switch ((uint8_t) *name) {
  124.         case 0xe5:
  125.             d->name[i] = FAT_DENTRY_E5_ESC;
  126.             name++;
  127.             break;
  128.         case '\0':
  129.         case '.':
  130.             d->name[i] = FAT_PAD;
  131.             break;
  132.         default:
  133.             d->name[i] = toupper(*name++);
  134.             break;
  135.         }
  136.     }
  137.     if (*name++ != '.')
  138.         name = fake_ext;
  139.     for (i = 0; i < FAT_EXT_LEN; i++) {
  140.         switch ((uint8_t) *name) {
  141.         case 0xe5:
  142.             d->ext[i] = FAT_DENTRY_E5_ESC;
  143.             name++;
  144.             break;
  145.         case '\0':
  146.             d->ext[i] = FAT_PAD;
  147.             break;
  148.         default:
  149.             d->ext[i] = toupper(*name++);
  150.             break;
  151.         }
  152.     }
  153. }
  154.  
  155. fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d)
  156. {
  157.     if (d->attr & FAT_ATTR_VOLLABEL) {
  158.         /* volume label entry */
  159.         return FAT_DENTRY_SKIP;
  160.     }
  161.     if (d->name[0] == FAT_DENTRY_ERASED) {
  162.         /* not-currently-used entry */
  163.         return FAT_DENTRY_FREE;
  164.     }
  165.     if (d->name[0] == FAT_DENTRY_UNUSED) {
  166.         /* never used entry */
  167.         return FAT_DENTRY_LAST;
  168.     }
  169.     if (d->name[0] == FAT_DENTRY_DOT) {
  170.         /*
  171.          * Most likely '.' or '..'.
  172.          * It cannot occur in a regular file name.
  173.          */
  174.         return FAT_DENTRY_SKIP;
  175.     }
  176.     return FAT_DENTRY_VALID;
  177. }
  178.  
  179. /**
  180.  * @}
  181.  */
  182.