| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id: drive_local.cpp,v 1.82 2009-07-18 18:42:55 c2woody Exp $ */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <time.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | #include "dosbox.h" |
| 28 | #include "dos_inc.h" |
| 29 | #include "drives.h" |
| 30 | #include "support.h" |
| 31 | #include "cross.h" |
| 32 | #include "inout.h" |
| 33 | |
| 34 | class localFile : public DOS_File { |
| 35 | public: |
| 36 | localFile(const char* name, FILE * handle); |
| 37 | bool Read(Bit8u * data,Bit16u * size); |
| 38 | bool Write(Bit8u * data,Bit16u * size); |
| 39 | bool Seek(Bit32u * pos,Bit32u type); |
| 40 | bool Close(); |
| 41 | Bit16u GetInformation(void); |
| 42 | bool UpdateDateTimeFromHost(void); |
| 43 | void FlagReadOnlyMedium(void); |
| 44 | private: |
| 45 | FILE * fhandle; |
| 46 | bool read_only_medium; |
| 47 | enum { NONE,READ,WRITE } last_action; |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | bool localDrive::FileCreate(DOS_File * * file,char * name,Bit16u /*attributes*/) { |
| 52 | //TODO Maybe care for attributes but not likely |
| 53 | char newname[CROSS_LEN]; |
| 54 | strcpy(newname,basedir); |
| 55 | strcat(newname,name); |
| 56 | CROSS_FILENAME(newname); |
| 57 | char* temp_name = dirCache.GetExpandName(newname); //Can only be used in till a new drive_cache action is preformed */ |
| 58 | /* Test if file exists (so we need to truncate it). don't add to dirCache then */ |
| 59 | bool existing_file = false; |
| 60 | |
| 61 | FILE * test = fopen_wrap(temp_name,"rb+"); |
| 62 | if(test) { |
| 63 | fclose(test); |
| 64 | existing_file=true; |
| 65 | |
| 66 | } |
| 67 | |
| 68 | FILE * hand = fopen_wrap(temp_name,"wb+"); |
| 69 | if (!hand){ |
| 70 | LOG_MSG("Warning: file creation failed: %s",newname); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if(!existing_file) dirCache.AddEntry(newname, true); |
| 75 | /* Make the 16 bit device information */ |
| 76 | *file=new localFile(name,hand); |
| 77 | (*file)->flags=OPEN_READWRITE; |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool localDrive::FileOpen(DOS_File * * file,char * name,Bit32u flags) { |
| 83 | const char* type; |
| 84 | switch (flags&0xf) { |
| 85 | case OPEN_READ:type="rb"; break; |
| 86 | case OPEN_WRITE:type="rb+"; break; |
| 87 | case OPEN_READWRITE:type="rb+"; break; |
| 88 | default: |
| 89 | DOS_SetError(DOSERR_ACCESS_CODE_INVALID); |
| 90 | return false; |
| 91 | } |
| 92 | char newname[CROSS_LEN]; |
| 93 | strcpy(newname,basedir); |
| 94 | strcat(newname,name); |
| 95 | CROSS_FILENAME(newname); |
| 96 | dirCache.ExpandName(newname); |
| 97 | |
| 98 | FILE * hand = fopen_wrap(newname,type); |
| 99 | // Bit32u err=errno; |
| 100 | if (!hand) { |
| 101 | if((flags&0xf) != OPEN_READ) { |
| 102 | FILE * hmm = fopen_wrap(newname,"rb"); |
| 103 | if (hmm) { |
| 104 | fclose(hmm); |
| 105 | LOG_MSG("Warning: file %s exists and failed to open in write mode.\nPlease Remove write-protection",newname); |
| 106 | } |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | *file=new localFile(name,hand); |
| 112 | (*file)->flags=flags; //for the inheritance flag and maybe check for others. |
| 113 | // (*file)->SetFileName(newname); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | FILE * localDrive::GetSystemFilePtr(char const * const name, char const * const type) { |
| 118 | |
| 119 | char newname[CROSS_LEN]; |
| 120 | strcpy(newname,basedir); |
| 121 | strcat(newname,name); |
| 122 | CROSS_FILENAME(newname); |
| 123 | dirCache.ExpandName(newname); |
| 124 | |
| 125 | return fopen_wrap(newname,type); |
| 126 | } |
| 127 | |
| 128 | bool localDrive::GetSystemFilename(char *sysName, char const * const dosName) { |
| 129 | |
| 130 | strcpy(sysName, basedir); |
| 131 | strcat(sysName, dosName); |
| 132 | CROSS_FILENAME(sysName); |
| 133 | dirCache.ExpandName(sysName); |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | bool localDrive::FileUnlink(char * name) { |
| 138 | char newname[CROSS_LEN]; |
| 139 | strcpy(newname,basedir); |
| 140 | strcat(newname,name); |
| 141 | CROSS_FILENAME(newname); |
| 142 | char *fullname = dirCache.GetExpandName(newname); |
| 143 | if (unlink(fullname)) { |
| 144 | //Unlink failed for some reason try finding it. |
| 145 | struct stat buffer; |
| 146 | if(stat(fullname,&buffer)) return false; // File not found. |
| 147 | |
| 148 | FILE* file_writable = fopen_wrap(fullname,"rb+"); |
| 149 | if(!file_writable) return false; //No acces ? ERROR MESSAGE NOT SET. FIXME ? |
| 150 | fclose(file_writable); |
| 151 | |
| 152 | //File exists and can technically be deleted, nevertheless it failed. |
| 153 | //This means that the file is probably open by some process. |
| 154 | //See if We have it open. |
| 155 | bool found_file = false; |
| 156 | for(Bitu i = 0;i < DOS_FILES;i++){ |
| 157 | if(Files[i] && Files[i]->IsName(name)) { |
| 158 | Bitu max = DOS_FILES; |
| 159 | while(Files[i]->IsOpen() && max--) { |
| 160 | Files[i]->Close(); |
| 161 | if (Files[i]->RemoveRef()<=0) break; |
| 162 | } |
| 163 | found_file=true; |
| 164 | } |
| 165 | } |
| 166 | if(!found_file) return false; |
| 167 | if (!unlink(fullname)) { |
| 168 | dirCache.DeleteEntry(newname); |
| 169 | return true; |
| 170 | } |
| 171 | return false; |
| 172 | } else { |
| 173 | dirCache.DeleteEntry(newname); |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | bool localDrive::FindFirst(char * _dir,DOS_DTA & dta,bool fcb_findfirst) { |
| 179 | char tempDir[CROSS_LEN]; |
| 180 | strcpy(tempDir,basedir); |
| 181 | strcat(tempDir,_dir); |
| 182 | CROSS_FILENAME(tempDir); |
| 183 | |
| 184 | if (allocation.mediaid==0xF0 ) { |
| 185 | EmptyCache(); //rescan floppie-content on each findfirst |
| 186 | } |
| 187 | |
| 188 | char end[2]={CROSS_FILESPLIT,0}; |
| 189 | if (tempDir[strlen(tempDir)-1]!=CROSS_FILESPLIT) strcat(tempDir,end); |
| 190 | |
| 191 | Bit16u id; |
| 192 | if (!dirCache.FindFirst(tempDir,id)) { |
| 193 | DOS_SetError(DOSERR_PATH_NOT_FOUND); |
| 194 | return false; |
| 195 | } |
| 196 | strcpy(srchInfo[id].srch_dir,tempDir); |
| 197 | dta.SetDirID(id); |
| 198 | |
| 199 | Bit8u sAttr; |
| 200 | dta.GetSearchParams(sAttr,tempDir); |
| 201 | |
| 202 | if (this->isRemote() && this->isRemovable()) { |
| 203 | // cdroms behave a bit different than regular drives |
| 204 | if (sAttr == DOS_ATTR_VOLUME) { |
| 205 | dta.SetResult(dirCache.GetLabel(),0,0,0,DOS_ATTR_VOLUME); |
| 206 | return true; |
| 207 | } |
| 208 | } else { |
| 209 | if (sAttr == DOS_ATTR_VOLUME) { |
| 210 | if ( strcmp(dirCache.GetLabel(), "") == 0 ) { |
| 211 | // LOG(LOG_DOSMISC,LOG_ERROR)("DRIVELABEL REQUESTED: none present, returned NOLABEL"); |
| 212 | // dta.SetResult("NO_LABEL",0,0,0,DOS_ATTR_VOLUME); |
| 213 | // return true; |
| 214 | DOS_SetError(DOSERR_NO_MORE_FILES); |
| 215 | return false; |
| 216 | } |
| 217 | dta.SetResult(dirCache.GetLabel(),0,0,0,DOS_ATTR_VOLUME); |
| 218 | return true; |
| 219 | } else if ((sAttr & DOS_ATTR_VOLUME) && (*_dir == 0) && !fcb_findfirst) { |
| 220 | //should check for a valid leading directory instead of 0 |
| 221 | //exists==true if the volume label matches the searchmask and the path is valid |
| 222 | if (WildFileCmp(dirCache.GetLabel(),tempDir)) { |
| 223 | dta.SetResult(dirCache.GetLabel(),0,0,0,DOS_ATTR_VOLUME); |
| 224 | return true; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | return FindNext(dta); |
| 229 | } |
| 230 | |
| 231 | bool localDrive::FindNext(DOS_DTA & dta) { |
| 232 | |
| 233 | char * dir_ent; |
| 234 | struct stat stat_block; |
| 235 | char full_name[CROSS_LEN]; |
| 236 | char dir_entcopy[CROSS_LEN]; |
| 237 | |
| 238 | Bit8u srch_attr;char srch_pattern[DOS_NAMELENGTH_ASCII]; |
| 239 | Bit8u find_attr; |
| 240 | |
| 241 | dta.GetSearchParams(srch_attr,srch_pattern); |
| 242 | Bit16u id = dta.GetDirID(); |
| 243 | |
| 244 | again: |
| 245 | if (!dirCache.FindNext(id,dir_ent)) { |
| 246 | DOS_SetError(DOSERR_NO_MORE_FILES); |
| 247 | return false; |
| 248 | } |
| 249 | if(!WildFileCmp(dir_ent,srch_pattern)) goto again; |
| 250 | |
| 251 | strcpy(full_name,srchInfo[id].srch_dir); |
| 252 | strcat(full_name,dir_ent); |
| 253 | |
| 254 | //GetExpandName might indirectly destroy dir_ent (by caching in a new directory |
| 255 | //and due to its design dir_ent might be lost.) |
| 256 | //Copying dir_ent first |
| 257 | strcpy(dir_entcopy,dir_ent); |
| 258 | if (stat(dirCache.GetExpandName(full_name),&stat_block)!=0) { |
| 259 | goto again;//No symlinks and such |
| 260 | } |
| 261 | |
| 262 | if(stat_block.st_mode & S_IFDIR) find_attr=DOS_ATTR_DIRECTORY; |
| 263 | else find_attr=DOS_ATTR_ARCHIVE; |
| 264 | if (~srch_attr & find_attr & (DOS_ATTR_DIRECTORY | DOS_ATTR_HIDDEN | DOS_ATTR_SYSTEM)) goto again; |
| 265 | |
| 266 | /*file is okay, setup everything to be copied in DTA Block */ |
| 267 | char find_name[DOS_NAMELENGTH_ASCII];Bit16u find_date,find_time;Bit32u find_size; |
| 268 | |
| 269 | if(strlen(dir_entcopy)<DOS_NAMELENGTH_ASCII){ |
| 270 | strcpy(find_name,dir_entcopy); |
| 271 | upcase(find_name); |
| 272 | } |
| 273 | |
| 274 | find_size=(Bit32u) stat_block.st_size; |
| 275 | struct tm *time; |
| 276 | if((time=localtime(&stat_block.st_mtime))!=0){ |
| 277 | find_date=DOS_PackDate((Bit16u)(time->tm_year+1900),(Bit16u)(time->tm_mon+1),(Bit16u)time->tm_mday); |
| 278 | find_time=DOS_PackTime((Bit16u)time->tm_hour,(Bit16u)time->tm_min,(Bit16u)time->tm_sec); |
| 279 | } else { |
| 280 | find_time=6; |
| 281 | find_date=4; |
| 282 | } |
| 283 | dta.SetResult(find_name,find_size,find_date,find_time,find_attr); |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | bool localDrive::GetFileAttr(char * name,Bit16u * attr) { |
| 288 | char newname[CROSS_LEN]; |
| 289 | strcpy(newname,basedir); |
| 290 | strcat(newname,name); |
| 291 | CROSS_FILENAME(newname); |
| 292 | dirCache.ExpandName(newname); |
| 293 | |
| 294 | struct stat status; |
| 295 | if (stat(newname,&status)==0) { |
| 296 | *attr=DOS_ATTR_ARCHIVE; |
| 297 | if(status.st_mode & S_IFDIR) *attr|=DOS_ATTR_DIRECTORY; |
| 298 | return true; |
| 299 | } |
| 300 | *attr=0; |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | bool localDrive::MakeDir(char * dir) { |
| 305 | char newdir[CROSS_LEN]; |
| 306 | strcpy(newdir,basedir); |
| 307 | strcat(newdir,dir); |
| 308 | CROSS_FILENAME(newdir); |
| 309 | #if defined (WIN32) /* MS Visual C++ */ |
| 310 | int temp=mkdir(dirCache.GetExpandName(newdir)); |
| 311 | #else |
| 312 | int temp=mkdir(dirCache.GetExpandName(newdir),0700); |
| 313 | #endif |
| 314 | if (temp==0) dirCache.CacheOut(newdir,true); |
| 315 | |
| 316 | return (temp==0);// || ((temp!=0) && (errno==EEXIST)); |
| 317 | } |
| 318 | |
| 319 | bool localDrive::RemoveDir(char * dir) { |
| 320 | char newdir[CROSS_LEN]; |
| 321 | strcpy(newdir,basedir); |
| 322 | strcat(newdir,dir); |
| 323 | CROSS_FILENAME(newdir); |
| 324 | int temp=rmdir(dirCache.GetExpandName(newdir)); |
| 325 | if (temp==0) dirCache.DeleteEntry(newdir,true); |
| 326 | return (temp==0); |
| 327 | } |
| 328 | |
| 329 | bool localDrive::TestDir(char * dir) { |
| 330 | char newdir[CROSS_LEN]; |
| 331 | strcpy(newdir,basedir); |
| 332 | strcat(newdir,dir); |
| 333 | CROSS_FILENAME(newdir); |
| 334 | dirCache.ExpandName(newdir); |
| 335 | // Skip directory test, if "\" |
| 336 | size_t len = strlen(newdir); |
| 337 | if (len && (newdir[len-1]!='\\')) { |
| 338 | // It has to be a directory ! |
| 339 | struct stat test; |
| 340 | if (stat(newdir,&test)) return false; |
| 341 | if ((test.st_mode & S_IFDIR)==0) return false; |
| 342 | }; |
| 343 | int temp=access(newdir,F_OK); |
| 344 | return (temp==0); |
| 345 | } |
| 346 | |
| 347 | bool localDrive::Rename(char * oldname,char * newname) { |
| 348 | char newold[CROSS_LEN]; |
| 349 | strcpy(newold,basedir); |
| 350 | strcat(newold,oldname); |
| 351 | CROSS_FILENAME(newold); |
| 352 | dirCache.ExpandName(newold); |
| 353 | |
| 354 | char newnew[CROSS_LEN]; |
| 355 | strcpy(newnew,basedir); |
| 356 | strcat(newnew,newname); |
| 357 | CROSS_FILENAME(newnew); |
| 358 | int temp=rename(newold,dirCache.GetExpandName(newnew)); |
| 359 | if (temp==0) dirCache.CacheOut(newnew); |
| 360 | return (temp==0); |
| 361 | |
| 362 | } |
| 363 | |
| 364 | bool localDrive::AllocationInfo(Bit16u * _bytes_sector,Bit8u * _sectors_cluster,Bit16u * _total_clusters,Bit16u * _free_clusters) { |
| 365 | /* Always report 100 mb free should be enough */ |
| 366 | /* Total size is always 1 gb */ |
| 367 | *_bytes_sector=allocation.bytes_sector; |
| 368 | *_sectors_cluster=allocation.sectors_cluster; |
| 369 | *_total_clusters=allocation.total_clusters; |
| 370 | *_free_clusters=allocation.free_clusters; |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | bool localDrive::FileExists(const char* name) { |
| 375 | char newname[CROSS_LEN]; |
| 376 | strcpy(newname,basedir); |
| 377 | strcat(newname,name); |
| 378 | CROSS_FILENAME(newname); |
| 379 | dirCache.ExpandName(newname); |
| 380 | FILE* Temp=fopen(newname,"rb"); //No reading done, so no wrapping for 0.74-3 (later code uses different calls) |
| 381 | if(Temp==NULL) return false; |
| 382 | fclose(Temp); |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | bool localDrive::FileStat(const char* name, FileStat_Block * const stat_block) { |
| 387 | char newname[CROSS_LEN]; |
| 388 | strcpy(newname,basedir); |
| 389 | strcat(newname,name); |
| 390 | CROSS_FILENAME(newname); |
| 391 | dirCache.ExpandName(newname); |
| 392 | struct stat temp_stat; |
| 393 | if(stat(newname,&temp_stat)!=0) return false; |
| 394 | /* Convert the stat to a FileStat */ |
| 395 | struct tm *time; |
| 396 | if((time=localtime(&temp_stat.st_mtime))!=0) { |
| 397 | stat_block->time=DOS_PackTime((Bit16u)time->tm_hour,(Bit16u)time->tm_min,(Bit16u)time->tm_sec); |
| 398 | stat_block->date=DOS_PackDate((Bit16u)(time->tm_year+1900),(Bit16u)(time->tm_mon+1),(Bit16u)time->tm_mday); |
| 399 | } else { |
| 400 | |
| 401 | } |
| 402 | stat_block->size=(Bit32u)temp_stat.st_size; |
| 403 | return true; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | Bit8u localDrive::GetMediaByte(void) { |
| 408 | return allocation.mediaid; |
| 409 | } |
| 410 | |
| 411 | bool localDrive::isRemote(void) { |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | bool localDrive::isRemovable(void) { |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | Bits localDrive::UnMount(void) { |
| 420 | delete this; |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | localDrive::localDrive(const char * startdir,Bit16u _bytes_sector,Bit8u _sectors_cluster,Bit16u _total_clusters,Bit16u _free_clusters,Bit8u _mediaid) { |
| 425 | strcpy(basedir,startdir); |
| 426 | sprintf(info,"local directory %s",startdir); |
| 427 | allocation.bytes_sector=_bytes_sector; |
| 428 | allocation.sectors_cluster=_sectors_cluster; |
| 429 | allocation.total_clusters=_total_clusters; |
| 430 | allocation.free_clusters=_free_clusters; |
| 431 | allocation.mediaid=_mediaid; |
| 432 | |
| 433 | dirCache.SetBaseDir(basedir); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | //TODO Maybe use fflush, but that seemed to fuck up in visual c |
| 438 | bool localFile::Read(Bit8u * data,Bit16u * size) { |
| 439 | if ((this->flags & 0xf) == OPEN_WRITE) { // check if file opened in write-only mode |
| 440 | DOS_SetError(DOSERR_ACCESS_DENIED); |
| 441 | return false; |
| 442 | } |
| 443 | if (last_action==WRITE) fseek(fhandle,ftell(fhandle),SEEK_SET); |
| 444 | last_action=READ; |
| 445 | *size=(Bit16u)fread(data,1,*size,fhandle); |
| 446 | /* Fake harddrive motion. Inspector Gadget with soundblaster compatible */ |
| 447 | /* Same for Igor */ |
| 448 | /* hardrive motion => unmask irq 2. Only do it when it's masked as unmasking is realitively heavy to emulate */ |
| 449 | Bit8u mask = IO_Read(0x21); |
| 450 | if(mask & 0x4 ) IO_Write(0x21,mask&0xfb); |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | bool localFile::Write(Bit8u * data,Bit16u * size) { |
| 455 | if ((this->flags & 0xf) == OPEN_READ) { // check if file opened in read-only mode |
| 456 | DOS_SetError(DOSERR_ACCESS_DENIED); |
| 457 | return false; |
| 458 | } |
| 459 | if (last_action==READ) fseek(fhandle,ftell(fhandle),SEEK_SET); |
| 460 | last_action=WRITE; |
| 461 | if(*size==0){ |
| 462 | return (!ftruncate(fileno(fhandle),ftell(fhandle))); |
| 463 | } |
| 464 | else |
| 465 | { |
| 466 | *size=(Bit16u)fwrite(data,1,*size,fhandle); |
| 467 | return true; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | bool localFile::Seek(Bit32u * pos,Bit32u type) { |
| 472 | int seektype; |
| 473 | switch (type) { |
| 474 | case DOS_SEEK_SET:seektype=SEEK_SET;break; |
| 475 | case DOS_SEEK_CUR:seektype=SEEK_CUR;break; |
| 476 | case DOS_SEEK_END:seektype=SEEK_END;break; |
| 477 | default: |
| 478 | //TODO Give some doserrorcode; |
| 479 | return false;//ERROR |
| 480 | } |
| 481 | int ret=fseek(fhandle,*reinterpret_cast<Bit32s*>(pos),seektype); |
| 482 | if (ret!=0) { |
| 483 | // Out of file range, pretend everythings ok |
| 484 | // and move file pointer top end of file... ?! (Black Thorne) |
| 485 | fseek(fhandle,0,SEEK_END); |
| 486 | }; |
| 487 | #if 0 |
| 488 | fpos_t temppos; |
| 489 | fgetpos(fhandle,&temppos); |
| 490 | Bit32u * fake_pos=(Bit32u*)&temppos; |
| 491 | *pos=*fake_pos; |
| 492 | #endif |
| 493 | *pos=(Bit32u)ftell(fhandle); |
| 494 | last_action=NONE; |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | bool localFile::Close() { |
| 499 | // only close if one reference left |
| 500 | if (refCtr==1) { |
| 501 | if(fhandle) fclose(fhandle); |
| 502 | fhandle = 0; |
| 503 | open = false; |
| 504 | }; |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | Bit16u localFile::GetInformation(void) { |
| 509 | return read_only_medium?0x40:0; |
| 510 | } |
| 511 | |
| 512 | |
| 513 | localFile::localFile(const char* _name, FILE * handle) { |
| 514 | fhandle=handle; |
| 515 | open=true; |
| 516 | UpdateDateTimeFromHost(); |
| 517 | |
| 518 | attr=DOS_ATTR_ARCHIVE; |
| 519 | last_action=NONE; |
| 520 | read_only_medium=false; |
| 521 | |
| 522 | name=0; |
| 523 | SetName(_name); |
| 524 | } |
| 525 | |
| 526 | void localFile::FlagReadOnlyMedium(void) { |
| 527 | read_only_medium = true; |
| 528 | } |
| 529 | |
| 530 | bool localFile::UpdateDateTimeFromHost(void) { |
| 531 | if(!open) return false; |
| 532 | struct stat temp_stat; |
| 533 | fstat(fileno(fhandle),&temp_stat); |
| 534 | struct tm * ltime; |
| 535 | if((ltime=localtime(&temp_stat.st_mtime))!=0) { |
| 536 | time=DOS_PackTime((Bit16u)ltime->tm_hour,(Bit16u)ltime->tm_min,(Bit16u)ltime->tm_sec); |
| 537 | date=DOS_PackDate((Bit16u)(ltime->tm_year+1900),(Bit16u)(ltime->tm_mon+1),(Bit16u)ltime->tm_mday); |
| 538 | } else { |
| 539 | time=1;date=1; |
| 540 | } |
| 541 | return true; |
| 542 | } |
| 543 | |
| 544 | |
| 545 | // ******************************************** |
| 546 | // CDROM DRIVE |
| 547 | // ******************************************** |
| 548 | |
| 549 | int MSCDEX_RemoveDrive(char driveLetter); |
| 550 | int MSCDEX_AddDrive(char driveLetter, const char* physicalPath, Bit8u& subUnit); |
| 551 | bool MSCDEX_HasMediaChanged(Bit8u subUnit); |
| 552 | bool MSCDEX_GetVolumeName(Bit8u subUnit, char* name); |
| 553 | |
| 554 | |
| 555 | cdromDrive::cdromDrive(const char driveLetter, const char * startdir,Bit16u _bytes_sector,Bit8u _sectors_cluster,Bit16u _total_clusters,Bit16u _free_clusters,Bit8u _mediaid, int& error) |
| 556 | :localDrive(startdir,_bytes_sector,_sectors_cluster,_total_clusters,_free_clusters,_mediaid), |
| 557 | subUnit(0), |
| 558 | driveLetter('\0') |
| 559 | { |
| 560 | // Init mscdex |
| 561 | error = MSCDEX_AddDrive(driveLetter,startdir,subUnit); |
| 562 | strcpy(info, "CDRom "); |
| 563 | strcat(info, startdir); |
| 564 | this->driveLetter = driveLetter; |
| 565 | // Get Volume Label |
| 566 | char name[32]; |
| 567 | if (MSCDEX_GetVolumeName(subUnit,name)) dirCache.SetLabel(name,true,true); |
| 568 | } |
| 569 | |
| 570 | bool cdromDrive::FileOpen(DOS_File * * file,char * name,Bit32u flags) { |
| 571 | if ((flags&0xf)==OPEN_READWRITE) { |
| 572 | flags &= ~OPEN_READWRITE; |
| 573 | } else if ((flags&0xf)==OPEN_WRITE) { |
| 574 | DOS_SetError(DOSERR_ACCESS_DENIED); |
| 575 | return false; |
| 576 | } |
| 577 | bool retcode = localDrive::FileOpen(file,name,flags); |
| 578 | if(retcode) (dynamic_cast<localFile*>(*file))->FlagReadOnlyMedium(); |
| 579 | return retcode; |
| 580 | } |
| 581 | |
| 582 | bool cdromDrive::FileCreate(DOS_File * * /*file*/,char * /*name*/,Bit16u /*attributes*/) { |
| 583 | DOS_SetError(DOSERR_ACCESS_DENIED); |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | bool cdromDrive::FileUnlink(char * /*name*/) { |
| 588 | DOS_SetError(DOSERR_ACCESS_DENIED); |
| 589 | return false; |
| 590 | } |
| 591 | |
| 592 | bool cdromDrive::RemoveDir(char * /*dir*/) { |
| 593 | DOS_SetError(DOSERR_ACCESS_DENIED); |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | bool cdromDrive::MakeDir(char * /*dir*/) { |
| 598 | DOS_SetError(DOSERR_ACCESS_DENIED); |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | bool cdromDrive::Rename(char * /*oldname*/,char * /*newname*/) { |
| 603 | DOS_SetError(DOSERR_ACCESS_DENIED); |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | bool cdromDrive::GetFileAttr(char * name,Bit16u * attr) { |
| 608 | bool result = localDrive::GetFileAttr(name,attr); |
| 609 | if (result) *attr |= DOS_ATTR_READ_ONLY; |
| 610 | return result; |
| 611 | } |
| 612 | |
| 613 | bool cdromDrive::FindFirst(char * _dir,DOS_DTA & dta,bool /*fcb_findfirst*/) { |
| 614 | // If media has changed, reInit drivecache. |
| 615 | if (MSCDEX_HasMediaChanged(subUnit)) { |
| 616 | dirCache.EmptyCache(); |
| 617 | // Get Volume Label |
| 618 | char name[32]; |
| 619 | if (MSCDEX_GetVolumeName(subUnit,name)) dirCache.SetLabel(name,true,true); |
| 620 | } |
| 621 | return localDrive::FindFirst(_dir,dta); |
| 622 | } |
| 623 | |
| 624 | void cdromDrive::SetDir(const char* path) { |
| 625 | // If media has changed, reInit drivecache. |
| 626 | if (MSCDEX_HasMediaChanged(subUnit)) { |
| 627 | dirCache.EmptyCache(); |
| 628 | // Get Volume Label |
| 629 | char name[32]; |
| 630 | if (MSCDEX_GetVolumeName(subUnit,name)) dirCache.SetLabel(name,true,true); |
| 631 | } |
| 632 | localDrive::SetDir(path); |
| 633 | } |
| 634 | |
| 635 | bool cdromDrive::isRemote(void) { |
| 636 | return true; |
| 637 | } |
| 638 | |
| 639 | bool cdromDrive::isRemovable(void) { |
| 640 | return true; |
| 641 | } |
| 642 | |
| 643 | Bits cdromDrive::UnMount(void) { |
| 644 | if(MSCDEX_RemoveDrive(driveLetter)) { |
| 645 | delete this; |
| 646 | return 0; |
| 647 | } |
| 648 | return 2; |
| 649 | } |