~ubuntu-branches/ubuntu/quantal/unrar-nonfree/quantal

« back to all changes in this revision

Viewing changes to filefn.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Meredith
  • Date: 2009-06-06 17:46:05 UTC
  • mto: (1.1.11 upstream) (5.1.5 sid)
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20090606174605-825ysfkrhw7wkc9e
ImportĀ upstreamĀ versionĀ 3.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "rar.hpp"
2
2
 
3
 
MKDIR_CODE MakeDir(const char *Name,const wchar *NameW,uint Attr)
 
3
MKDIR_CODE MakeDir(const char *Name,const wchar *NameW,bool SetAttr,uint Attr)
4
4
{
5
5
#ifdef _WIN_32
6
6
  int Success;
10
10
    Success=CreateDirectory(Name,NULL);
11
11
  if (Success)
12
12
  {
13
 
    SetFileAttr(Name,NameW,Attr);
 
13
    if (SetAttr)
 
14
      SetFileAttr(Name,NameW,Attr);
14
15
    return(MKDIR_SUCCESS);
15
16
  }
16
17
  int ErrCode=GetLastError();
25
26
  if (__mkdir(Name)==0)
26
27
#endif
27
28
  {
28
 
    SetFileAttr(Name,NameW,Attr);
 
29
    if (SetAttr)
 
30
      SetFileAttr(Name,NameW,Attr);
29
31
    return(MKDIR_SUCCESS);
30
32
  }
31
33
  return(errno==ENOENT ? MKDIR_BADPATH:MKDIR_ERROR);
32
34
#endif
33
35
#ifdef _UNIX
34
 
//  int prevmask=umask(0);
35
 
  int ErrCode=Name==NULL ? -1:mkdir(Name,(mode_t)Attr);
36
 
//  umask(prevmask);
 
36
  mode_t uattr=SetAttr ? (mode_t)Attr:0777;
 
37
  int ErrCode=Name==NULL ? -1:mkdir(Name,uattr);
37
38
  if (ErrCode==-1)
38
39
    return(errno==ENOENT ? MKDIR_BADPATH:MKDIR_ERROR);
39
40
  return(MKDIR_SUCCESS);
89
90
        strncpy(DirName,Path,s-Path);
90
91
        DirName[s-Path]=0;
91
92
      }
92
 
      if (MakeDir(DirName,DirPtrW,DirAttr)==MKDIR_SUCCESS)
 
93
      if (MakeDir(DirName,DirPtrW,true,DirAttr)==MKDIR_SUCCESS)
93
94
      {
94
95
#ifndef GUI
95
96
        mprintf(St(MCreatDir),DirName);
103
104
      s=charnext(s);
104
105
  }
105
106
  if (!SkipLastName && !IsPathDiv(*PointToLastChar(Path)))
106
 
    if (MakeDir(Path,PathW,DirAttr)!=MKDIR_SUCCESS)
 
107
    if (MakeDir(Path,PathW,true,DirAttr)!=MKDIR_SUCCESS)
107
108
      Success=false;
108
109
  return(Success);
109
110
}
165
166
 
166
167
 
167
168
#ifndef SFX_MODULE
168
 
Int64 GetFreeDisk(const char *Name)
 
169
int64 GetFreeDisk(const char *Name)
169
170
{
170
171
#ifdef _WIN_32
171
172
  char Root[NM];
189
190
    uiUserFree.u.LowPart=uiUserFree.u.HighPart=0;
190
191
    if (pGetDiskFreeSpaceEx(*Root ? Root:NULL,&uiUserFree,&uiTotalSize,&uiTotalFree) &&
191
192
        uiUserFree.u.HighPart<=uiTotalFree.u.HighPart)
192
 
      return(int32to64(uiUserFree.u.HighPart,uiUserFree.u.LowPart));
 
193
      return(INT32TO64(uiUserFree.u.HighPart,uiUserFree.u.LowPart));
193
194
  }
194
195
 
 
196
  // We are here if we failed to load GetDiskFreeSpaceExA.
195
197
  DWORD SectorsPerCluster,BytesPerSector,FreeClusters,TotalClusters;
196
198
  if (!GetDiskFreeSpace(*Root ? Root:NULL,&SectorsPerCluster,&BytesPerSector,&FreeClusters,&TotalClusters))
197
199
    return(1457664);
198
 
  Int64 FreeSize=SectorsPerCluster*BytesPerSector;
 
200
  int64 FreeSize=SectorsPerCluster*BytesPerSector;
199
201
  FreeSize=FreeSize*FreeClusters;
200
202
  return(FreeSize);
201
203
#elif defined(_BEOS)
207
209
  fs_info Info;
208
210
  if (fs_stat_dev(Dev,&Info)!=0)
209
211
    return(1457664);
210
 
  Int64 FreeSize=Info.block_size;
 
212
  int64 FreeSize=Info.block_size;
211
213
  FreeSize=FreeSize*Info.free_blocks;
212
214
  return(FreeSize);
213
215
#elif defined(_UNIX)
220
222
    FSALLOCATE fsa;
221
223
    if (DosQueryFSInfo(Drive,1,&fsa,sizeof(fsa))!=0)
222
224
      return(1457664);
223
 
    Int64 FreeSize=fsa.cSectorUnit*fsa.cbSector;
 
225
    int64 FreeSize=fsa.cSectorUnit*fsa.cbSector;
224
226
    FreeSize=FreeSize*fsa.cUnitAvail;
225
227
    return(FreeSize);
226
228
  }
238
240
#endif
239
241
    if (outregs.x.ax==0xffff)
240
242
      return(1457664);
241
 
    Int64 FreeSize=outregs.x.ax*outregs.x.cx;
 
243
    int64 FreeSize=outregs.x.ax*outregs.x.cx;
242
244
    FreeSize=FreeSize*outregs.x.bx;
243
245
    return(FreeSize);
244
246
  }
405
407
    strcpy(FullName,Src);
406
408
  else
407
409
  {
408
 
    getcwd(FullName,sizeof(FullName));
409
 
    AddEndSlash(FullName);
 
410
    if (getcwd(FullName,sizeof(FullName))==NULL)
 
411
      *FullName=0;
 
412
    else
 
413
      AddEndSlash(FullName);
410
414
    strcat(FullName,Src);
411
415
  }
412
416
  strcpy(Dest,FullName);
479
483
 
480
484
 
481
485
#ifndef SFX_MODULE
482
 
uint CalcFileCRC(File *SrcFile,Int64 Size,CALCCRC_SHOWMODE ShowMode)
 
486
uint CalcFileCRC(File *SrcFile,int64 Size,CALCCRC_SHOWMODE ShowMode)
483
487
{
484
488
  SaveFilePos SavePos(*SrcFile);
485
 
  const int BufSize=0x10000;
 
489
  const size_t BufSize=0x10000;
486
490
  Array<byte> Data(BufSize);
487
 
  Int64 BlockCount=0;
 
491
  int64 BlockCount=0;
488
492
  uint DataCRC=0xffffffff;
489
 
  int ReadSize;
490
493
 
491
494
#if !defined(SILENT) && !defined(_WIN_CE)
492
 
  Int64 FileLength=SrcFile->FileLength();
 
495
  int64 FileLength=SrcFile->FileLength();
493
496
  if (ShowMode!=CALCCRC_SHOWNONE)
494
497
  {
495
498
    mprintf(St(MCalcCRC));
499
502
#endif
500
503
 
501
504
  SrcFile->Seek(0,SEEK_SET);
502
 
  while ((ReadSize=SrcFile->Read(&Data[0],int64to32(Size==INT64ERR ? Int64(BufSize):Min(Int64(BufSize),Size))))!=0)
 
505
  while (true)
503
506
  {
 
507
    size_t SizeToRead;
 
508
    if (Size==INT64NDF)   // If we process the entire file.
 
509
      SizeToRead=BufSize; // Then always attempt to read the entire buffer.
 
510
    else
 
511
      SizeToRead=(size_t)Min((int64)BufSize,Size);
 
512
    int ReadSize=SrcFile->Read(&Data[0],SizeToRead);
 
513
    if (ReadSize==0)
 
514
      break;
 
515
 
504
516
    ++BlockCount;
505
517
    if ((BlockCount & 15)==0)
506
518
    {
507
519
#if !defined(SILENT) && !defined(_WIN_CE)
508
520
      if (ShowMode==CALCCRC_SHOWALL)
509
 
        mprintf("\b\b\b\b%3d%%",ToPercent(BlockCount*Int64(BufSize),FileLength));
 
521
        mprintf("\b\b\b\b%3d%%",ToPercent(BlockCount*int64(BufSize),FileLength));
510
522
#endif
511
523
      Wait();
512
524
    }
513
525
    DataCRC=CRC(DataCRC,&Data[0],ReadSize);
514
 
    if (Size!=INT64ERR)
 
526
    if (Size!=INT64NDF)
515
527
      Size-=ReadSize;
516
528
  }
517
529
#if !defined(SILENT) && !defined(_WIN_CE)
541
553
}
542
554
 
543
555
 
544
 
bool DelDir(const char *Name)
545
 
{
546
 
  return(DelDir(Name,NULL));
547
 
}
548
 
 
549
 
 
550
 
bool DelDir(const char *Name,const wchar *NameW)
551
 
{
552
 
  return(rmdir(Name)==0);
553
 
}
 
556
 
 
557
 
554
558
 
555
559
 
556
560
#if defined(_WIN_32) && !defined(_WIN_CE) && !defined(SFX_MODULE)
577
581
 
578
582
 
579
583
 
 
584
 
 
585