/[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 599 by n-ki, Thu Feb 5 15:41:56 2004 UTC revision 600 by n-ki, Fri Feb 6 10:41:34 2004 UTC
# Line 98  Line 98 
98  #include <fnmatch.h>  #include <fnmatch.h>
99  #include <errno.h>              /* errno */  #include <errno.h>              /* errno */
100    
101    #include <utime.h>
102    #include <time.h>               /* ctime */
103    
104    
105  #if defined(SOLARIS)  #if defined(SOLARIS)
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))
# Line 133  struct fileinfo Line 137  struct fileinfo
137  }  }
138  g_fileinfo[MAX_OPEN_FILES];  g_fileinfo[MAX_OPEN_FILES];
139    
140    
141    time_t
142    get_create_time(struct stat *st)
143    {
144            time_t ret, ret1;
145    
146            ret = MIN(st->st_ctime, st->st_mtime);
147            ret1 = MIN(ret, st->st_atime);
148    
149            if (ret1 != (time_t) 0)
150                    return ret1;
151    
152            return ret;
153    }
154    
155  /* Convert seconds since 1970 to a filetime */  /* Convert seconds since 1970 to a filetime */
156  void  void
157  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 163  seconds_since_1970_to_filetime(time_t se
163          *high = (uint32) (ticks >> 32);          *high = (uint32) (ticks >> 32);
164  }  }
165    
166    /* Convert seconds since 1970 back to filetime */
167    time_t
168    convert_1970_to_filetime(uint32 high, uint32 low)
169    {
170            unsigned long long ticks;
171            time_t val;
172    
173            ticks = low + (((unsigned long long) high) << 32);
174            ticks /= 10000000;
175            ticks -= 11644473600LL;
176    
177            val = (time_t) ticks;
178            return (val);
179    
180    }
181    
182    
183  /* Enumeration of devices from rdesktop.c        */  /* Enumeration of devices from rdesktop.c        */
184  /* returns numer of units found and initialized. */  /* returns numer of units found and initialized. */
185  /* optarg looks like ':h:=/mnt/floppy,b:=/mnt/usbdevice1' */  /* optarg looks like ':h:=/mnt/floppy,b:=/mnt/usbdevice1' */
# Line 188  disk_create(uint32 device_id, uint32 acc Line 224  disk_create(uint32 device_id, uint32 acc
224          DIR *dirp;          DIR *dirp;
225          int flags, mode;          int flags, mode;
226          char path[256];          char path[256];
227            struct stat filestat;
228    
229          handle = 0;          handle = 0;
230          dirp = NULL;          dirp = NULL;
231          flags = 0;          flags = 0;
232          mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;          mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
233    
234    
235          if (filename[strlen(filename) - 1] == '/')          if (filename[strlen(filename) - 1] == '/')
236                  filename[strlen(filename) - 1] = 0;                  filename[strlen(filename) - 1] = 0;
237          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 271  disk_create(uint32 device_id, uint32 acc
271    
272          //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);
273    
         /* 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;  
   
274          // Get information about file and set that flag ourselfs          // Get information about file and set that flag ourselfs
275          if ((stat(path, &filestat) == 0) && (S_ISDIR(filestat.st_mode)))          if ((stat(path, &filestat) == 0) && (S_ISDIR(filestat.st_mode)))
276                  flags_and_attributes |= FILE_DIRECTORY_FILE;          {
277                    if (flags_and_attributes & FILE_NON_DIRECTORY_FILE)
278                            return STATUS_FILE_IS_A_DIRECTORY;
279                    else
280                            flags_and_attributes |= FILE_DIRECTORY_FILE;
281            }
282    
283          if (flags_and_attributes & FILE_DIRECTORY_FILE)          if (flags_and_attributes & FILE_DIRECTORY_FILE)
284          {          {
# Line 291  disk_create(uint32 device_id, uint32 acc Line 330  disk_create(uint32 device_id, uint32 acc
330                  {                  {
331                          switch (errno)                          switch (errno)
332                          {                          {
333                                    case EISDIR:
334    
335                                            return STATUS_FILE_IS_A_DIRECTORY;
336    
337                                  case EACCES:                                  case EACCES:
338    
339                                          return STATUS_ACCESS_DENIED;                                          return STATUS_ACCESS_DENIED;
# Line 352  disk_read(HANDLE handle, uint8 * data, u Line 395  disk_read(HANDLE handle, uint8 * data, u
395  {  {
396          int n;          int n;
397    
398    #if 0
399          /* browsing dir ????        */          /* browsing dir ????        */
400          /* each request is 24 bytes */          /* each request is 24 bytes */
401          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 403  disk_read(HANDLE handle, uint8 * data, u
403                  *result = 0;                  *result = 0;
404                  return STATUS_SUCCESS;                  return STATUS_SUCCESS;
405          }          }
406    #endif
407    
408          if (offset)          if (offset)
409                  lseek(handle, offset, SEEK_SET);                  lseek(handle, offset, SEEK_SET);
# Line 366  disk_read(HANDLE handle, uint8 * data, u Line 411  disk_read(HANDLE handle, uint8 * data, u
411    
412          if (n < 0)          if (n < 0)
413          {          {
                 perror("read");  
414                  *result = 0;                  *result = 0;
415                  return STATUS_INVALID_PARAMETER;                  switch (errno)
416                    {
417                            case EISDIR:
418                                    return STATUS_FILE_IS_A_DIRECTORY;
419                            default:
420                                    perror("read");
421                                    return STATUS_INVALID_PARAMETER;
422                    }
423          }          }
424    
425          *result = n;          *result = n;
# Line 390  disk_write(HANDLE handle, uint8 * data, Line 441  disk_write(HANDLE handle, uint8 * data,
441          {          {
442                  perror("write");                  perror("write");
443                  *result = 0;                  *result = 0;
444                  return STATUS_ACCESS_DENIED;                  switch (errno)
445                    {
446                            case ENOSPC:
447                                    return STATUS_DISK_FULL;
448                            default:
449                                    return STATUS_ACCESS_DENIED;
450                    }
451          }          }
452    
453          *result = n;          *result = n;
# Line 418  disk_query_information(HANDLE handle, ui Line 475  disk_query_information(HANDLE handle, ui
475          // Set file attributes          // Set file attributes
476          file_attributes = 0;          file_attributes = 0;
477          if (S_ISDIR(filestat.st_mode))          if (S_ISDIR(filestat.st_mode))
         {  
478                  file_attributes |= FILE_ATTRIBUTE_DIRECTORY;                  file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
479          }  
480          filename = 1 + strrchr(path, '/');          filename = 1 + strrchr(path, '/');
481          if (filename && filename[0] == '.')          if (filename && filename[0] == '.')
         {  
482                  file_attributes |= FILE_ATTRIBUTE_HIDDEN;                  file_attributes |= FILE_ATTRIBUTE_HIDDEN;
483          }  
484            if (!file_attributes)
485                    file_attributes |= FILE_ATTRIBUTE_NORMAL;
486    
487            if (!(filestat.st_mode & S_IWUSR))
488                    file_attributes |= FILE_ATTRIBUTE_READONLY;
489    
490          // Return requested data          // Return requested data
491          switch (info_class)          switch (info_class)
492          {          {
493                  case 4: /* FileBasicInformation */                  case 4: /* FileBasicInformation */
494                            seconds_since_1970_to_filetime(get_create_time(&filestat), &ft_high,
495                          out_uint8s(out, 8);     //create_time not available;                                                         &ft_low);
496                            out_uint32_le(out, ft_low);     //create_access_time
497                            out_uint32_le(out, ft_high);
498    
499                          seconds_since_1970_to_filetime(filestat.st_atime, &ft_high, &ft_low);                          seconds_since_1970_to_filetime(filestat.st_atime, &ft_high, &ft_low);
500                          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 504  disk_query_information(HANDLE handle, ui
504                          out_uint32_le(out, ft_low);     //last_write_time                          out_uint32_le(out, ft_low);     //last_write_time
505                          out_uint32_le(out, ft_high);                          out_uint32_le(out, ft_high);
506    
507                          out_uint8s(out, 8);     //unknown zero                          seconds_since_1970_to_filetime(filestat.st_ctime, &ft_high, &ft_low);
508                            out_uint32_le(out, ft_low);     //last_change_time
509                            out_uint32_le(out, ft_high);
510    
511                          out_uint32_le(out, file_attributes);                          out_uint32_le(out, file_attributes);
512                          break;                          break;
513    
# Line 478  disk_set_information(HANDLE handle, uint Line 543  disk_set_information(HANDLE handle, uint
543          char newname[256], fullpath[256];          char newname[256], fullpath[256];
544          struct fileinfo *pfinfo;          struct fileinfo *pfinfo;
545    
546            int mode;
547            struct stat filestat;
548            time_t write_time, change_time, access_time, mod_time;
549            struct utimbuf tvs;
550    
551          pfinfo = &(g_fileinfo[handle]);          pfinfo = &(g_fileinfo[handle]);
552    
553          switch (info_class)          switch (info_class)
554          {          {
555                  case 4: /* FileBasicInformation */                  case 4: /* FileBasicInformation */
556                            write_time = change_time = access_time = 0;
557    
558                            in_uint8s(in, 4);       /* Handle of root dir? */
559                            in_uint8s(in, 24);      /* unknown */
560    
561                          // Probably safe to ignore                          // CreationTime
562                            in_uint32_le(in, ft_low);
563                            in_uint32_le(in, ft_high);
564    
565                            // AccessTime
566                            in_uint32_le(in, ft_low);
567                            in_uint32_le(in, ft_high);
568                            if (ft_low || ft_high)
569                                    access_time = convert_1970_to_filetime(ft_high, ft_low);
570    
571                            // WriteTime
572                            in_uint32_le(in, ft_low);
573                            in_uint32_le(in, ft_high);
574                            if (ft_low || ft_high)
575                                    write_time = convert_1970_to_filetime(ft_high, ft_low);
576    
577                            // ChangeTime
578                            in_uint32_le(in, ft_low);
579                            in_uint32_le(in, ft_high);
580                            if (ft_low || ft_high)
581                                    change_time = convert_1970_to_filetime(ft_high, ft_low);
582    
583                            in_uint32_le(in, file_attributes);
584    
585                            if (fstat(handle, &filestat))
586                                    return STATUS_ACCESS_DENIED;
587    
588                            tvs.modtime = filestat.st_mtime;
589                            tvs.actime = filestat.st_atime;
590                            if (access_time)
591                                    tvs.actime = access_time;
592    
593    
594                            if (write_time || change_time)
595                                    mod_time = MIN(write_time, change_time);
596                            else
597                                    mod_time = write_time ? write_time : change_time;
598    
599                            if (mod_time)
600                                    tvs.modtime = mod_time;
601    
602    
603                            if (access_time || write_time || change_time)
604                            {
605    #if WITH_DEBUG_RDP5
606                                    printf("FileBasicInformation access       time %s",
607                                           ctime(&tvs.actime));
608                                    printf("FileBasicInformation modification time %s",
609                                           ctime(&tvs.modtime));
610    #endif
611                                    if (utime(pfinfo->path, &tvs))
612                                            return STATUS_ACCESS_DENIED;
613                            }
614    
615                            if (!file_attributes)
616                                    break;  // not valid
617    
618                            mode = filestat.st_mode;
619    
620                            if (file_attributes & FILE_ATTRIBUTE_READONLY)
621                                    mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
622                            else
623                                    mode |= S_IWUSR;
624    
625                            mode &= 0777;
626    #if WITH_DEBUG_RDP5
627                            printf("FileBasicInformation set access mode 0%o", mode);
628    #endif
629    
630                            if (fchmod(handle, mode))
631                                    return STATUS_ACCESS_DENIED;
632                          break;                          break;
633    
634                  case 10:        /* FileRenameInformation */                  case 10:        /* FileRenameInformation */
# Line 527  disk_set_information(HANDLE handle, uint Line 671  disk_set_information(HANDLE handle, uint
671                          break;                          break;
672    
673                  case 20:        /* FileEndOfFileInformation */                  case 20:        /* FileEndOfFileInformation */
674                            in_uint8s(in, 28);      /* unknown */
675                            in_uint32_le(in, length);       /* file size */
676    
677                            printf("FileEndOfFileInformation length = %d\n", length);
678                            // ????????????
679    
680                          unimpl("IRP Set File Information class: FileEndOfFileInformation\n");                          unimpl("IRP Set File Information class: FileEndOfFileInformation\n");
681                          break;                          break;
   
682                  default:                  default:
683    
684                          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 698  disk_query_volume_information(HANDLE han
698          volume = "RDESKTOP";          volume = "RDESKTOP";
699          fs_type = "RDPFS";          fs_type = "RDPFS";
700    
701          if (STATFS_FN(pfinfo->path, &stat_fs) != 0)     /* FIXME: statfs is not portable */          if (STATFS_FN(pfinfo->path, &stat_fs) != 0)
702          {          {
703                  perror("statfs");                  perror("statfs");
704                  return STATUS_ACCESS_DENIED;                  return STATUS_ACCESS_DENIED;
# Line 629  disk_query_directory(HANDLE handle, uint Line 777  disk_query_directory(HANDLE handle, uint
777                          // find next dirent matching pattern                          // find next dirent matching pattern
778                          pdirent = readdir(pdir);                          pdirent = readdir(pdir);
779                          while (pdirent && fnmatch(pfinfo->pattern, pdirent->d_name, 0) != 0)                          while (pdirent && fnmatch(pfinfo->pattern, pdirent->d_name, 0) != 0)
                         {  
780                                  pdirent = readdir(pdir);                                  pdirent = readdir(pdir);
                         }  
781    
782                          if (pdirent == NULL)                          if (pdirent == NULL)
                         {  
783                                  return STATUS_NO_MORE_FILES;                                  return STATUS_NO_MORE_FILES;
                         }  
784    
785                          // Get information for directory entry                          // Get information for directory entry
786                          sprintf(fullpath, "%s/%s", dirname, pdirent->d_name);                          sprintf(fullpath, "%s/%s", dirname, pdirent->d_name);
787    
788                          /* JIF                          /* JIF
789                             printf("Stat: %s\n", fullpath); */                             printf("Stat: %s\n", fullpath); */
790                          if (stat(fullpath, &fstat))                          if (stat(fullpath, &fstat))
# Line 653  disk_query_directory(HANDLE handle, uint Line 798  disk_query_directory(HANDLE handle, uint
798                                  file_attributes |= FILE_ATTRIBUTE_DIRECTORY;                                  file_attributes |= FILE_ATTRIBUTE_DIRECTORY;
799                          if (pdirent->d_name[0] == '.')                          if (pdirent->d_name[0] == '.')
800                                  file_attributes |= FILE_ATTRIBUTE_HIDDEN;                                  file_attributes |= FILE_ATTRIBUTE_HIDDEN;
801                            if (!file_attributes)
802                                    file_attributes |= FILE_ATTRIBUTE_NORMAL;
803                            if (!(fstat.st_mode & S_IWUSR))
804                                    file_attributes |= FILE_ATTRIBUTE_READONLY;
805    
806                          // Return requested information                          // Return requested information
807                          out_uint8s(out, 8);     //unknown zero                          out_uint8s(out, 8);     //unknown zero
808                          out_uint8s(out, 8);     //create_time not available in posix;  
809                            seconds_since_1970_to_filetime(get_create_time(&fstat), &ft_high, &ft_low);
810                            out_uint32_le(out, ft_low);     // create time
811                            out_uint32_le(out, ft_high);
812    
813                          seconds_since_1970_to_filetime(fstat.st_atime, &ft_high, &ft_low);                          seconds_since_1970_to_filetime(fstat.st_atime, &ft_high, &ft_low);
814                          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 818  disk_query_directory(HANDLE handle, uint
818                          out_uint32_le(out, ft_low);     //last_write_time                          out_uint32_le(out, ft_low);     //last_write_time
819                          out_uint32_le(out, ft_high);                          out_uint32_le(out, ft_high);
820    
821                          out_uint8s(out, 8);     //unknown zero                          seconds_since_1970_to_filetime(fstat.st_ctime, &ft_high, &ft_low);
822                            out_uint32_le(out, ft_low);     //change_write_time
823                            out_uint32_le(out, ft_high);
824    
825                          out_uint32_le(out, fstat.st_size);      //filesize low                          out_uint32_le(out, fstat.st_size);      //filesize low
826                          out_uint32_le(out, 0);  //filesize high                          out_uint32_le(out, 0);  //filesize high
827                          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 843  disk_query_directory(HANDLE handle, uint
843          return STATUS_SUCCESS;          return STATUS_SUCCESS;
844  }  }
845    
846    
847    
848    static NTSTATUS
849    disk_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out)
850    {
851            uint32 result;
852    
853            if (((request >> 16) != 20) || ((request >> 16) != 9))
854                    return STATUS_INVALID_PARAMETER;
855    
856            /* extract operation */
857            request >>= 2;
858            request &= 0xfff;
859    
860            printf("DISK IOCTL %d\n", request);
861    
862            switch (request)
863            {
864                    case 25:        // ?
865                    case 42:        // ?
866                    default:
867                            unimpl("DISK IOCTL %d\n", request);
868                            return STATUS_INVALID_PARAMETER;
869            }
870    
871            return STATUS_SUCCESS;
872    }
873    
874  DEVICE_FNS disk_fns = {  DEVICE_FNS disk_fns = {
875          disk_create,          disk_create,
876          disk_close,          disk_close,
877          disk_read,          disk_read,
878          disk_write,          disk_write,
879          NULL                    /* device_control */          disk_device_control     /* device_control */
880  };  };

Legend:
Removed from v.599  
changed lines
  Added in v.600

  ViewVC Help
Powered by ViewVC 1.1.26