File size: 3,887 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

U8 *FileRead(U8 *filename,I64 *_size=NULL,I64 *_attr=NULL)
{//Read whole file from disk.
  CHashGeneric *tmph;
  U8 *absname,*altname,*curname,*res=NULL;
  I64 i,size=0,attr=0;
  CDirContext *dirc;
  CArcCompress *arc;
  absname=FileNameAbs(filename);
  altname=ToggleZorNotZ(absname);
  if ((tmph=HashFind(absname,adam_task->hash_table,HTT_FILE))||
        (tmph=HashFind(altname,adam_task->hash_table,HTT_FILE))) {
    size=tmph->user_data1;
    res=MAlloc(size+1);
    MemCpy(res,tmph->user_data0,size);
    res[size]=0; //Terminate
    attr=FileAttr(tmph->str,attr);
  } else {
    for (i=0;i<2 && !res;i++) {//Try name, then altname
      if (!i)
        curname=absname;
      else
        curname=altname;
      if (dirc=DirContextNew(curname)) {
        switch (dirc->dv->fs_type) {
          case FSt_REDSEA:
            res=RedSeaFileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);
            break;
          case FSt_FAT32:
            res=FAT32FileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);
            break;
          case FSt_ISO9660:
            res=ISO1FileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);
            break;
          default:
            PrintErr("File System Not Supported\n");
        }
        DirContextDel(dirc);
      }
    }

    //Search parent directories.
    for (i=0;i<2 && !res;i++) {//Try name, then altname
      if (!i)
        curname=absname;
      else
        curname=altname;
      if (dirc=DirContextNew(curname)) {
        while (!res && StrCmp(Fs->cur_dir,"/")) {
          Cd("..");
          switch (Fs->cur_dv->fs_type) {
            case FSt_REDSEA:
              res=RedSeaFileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);
              break;
            case FSt_FAT32:
              res=FAT32FileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);
              break;
            case FSt_ISO9660:
              res=ISO1FileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);
              break;
            default:
              PrintErr("File System Not Supported\n");
          }
        }
        DirContextDel(dirc);
      }
    }
    if (!res)
      PrintErr("File not found: \"%s\".\n",filename);
    if (res && attr & RS_ATTR_RESIDENT)
      HashGenericAdd(curname,HTT_FILE,AMAllocIdent(res),size,0,adam_task);
  }
  if (res && attr & RS_ATTR_COMPRESSED) {
    arc=res;
    size=arc->expanded_size;
    res=ExpandBuf(arc);
    Free(arc);
  }
  if (_attr) *_attr=attr;
  if (_size) *_size=size;
  Free(absname);
  Free(altname);
  return res;
}

I64 FileWrite(U8 *filename,U8 *fbuf,I64 size,CDate cdt=0,I64 attr=0)
{//Write whole file to disk.
  I64 c=0;
  CHashGeneric *tmph;
  CDirContext *dirc;
  U8 *fbuf2,*absname=FileNameAbs(filename);
  if (dirc=DirContextNew(filename,FALSE,TRUE)) {
    attr=FileAttr(dirc->mask,attr);
    if (attr&RS_ATTR_COMPRESSED) {
      fbuf=CompressBuf(fbuf,size);
      size=fbuf(CArcCompress *)->compressed_size;
      fbuf2=fbuf;
    } else
      fbuf2=NULL;
    if (!cdt) cdt=Now;
    switch (dirc->dv->fs_type) {
      case FSt_REDSEA:
        c=RedSeaFileWrite(dirc->dv,Fs->cur_dir,dirc->mask,fbuf,size,cdt,attr);
        break;
      case FSt_FAT32:
        c=FAT32FileWrite(dirc->dv,Fs->cur_dir,dirc->mask,fbuf,size,cdt,attr);
        break;
      case FSt_ISO9660:
        PrintErr("Not Writable\n");
        break;
      default:
        PrintErr("File System Not Supported\n");
    }
    if (tmph=HashFind(absname,adam_task->hash_table,HTT_FILE)) {
      if (attr & RS_ATTR_RESIDENT) {
        Free(tmph->user_data0);
        tmph->user_data0=AMAllocIdent(fbuf);
        tmph->user_data1=size;
      } else
        HashRemDel(tmph,adam_task->hash_table);
    } else if (attr & RS_ATTR_RESIDENT)
      HashGenericAdd(absname,HTT_FILE,AMAllocIdent(fbuf),size,0,adam_task);
    Free(fbuf2);
    DirContextDel(dirc);
  }
  Free(absname);
  return c;
}