File size: 5,873 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

U0 Date2ISO1(CISO1Date *dst,CDate cdt)
{
  CDateStruct ds;
  Date2Struct(&ds,cdt);
  dst->year=ds.year-ISO1_BASE_YEAR;
  dst->mon=ds.mon;
  dst->day=ds.day_of_mon;
  dst->hour=ds.hour;
  dst->min=ds.min;
  dst->sec=ds.sec;
  dst->sec100=ds.sec100;
}

CDate ISO1DateStruct2CDate(CISO1Date *dt)
{
  CDateStruct ds;
  MemSet(&ds,0,sizeof(CDateStruct));
  ds.day_of_mon=dt->day;
  ds.mon=dt->mon;
  ds.year=dt->year+ISO1_BASE_YEAR;
  ds.sec100=dt->sec100;
  ds.sec=dt->sec;
  ds.min=dt->min;
  ds.hour=dt->hour;
  return Struct2Date(&ds);
}

Bool ISO1FromName(U8 *dst,U8 *src)
{
  I64 i,j,n;
  MemSet(dst,0,CDIR_FILENAME_LEN);
  n=*src++;
  if (n==1 && !*src) {
    *dst='.';
  } else if (n==1 && *src==1) {
    *dst='.';
    dst[1]='.';
  } else {
    n>>=1;
    j=0;
    for (i=0;i<n;i++) {
      src++;
      if (*src==';')
        break;
      if (Bt(char_bmp_filename,*src)) {
        if (j>=CDIR_FILENAME_LEN-1)
          return FALSE;
        dst[j++]=*src++;
      } else
        return FALSE;
    }
  }
  return FileNameChk(dst);
}

Bool ISO1CDirFill(CDirEntry *tmpde,CISO1DirEntry *de)
{
  Bool res;
  MemSet(tmpde,0,sizeof(CDirEntry));
  res=ISO1FromName(tmpde->name,&de->name_len);
  tmpde->clus=de->loc.little;
  tmpde->size=de->size.little;
  tmpde->attr=FileAttr(tmpde->name);
  if (de->flags & ISO1_ATTR_DIR)
    tmpde->attr|=RS_ATTR_DIR;
  tmpde->datetime=ISO1DateStruct2CDate(&de->date);
  return res;
}

Bool ISO1FileFind(CDrv *dv,I64 cur_dir_clus,U8 *name,
        CDirEntry *_res,I64 fuf_flags=0)
{//FUF_JUST_DIRS, FUF_JUST_FILES
  CISO1DirEntry *isoptr,*buf;
  U8 dname[CDIR_FILENAME_LEN];
  Bool res=FALSE,unlock;
  I64 i;
  if (fuf_flags&~FUG_FILE_FIND)
    throw('FUF');
  DrvChk(dv);
  if (dv->fs_type!=FSt_ISO9660)
    PrintErr("Not ISO9660 Drv\n");
  else
    try {
      unlock=DrvLock(dv);
      isoptr=MAlloc(dv->spc<<BLK_SIZE_BITS);
      ClusRead(dv,isoptr,cur_dir_clus,1);
      if (isoptr->name_len==1 && !isoptr->name) {//curdir
        i=(isoptr->size.little+dv->spc<<BLK_SIZE_BITS-1)/dv->spc<<BLK_SIZE_BITS;
        buf=MAlloc(dv->spc<<BLK_SIZE_BITS*i);
        ClusRead(dv,buf,cur_dir_clus,i);
        Free(isoptr);
      } else {
        buf=isoptr;
        i=1;
      }
      i*=dv->spc<<BLK_SIZE_BITS;
      isoptr=buf;
      while (i>0) {
        if (!isoptr->len) {
          isoptr(U8 *)++;
          i--;
        } else {
          ISO1FromName(dname,&isoptr->name_len);
          if (*dname) {
            if (!StrCmp(name,dname)) {
              res=ISO1CDirFill(_res,isoptr);
              if (res &&
                    !(fuf_flags&FUF_JUST_DIRS &&
                    !(_res->attr & RS_ATTR_DIR)) &&
                    !(fuf_flags&FUF_JUST_FILES && _res->attr & RS_ATTR_DIR) )
                goto iff_done;
              else
                res=FALSE;
            }
          }
          i-=isoptr->len;
          isoptr(U8 *)+=isoptr->len;
        }
      }
iff_done:
      Free(buf);
      if (unlock)
        DrvUnlock(dv);
    } catch
      if (unlock)
        DrvUnlock(dv);
  return res;
}

U8 *ISO1FileRead(CDrv *dv,U8 *cur_dir,U8 *filename,I64 *_size,I64 *_attr)
{
  U8 *buf=NULL;
  CDirEntry de;
  I64 c,blk_cnt,cur_dir_clus;
  DrvChk(dv);
  *_size=0;
  *_attr=0;
  if (dv->fs_type!=FSt_ISO9660)
    PrintErr("Not ISO9660 Drv\n");
  else
    try {
      DrvLock(dv);
      cur_dir_clus=Name2DirClus(dv,cur_dir);
      if (ISO1FileFind(dv,cur_dir_clus,filename,&de,FUF_JUST_FILES)) {
        blk_cnt=(de.size+BLK_SIZE-1)>>BLK_SIZE_BITS;
        buf=MAlloc(blk_cnt<<BLK_SIZE_BITS+1);
        c=de.clus;
        c=ClusBlkRead(dv,buf,c,blk_cnt);
        buf[de.size]=0; //Terminate
        *_size=de.size;
        *_attr=FileAttr(de.name,de.attr);
      }
      DrvUnlock(dv);
    } catch
      DrvUnlock(dv);
  return buf;
}

Bool ISO1Cd(U8 *name,I64 cur_dir_clus)
{
  CDirEntry de;
  if (Fs->cur_dv->fs_type!=FSt_ISO9660)
    PrintErr("Not ISO9660 Drv\n");
  else if (ISO1FileFind(Fs->cur_dv,cur_dir_clus,name,&de,FUF_JUST_DIRS))
    return TRUE;
  else
    PrintErr("File not found: \"%s\".\n",name);
  return FALSE;
}

CDirEntry *ISO1FilesFind(U8 *files_find_mask,
        I64 fuf_flags,CDirEntry *parent=NULL)
{
  CDrv *dv=Fs->cur_dv;
  CISO1DirEntry *buf,*buf2,*isoptr;
  I64 i,cur_dir_clus=Name2DirClus(dv,Fs->cur_dir);
  CDirEntry *res=NULL,*tmpde;
  if (fuf_flags&~FUG_FILES_FIND)
    throw('FUF');
  isoptr=MAlloc(dv->spc<<BLK_SIZE_BITS);
  ClusRead(dv,isoptr,cur_dir_clus,1);
  if (isoptr->name_len==1 && !isoptr->name) {//curdir
    i=(isoptr->size.little+dv->spc<<BLK_SIZE_BITS-1)/dv->spc<<BLK_SIZE_BITS;
    buf=MAlloc(dv->spc<<BLK_SIZE_BITS*i);
    ClusRead(dv,buf,cur_dir_clus,i);
    Free(isoptr);
  } else {
    buf=isoptr;
    i=1;
  }
  buf2=buf;
  i*=dv->spc<<BLK_SIZE_BITS;
  while (i>0) {
    if (!buf->len) {
      buf(U8 *)++;
      i--;
    } else {
      tmpde=MAlloc(sizeof(CDirEntry));
      if (ISO1CDirFill(tmpde,buf)) {
        tmpde->parent=parent;
        if (Bt(&fuf_flags,FUf_RECURSE) && tmpde->attr&RS_ATTR_DIR &&
              *tmpde->name!='.') {
          tmpde->next=res;
          res=tmpde;
          tmpde->full_name=DirNameAbs(tmpde->name);
          if (Cd(tmpde->name)) {
            tmpde->sub=ISO1FilesFind(files_find_mask,fuf_flags,tmpde);
            Cd("..");
          }
        } else {
          tmpde->full_name=FileNameAbs(tmpde->name);
          if ((tmpde->attr & RS_ATTR_DIR ||
                !Bt(&fuf_flags,FUf_JUST_DIRS)) &&
                !(Bt(&fuf_flags,FUf_RECURSE) &&
                *tmpde->name=='.' && tmpde->attr&RS_ATTR_DIR) &&
                FilesFindMatch(tmpde->full_name,files_find_mask,fuf_flags)) {
            tmpde->next=res;
            res=tmpde;
          } else
            DirEntryDel(tmpde);
        }
      } else
        DirEntryDel(tmpde);
      i-=buf->len;
      buf(U8 *)+=buf->len;
    }
  }
  Free(buf2);
  return res;
}