/[rdesktop]/sourceforge.net/trunk/rdesktop/disk.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sourceforge.net/trunk/rdesktop/disk.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 597 by n-ki, Thu Feb 5 15:41:56 2004 UTC revision 613 by n-ki, Mon Feb 23 10:34:18 2004 UTC
# Line 84  Line 84 
84  #define SOLARIS  #define SOLARIS
85  #endif  #endif
86    
87  #ifdef SOLARIS  #if (defined(SOLARIS) || defined(__hpux))
88  #define DIRFD(a) ((a)->dd_fd)  #define DIRFD(a) ((a)->dd_fd)
89  #else  #else
90  #define DIRFD(a) (dirfd(a))  #define DIRFD(a) (dirfd(a))
# Line 98  Line 98 
98  #include <fnmatch.h>  #include <fnmatch.h>
99  #include <errno.h>              /* errno */  #include <errno.h>              /* errno */
100    
101  #if defined(SOLARIS)  #include <utime.h>
102    #include <time.h>               /* ctime */
103    
104    
105    #if (defined(SOLARIS) || defined (__hpux) || defined(__BEOS__))
106  #include <sys/statvfs.h>        /* solaris statvfs */  #include <sys/statvfs.h>        /* solaris statvfs */
107  #define STATFS_FN(path, buf) (statvfs(path,buf))  #define STATFS_FN(path, buf) (statvfs(path,buf))
108  #define STATFS_T statvfs  #define STATFS_T statvfs
# Line 133  struct fileinfo Line 137  struct fileinfo
137  }  }
138  g_fileinfo[MAX_OPEN_FILES];  g_fileinfo[MAX_OPEN_FILES];
139    
140    time_t
141    get_create_time(struct stat *st)
142    {
143            time_t ret, ret1;
144    
145            ret = MIN(st->st_ctime, st->st_mtime);
146            ret1 = MIN(ret, st->st_atime);
147    
148            if (ret1 != (time_t) 0)
149                    return ret1;
150    
151            return ret;
152    }
153    
154  /* Convert seconds since 1970 to a filetime */  /* Convert seconds since 1970 to a filetime */
155  void  void
156  seconds_since_1970_to_filetime(time_t seconds, uint32 * high, uint32 * low)  seconds_since_1970_to_filetime(time_t seconds, uint32 * high, uint32 * low)
# Line 144  seconds_since_1970_to_filetime(time_t se Line 162  seconds_since_1970_to_filetime(time_t se
162          *high = (uint32) (ticks >> 32);          *high = (uint32) (ticks >> 32);
163  }  }
164    
165    /* Convert seconds since 1970 back to filetime */
166    time_t
167    convert_1970_to_filetime(uint32 high, uint32 low)
168    {
169            unsigned long long ticks;
170            time_t val;
171    
172            ticks = low + (((unsigned long long) high) << 32);
173            ticks /= 10000000;
174            ticks -= 11644473600LL;
175    
176            val = (time_t) ticks;
177            return (val);
178    
179    }
180    
181    
182  /* Enumeration of devices from rdesktop.c        */  /* Enumeration of devices from rdesktop.c        */
183  /* returns numer of units found and initialized. */  /* returns numer of units found and initialized. */
184  /* optarg looks like ':h:=/mnt/floppy,b:=/mnt/usbdevice1' */  /* optarg looks like ':h=/mnt/floppy,b=/mnt/usbdevice1' */
185  /* when it arrives to this function.             */  /* when it arrives to this function.             */
186  int  int
187  disk_enum_devices(int *id, char *optarg)  disk_enum_devices(uint32 * id, char *optarg)
188  {  {
189          char *pos = optarg;          char *pos = optarg;
190          char *pos2;          char *pos2;
# Line 188  disk_create(uint32 device_id, uint32 acc Line 223  disk_create(uint32 device_id, uint32 acc
223          DIR *dirp;          DIR *dirp;
224          int flags, mode;          int flags, mode;
225          char path[256];          char path[256];
226            struct stat filestat;
227    
228          handle = 0;          handle = 0;
229          dirp = NULL;          dirp = NULL;
230          flags = 0;          flags = 0;
231          mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;          mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
232    
233    
234          if (filename[strlen(filename) - 1] == '/')          if (filename[strlen(filename) - 1] == '/')
235                  filename[strlen(filename) - 1] = 0;                  filename[strlen(filename) - 1] = 0;
236          sprintf(path, "%s%s", g_rdpdr_device[device_id].local_path, filename);          sprintf(path, "%s%s", g_rdpdr_device[device_id].local_path, filename);
# Line 233  disk_create(uint32 device_id, uint32 acc Line 270  disk_create(uint32 device_id, uint32 acc
270    
271          //printf("Open: \"%s\"  flags: %u, accessmask: %u sharemode: %u create disp: %u\n", path, flags_and_attributes, accessmask, sharemode, create_disposition);          //printf("Open: \"%s\"  flags: %u, accessmask: %u sharemode: %u create disp: %u\n", path, flags_and_attributes, accessmask, sharemode, create_disposition);
272    
         /* since we can't trust the FILE_DIRECTORY_FILE flag */  
         /* we need to double check that the file isn't a dir */  
         struct stat filestat;  
   
273          // Get information about file and set that flag ourselfs          // Get information about file and set that flag ourselfs
274          if ((stat(path, &filestat) == 0) && (S_ISDIR(filestat.st_mode)))          if ((stat(path, &filestat) == 0) && (S_ISDIR(filestat.st_mode)))
275                  flags_and_attributes |= FILE_DIRECTORY_FILE;          {
276                    if (flags_and_attributes & FILE_NON_DIRECTORY_FILE)
277                            return STATUS_FILE_IS_A_DIRECTORY;
278                    else
279                            flags_and_attributes |= FILE_DIRECTORY_FILE;
280            }
281    
282          if (flags_and_attributes & FILE_DIRECTORY_FILE)          if (flags_and_attributes & FILE_DIRECTORY_FILE)
283          {          {
# Line 291  disk_create(uint32 device_id, uint32 acc Line 329  disk_create(uint32 device_id, uint32 acc
329                  {                  {
330                          switch (errno)                          switch (errno)
331                          {                          {
332                                    case EISDIR:
333    
334                                            return STATUS_FILE_IS_A_DIRECTORY;
335    
336                                  case EACCES:                                  case EACCES:
337    
338                                          return STATUS_ACCESS_DENIED;                                          return STATUS_ACCESS_DENIED;
# Line 298  disk_create(uint32 device_id, uint32 acc Line 340  disk_create(uint32 device_id, uint32 acc
340                                  case ENOENT:                                  case ENOENT:
341    
342                                          return STATUS_NO_SUCH_FILE;                                          return STATUS_NO_SUCH_FILE;
343                                    case EEXIST:
344    
345                                            return STATUS_OBJECT_NAME_COLLISION;
346                                  default:                                  default:
347    
348                                          perror("open");                                          perror("open");
# Line 352  disk_read(HANDLE handle, uint8 * data, u Line 397  disk_read(HANDLE handle, uint8 * data, u
397  {  {
398          int n;          int n;
399    
400    #if 0
401          /* browsing dir ????        */          /* browsing dir ????        */
402          /* each request is 24 bytes */          /* each request is 24 bytes */
403          if (g_fileinfo[handle].flags_and_attributes & FILE_DIRECTORY_FILE)          if (g_fileinfo[handle].flags_and_attributes & FILE_DIRECTORY_FILE)
# Line 359  disk_read(HANDLE handle, uint8 * data, u Line 405  disk_read(HANDLE handle, uint8 * data, u
405                  *result = 0;                  *result = 0;
406                  return STATUS_SUCCESS;                  return STATUS_SUCCESS;
407          }          }
408    #endif
409    
410            lseek(handle, offset, SEEK_SET);
411    
         if (offset)  
                 lseek(handle, offset, SEEK_SET);  
412          n = read(handle, data, length);          n = read(handle, data, length);
413    
414          if (n < 0)          if (n < 0)
415          {          {
                 perror("read");  
416                  *result = 0;                  *result = 0;
417                  return STATUS_INVALID_PARAMETER;                  switch (errno)
418                    {
419                            case EISDIR:
420                                    return STATUS_FILE_IS_A_DIRECTORY;
421                            default:
422                                    perror("read");
423                                    return STATUS_INVALID_PARAMETER;
424                    }
425          }          }
426    
427          *result = n;          *result = n;
# Line 381  disk_write(HANDLE handle, uint8 * data, Line 434  disk_write(HANDLE handle, uint8 * data,
434  {  {
435          int n;          int n;
436    
437          if (offset)          lseek(handle, offset, SEEK_SET);
                 lseek(handle, offset, SEEK_SET);  
438    
439          n = write(handle, data, length);          n = write(handle, data, length);
440    
# Line 390  disk_write(HANDLE handle, uint8 * data, Line 442  disk_write(HANDLE handle, uint8 * data,
442          {          {
443                  perror("write");                  perror("write");
444                  *result = 0;                  *result = 0;
445                  return STATUS_ACCESS_DENIED;                  switch (errno)
446                    {
447                            case ENOSPC:
448                                    return STATUS_DISK_FULL;
449                            default:
450                                    return STATUS_ACCESS_DENIED;
451                    }
452          }          }
453    
454          *result = n;          *result = n;
# Line 418  disk_query_information(HANDLE handle, ui Line 476  disk_query_information(HANDLE handle, ui
476          // Set file attributes          // Set file attributes
477          file_attributes = 0;          file_attributes = 0;
478          if (S_ISDIR(filestat.st_mode))          if (S_ISDIR(filestat.st_mode))
         {  
479                  file_attributes |= FILE_ATTRIBUTE_DIRECTORY;                  file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
480          }  
481          filename = 1 + strrchr(path, '/');          filename = 1 + strrchr(path, '/');
482          if (filename && filename[0] == '.')          if (filename && filename[0] == '.')
         {  
483                  file_attributes |= FILE_ATTRIBUTE_HIDDEN;                  file_attributes |= FILE_ATTRIBUTE_HIDDEN;
484          }  
485            if (!file_attributes)
486                    file_attributes |= FILE_ATTRIBUTE_NORMAL;
487    
488            if (!(filestat.st_mode & S_IWUSR))
489                    file_attributes |= FILE_ATTRIBUTE_READONLY;
490    
491          // Return requested data          // Return requested data
492          switch (info_class)          switch (info_class)
493          {          {
494                  case 4: /* FileBasicInformation */                  case 4: /* FileBasicInformation */
495                            seconds_since_1970_to_filetime(get_create_time(&filestat), &ft_high,
496                          out_uint8s(out, 8);     //create_time not available;                                                         &ft_low);
497                            out_uint32_le(out, ft_low);     //create_access_time
498                            out_uint32_le(out, ft_high);
499    
500                          seconds_since_1970_to_filetime(filestat.st_atime, &ft_high, &ft_low);                          seconds_since_1970_to_filetime(filestat.st_atime, &ft_high, &ft_low);
501                          out_uint32_le(out, ft_low);     //last_access_time                          out_uint32_le(out, ft_low);     //last_access_time
# Line 442  disk_query_information(HANDLE handle, ui Line 505  disk_query_information(HANDLE handle, ui
505                          out_uint32_le(out, ft_low);     //last_write_time                          out_uint32_le(out, ft_low);     //last_write_time
506                          out_uint32_le(out, ft_high);                          out_uint32_le(out, ft_high);
507    
508                          out_uint8s(out, 8);     //unknown zero                          seconds_since_1970_to_filetime(filestat.st_ctime, &ft_high, &ft_low);
509                            out_uint32_le(out, ft_low);     //last_change_time
510                            out_uint32_le(out, ft_high);
511    
512                          out_uint32_le(out, file_attributes);                          out_uint32_le(out, file_attributes);
513                          break;                          break;
514    
# Line 478  disk_set_information(HANDLE handle, uint Line 544  disk_set_information(HANDLE handle, uint
544          char newname[256], fullpath[256];          char newname[256], fullpath[256];
545          struct fileinfo *pfinfo;          struct fileinfo *pfinfo;
546    
547            int mode;
548            struct stat filestat;
549            time_t write_time, change_time, access_time, mod_time;
550            struct utimbuf tvs;
551            struct STATFS_T stat_fs;
552    
553          pfinfo = &(g_fileinfo[handle]);          pfinfo = &(g_fileinfo[handle]);
554    
555          switch (info_class)          switch (info_class)
556          {          {
557                  case 4: /* FileBasicInformation */                  case 4: /* FileBasicInformation */
558                            write_time = change_time = access_time = 0;
559    
560                            in_uint8s(in, 4);       /* Handle of root dir? */
561                            in_uint8s(in, 24);      /* unknown */
562    
563                            // CreationTime
564                            in_uint32_le(in, ft_low);
565                            in_uint32_le(in, ft_high);
566    
567                            // AccessTime
568                            in_uint32_le(in, ft_low);
569                            in_uint32_le(in, ft_high);
570                            if (ft_low || ft_high)
571                                    access_time = convert_1970_to_filetime(ft_high, ft_low);
572    
573                            // WriteTime
574                            in_uint32_le(in, ft_low);
575                            in_uint32_le(in, ft_high);
576                            if (ft_low || ft_high)
577                                    write_time = convert_1970_to_filetime(ft_high, ft_low);
578    
579                            // ChangeTime
580                            in_uint32_le(in, ft_low);
581                            in_uint32_le(in, ft_high);
582                            if (ft_low || ft_high)
583                                    change_time = convert_1970_to_filetime(ft_high, ft_low);
584    
585                            in_uint32_le(in, file_attributes);
586    
587                            if (fstat(handle, &filestat))
588                                    return STATUS_ACCESS_DENIED;
589    
590                            tvs.modtime = filestat.st_mtime;
591                            tvs.actime = filestat.st_atime;
592                            if (access_time)
593                                    tvs.actime = access_time;
594    
595    
596                            if (write_time || change_time)
597                                    mod_time = MIN(write_time, change_time);
598                            else
599                                    mod_time = write_time ? write_time : change_time;
600    
601                            if (mod_time)
602                                    tvs.modtime = mod_time;
603    
604    
605                            if (access_time || write_time || change_time)
606                            {
607    #if WITH_DEBUG_RDP5
608                                    printf("FileBasicInformation access       time %s",
609                                           ctime(&tvs.actime));
610                                    printf("FileBasicInformation modification time %s",
611                                           ctime(&tvs.modtime));
612    #endif
613                                    if (utime(pfinfo->path, &tvs))
614                                            return STATUS_ACCESS_DENIED;
615                            }
616    
617                            if (!file_attributes)
618                                    break;  // not valid
619    
620                            mode = filestat.st_mode;
621    
622                            if (file_attributes & FILE_ATTRIBUTE_READONLY)
623                                    mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
624                            else
625                                    mode |= S_IWUSR;
626    
627                            mode &= 0777;
628    #if WITH_DEBUG_RDP5
629                            printf("FileBasicInformation set access mode 0%o", mode);
630    #endif
631    
632                            if (fchmod(handle, mode))
633                                    return STATUS_ACCESS_DENIED;
634    
635                            /* prevents start of writing if not enough space left on device */
636                            if (STATFS_FN(g_rdpdr_device[pfinfo->device_id].local_path, &stat_fs) == 0)
637                                    if (stat_fs.f_bsize * stat_fs.f_bfree < length)
638                                            return STATUS_DISK_FULL;
639    
                         // Probably safe to ignore  
640                          break;                          break;
641    
642                  case 10:        /* FileRenameInformation */                  case 10:        /* FileRenameInformation */
# Line 516  disk_set_information(HANDLE handle, uint Line 668  disk_set_information(HANDLE handle, uint
668                  case 13:        /* FileDispositionInformation */                  case 13:        /* FileDispositionInformation */
669    
670                          //unimpl("IRP Set File Information class: FileDispositionInformation\n");                          //unimpl("IRP Set File Information class: FileDispositionInformation\n");
671                          // in_uint32_le(in, delete_on_close);  
672                            //in_uint32_le(in, delete_on_close);
673                          // disk_close(handle);                          // disk_close(handle);
674                          unlink(pfinfo->path);                          if ((pfinfo->flags_and_attributes & FILE_DIRECTORY_FILE))       // remove a directory
675                            {
676                                    if (rmdir(pfinfo->path) < 0)
677                                            return STATUS_ACCESS_DENIED;
678                            }
679                            else if (unlink(pfinfo->path) < 0)      // unlink a file
680                                    return STATUS_ACCESS_DENIED;
681    
682                          break;                          break;
683    
684                  case 19:        /* FileAllocationInformation */                  case 19:        /* FileAllocationInformation */
# Line 527  disk_set_information(HANDLE handle, uint Line 687  disk_set_information(HANDLE handle, uint
687                          break;                          break;
688    
689                  case 20:        /* FileEndOfFileInformation */                  case 20:        /* FileEndOfFileInformation */
690                            in_uint8s(in, 28);      /* unknown */
691                            in_uint32_le(in, length);       /* file size */
692    
693                            printf("FileEndOfFileInformation length = %d\n", length);
694                            // ????????????
695    
696                          unimpl("IRP Set File Information class: FileEndOfFileInformation\n");                          unimpl("IRP Set File Information class: FileEndOfFileInformation\n");
697                          break;                          break;
   
698                  default:                  default:
699    
700                          unimpl("IRP Set File Information class: 0x%x\n", info_class);                          unimpl("IRP Set File Information class: 0x%x\n", info_class);
# Line 550  disk_query_volume_information(HANDLE han Line 714  disk_query_volume_information(HANDLE han
714          volume = "RDESKTOP";          volume = "RDESKTOP";
715          fs_type = "RDPFS";          fs_type = "RDPFS";
716    
717          if (STATFS_FN(pfinfo->path, &stat_fs) != 0)     /* FIXME: statfs is not portable */          if (STATFS_FN(pfinfo->path, &stat_fs) != 0)
718          {          {
719                  perror("statfs");                  perror("statfs");
720                  return STATUS_ACCESS_DENIED;                  return STATUS_ACCESS_DENIED;
# Line 629  disk_query_directory(HANDLE handle, uint Line 793  disk_query_directory(HANDLE handle, uint
793                          // find next dirent matching pattern                          // find next dirent matching pattern
794                          pdirent = readdir(pdir);                          pdirent = readdir(pdir);
795                          while (pdirent && fnmatch(pfinfo->pattern, pdirent->d_name, 0) != 0)                          while (pdirent && fnmatch(pfinfo->pattern, pdirent->d_name, 0) != 0)
                         {  
796                                  pdirent = readdir(pdir);                                  pdirent = readdir(pdir);
                         }  
797    
798                          if (pdirent == NULL)                          if (pdirent == NULL)
                         {  
799                                  return STATUS_NO_MORE_FILES;                                  return STATUS_NO_MORE_FILES;
                         }  
800    
801                          // Get information for directory entry                          // Get information for directory entry
802                          sprintf(fullpath, "%s/%s", dirname, pdirent->d_name);                          sprintf(fullpath, "%s/%s", dirname, pdirent->d_name);
803    
804                          /* JIF                          /* JIF
805                             printf("Stat: %s\n", fullpath); */                             printf("Stat: %s\n", fullpath); */
806                          if (stat(fullpath, &fstat))                          if (stat(fullpath, &fstat))
# Line 653  disk_query_directory(HANDLE handle, uint Line 814  disk_query_directory(HANDLE handle, uint
814                                  file_attributes |= FILE_ATTRIBUTE_DIRECTORY;                                  file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
815                          if (pdirent->d_name[0] == '.')                          if (pdirent->d_name[0] == '.')
816                                  file_attributes |= FILE_ATTRIBUTE_HIDDEN;                                  file_attributes |= FILE_ATTRIBUTE_HIDDEN;
817                            if (!file_attributes)
818                                    file_attributes |= FILE_ATTRIBUTE_NORMAL;
819                            if (!(fstat.st_mode & S_IWUSR))
820                                    file_attributes |= FILE_ATTRIBUTE_READONLY;
821    
822                          // Return requested information                          // Return requested information
823                          out_uint8s(out, 8);     //unknown zero                          out_uint8s(out, 8);     //unknown zero
824                          out_uint8s(out, 8);     //create_time not available in posix;  
825                            seconds_since_1970_to_filetime(get_create_time(&fstat), &ft_high, &ft_low);
826                            out_uint32_le(out, ft_low);     // create time
827                            out_uint32_le(out, ft_high);
828    
829                          seconds_since_1970_to_filetime(fstat.st_atime, &ft_high, &ft_low);                          seconds_since_1970_to_filetime(fstat.st_atime, &ft_high, &ft_low);
830                          out_uint32_le(out, ft_low);     //last_access_time                          out_uint32_le(out, ft_low);     //last_access_time
# Line 666  disk_query_directory(HANDLE handle, uint Line 834  disk_query_directory(HANDLE handle, uint
834                          out_uint32_le(out, ft_low);     //last_write_time                          out_uint32_le(out, ft_low);     //last_write_time
835                          out_uint32_le(out, ft_high);                          out_uint32_le(out, ft_high);
836    
837                          out_uint8s(out, 8);     //unknown zero                          seconds_since_1970_to_filetime(fstat.st_ctime, &ft_high, &ft_low);
838                            out_uint32_le(out, ft_low);     //change_write_time
839                            out_uint32_le(out, ft_high);
840    
841                          out_uint32_le(out, fstat.st_size);      //filesize low                          out_uint32_le(out, fstat.st_size);      //filesize low
842                          out_uint32_le(out, 0);  //filesize high                          out_uint32_le(out, 0);  //filesize high
843                          out_uint32_le(out, fstat.st_size);      //filesize low                          out_uint32_le(out, fstat.st_size);      //filesize low
# Line 688  disk_query_directory(HANDLE handle, uint Line 859  disk_query_directory(HANDLE handle, uint
859          return STATUS_SUCCESS;          return STATUS_SUCCESS;
860  }  }
861    
862    
863    
864    static NTSTATUS
865    disk_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out)
866    {
867            uint32 result;
868    
869            if (((request >> 16) != 20) || ((request >> 16) != 9))
870                    return STATUS_INVALID_PARAMETER;
871    
872            /* extract operation */
873            request >>= 2;
874            request &= 0xfff;
875    
876            printf("DISK IOCTL %d\n", request);
877    
878            switch (request)
879            {
880                    case 25:        // ?
881                    case 42:        // ?
882                    default:
883                            unimpl("DISK IOCTL %d\n", request);
884                            return STATUS_INVALID_PARAMETER;
885            }
886    
887            return STATUS_SUCCESS;
888    }
889    
890  DEVICE_FNS disk_fns = {  DEVICE_FNS disk_fns = {
891          disk_create,          disk_create,
892          disk_close,          disk_close,
893          disk_read,          disk_read,
894          disk_write,          disk_write,
895          NULL                    /* device_control */          disk_device_control     /* device_control */
896  };  };

Legend:
Removed from v.597  
changed lines
  Added in v.613

  ViewVC Help
Powered by ViewVC 1.1.26