--- sourceforge.net/trunk/rdesktop/disk.c 2004/01/21 22:13:20 573 +++ sourceforge.net/trunk/rdesktop/disk.c 2004/10/15 18:02:45 783 @@ -18,61 +18,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#define FILE_ATTRIBUTE_READONLY 0x00000001 -#define FILE_ATTRIBUTE_HIDDEN 0x00000002 -#define FILE_ATTRIBUTE_SYSTEM 0x00000004 -#define FILE_ATTRIBUTE_DIRECTORY 0x00000010 -#define FILE_ATTRIBUTE_ARCHIVE 0x00000020 -#define FILE_ATTRIBUTE_DEVICE 0x00000040 -#define FILE_ATTRIBUTE_NORMAL 0x00000080 -#define FILE_ATTRIBUTE_TEMPORARY 0x00000100 -#define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200 -#define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400 -#define FILE_ATTRIBUTE_COMPRESSED 0x00000800 -#define FILE_ATTRIBUTE_OFFLINE 0x00001000 -#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000 -#define FILE_ATTRIBUTE_ENCRYPTED 0x00004000 - -#define FILE_BASIC_INFORMATION 0x04 -#define FILE_STANDARD_INFORMATION 0x05 - -#define FS_CASE_SENSITIVE 0x00000001 -#define FS_CASE_IS_PRESERVED 0x00000002 -#define FS_UNICODE_STORED_ON_DISK 0x00000004 -#define FS_PERSISTENT_ACLS 0x00000008 -#define FS_FILE_COMPRESSION 0x00000010 -#define FS_VOLUME_QUOTAS 0x00000020 -#define FS_SUPPORTS_SPARSE_FILES 0x00000040 -#define FS_SUPPORTS_REPARSE_POINTS 0x00000080 -#define FS_SUPPORTS_REMOTE_STORAGE 0X00000100 -#define FS_VOL_IS_COMPRESSED 0x00008000 -#define FILE_READ_ONLY_VOLUME 0x00080000 - -#define OPEN_EXISTING 1 -#define CREATE_NEW 2 -#define OPEN_ALWAYS 3 -#define TRUNCATE_EXISTING 4 -#define CREATE_ALWAYS 5 - -#define GENERIC_READ 0x80000000 -#define GENERIC_WRITE 0x40000000 -#define GENERIC_EXECUTE 0x20000000 -#define GENERIC_ALL 0x10000000 - -#define ERROR_FILE_NOT_FOUND 2L -#define ERROR_ALREADY_EXISTS 183L - -#define MAX_OPEN_FILES 0x100 - -#if (defined(sun) && (defined(__svr4__) || defined(__SVR4))) -#define SOLARIS -#endif - -#ifdef SOLARIS -#define DIRFD(a) ((a)->dd_fd) -#else -#define DIRFD(a) (dirfd(a)) -#endif +#include "disk.h" #include #include @@ -82,20 +28,51 @@ #include #include /* errno */ -#if defined(SOLARIS) +#include +#include /* ctime */ + +#if (defined(HAVE_DIRFD) || (HAVE_DECL_DIRFD == 1)) +#define DIRFD(a) (dirfd(a)) +#else +#define DIRFD(a) ((a)->DIR_FD_MEMBER_NAME) +#endif + +/* TODO: let autoconf figure out everything below... */ +#if (defined(sun) && (defined(__svr4__) || defined(__SVR4))) +#define SOLARIS +#endif + +#if (defined(SOLARIS) || defined (__hpux) || defined(__BEOS__)) #include /* solaris statvfs */ -#define HAVE_STATVFS +/* TODO: Fix mntent-handling for solaris/hpux + * #include */ +#undef HAVE_MNTENT_H +#define MNTENT_PATH "/etc/mnttab" +#define STATFS_FN(path, buf) (statvfs(path,buf)) +#define STATFS_T statvfs #define F_NAMELEN(buf) ((buf).f_namemax) -#elif defined(__OpenBSD__) +#elif (defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__)) #include #include -#define HAVE_STATFS +#define STATFS_FN(path, buf) (statfs(path,buf)) +#define STATFS_T statfs #define F_NAMELEN(buf) (NAME_MAX) +#elif (defined(__SGI_IRIX__)) +#include +#include +#define STATFS_FN(path, buf) (statvfs(path,buf)) +#define STATFS_T statvfs +#define F_NAMELEN(buf) ((buf).f_namemax) + #else #include /* linux statfs */ -#define HAVE_STATFS +#include +#define HAVE_MNTENT_H +#define MNTENT_PATH "/etc/mtab" +#define STATFS_FN(path, buf) (statfs(path,buf)) +#define STATFS_T statfs #define F_NAMELEN(buf) ((buf).f_namelen) #endif @@ -103,24 +80,33 @@ extern RDPDR_DEVICE g_rdpdr_device[]; -struct fileinfo +FILEINFO g_fileinfo[MAX_OPEN_FILES]; + +typedef struct { - uint32 device_id, flags_and_attributes; - char path[256]; - DIR *pdir; - struct dirent *pdirent; - char pattern[64]; - BOOL delete_on_close; -} -g_fileinfo[MAX_OPEN_FILES]; + char name[256]; + char label[256]; + unsigned long serial; + char type[256]; +} FsInfoType; -struct fsinfo + +static time_t +get_create_time(struct stat *st) { - uint32 f_blocks, f_bfree, f_bsize, f_namelen; -}; + time_t ret, ret1; + + ret = MIN(st->st_ctime, st->st_mtime); + ret1 = MIN(ret, st->st_atime); + + if (ret1 != (time_t) 0) + return ret1; + + return ret; +} /* Convert seconds since 1970 to a filetime */ -void +static void seconds_since_1970_to_filetime(time_t seconds, uint32 * high, uint32 * low) { unsigned long long ticks; @@ -130,12 +116,29 @@ *high = (uint32) (ticks >> 32); } +/* Convert seconds since 1970 back to filetime */ +static time_t +convert_1970_to_filetime(uint32 high, uint32 low) +{ + unsigned long long ticks; + time_t val; + + ticks = low + (((unsigned long long) high) << 32); + ticks /= 10000000; + ticks -= 11644473600LL; + + val = (time_t) ticks; + return (val); + +} + + /* Enumeration of devices from rdesktop.c */ /* returns numer of units found and initialized. */ -/* optarg looks like ':h:=/mnt/floppy,b:=/mnt/usbdevice1' */ +/* optarg looks like ':h=/mnt/floppy,b=/mnt/usbdevice1' */ /* when it arrives to this function. */ int -disk_enum_devices(int *id, char *optarg) +disk_enum_devices(uint32 * id, char *optarg) { char *pos = optarg; char *pos2; @@ -146,16 +149,14 @@ while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES) { pos2 = next_arg(optarg, '='); - strcpy(g_rdpdr_device[*id].name, optarg); - toupper_str(g_rdpdr_device[*id].name); - - /* add trailing colon to name. */ - strcat(g_rdpdr_device[*id].name, ":"); + strncpy(g_rdpdr_device[*id].name, optarg, sizeof(g_rdpdr_device[*id].name)); + if (strlen(optarg) > 8) + fprintf(stderr, "share name %s truncated to %s\n", optarg, + g_rdpdr_device[*id].name); g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1); strcpy(g_rdpdr_device[*id].local_path, pos2); - printf("DISK %s to %s\n", g_rdpdr_device[*id].name, g_rdpdr_device[*id].local_path); g_rdpdr_device[*id].device_type = DEVICE_TYPE_DISK; count++; (*id)++; @@ -165,25 +166,26 @@ return count; } -/* Opens of creates a file or directory */ -NTSTATUS +/* Opens or creates a file or directory */ +static NTSTATUS disk_create(uint32 device_id, uint32 accessmask, uint32 sharemode, uint32 create_disposition, - uint32 flags_and_attributes, char *filename, HANDLE * phandle) + uint32 flags_and_attributes, char *filename, NTHANDLE * phandle) { - HANDLE handle; + NTHANDLE handle; DIR *dirp; int flags, mode; char path[256]; + struct stat filestat; handle = 0; dirp = NULL; flags = 0; mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; + if (filename[strlen(filename) - 1] == '/') filename[strlen(filename) - 1] = 0; sprintf(path, "%s%s", g_rdpdr_device[device_id].local_path, filename); - //printf("Open: %s\n", path); switch (create_disposition) { @@ -218,6 +220,17 @@ break; } + //printf("Open: \"%s\" flags: %u, accessmask: %u sharemode: %u create disp: %u\n", path, flags_and_attributes, accessmask, sharemode, create_disposition); + + // Get information about file and set that flag ourselfs + if ((stat(path, &filestat) == 0) && (S_ISDIR(filestat.st_mode))) + { + if (flags_and_attributes & FILE_NON_DIRECTORY_FILE) + return STATUS_FILE_IS_A_DIRECTORY; + else + flags_and_attributes |= FILE_DIRECTORY_FILE; + } + if (flags_and_attributes & FILE_DIRECTORY_FILE) { if (flags & O_CREAT) @@ -248,6 +261,7 @@ } else { + if (accessmask & GENERIC_ALL || (accessmask & GENERIC_READ && accessmask & GENERIC_WRITE)) { @@ -267,6 +281,10 @@ { switch (errno) { + case EISDIR: + + return STATUS_FILE_IS_A_DIRECTORY; + case EACCES: return STATUS_ACCESS_DENIED; @@ -274,13 +292,19 @@ case ENOENT: return STATUS_NO_SUCH_FILE; + case EEXIST: + return STATUS_OBJECT_NAME_COLLISION; default: perror("open"); return STATUS_NO_SUCH_FILE; } } + + /* all read and writes of files should be non blocking */ + if (fcntl(handle, F_SETFL, O_NONBLOCK) == -1) + perror("fcntl"); } if (handle >= MAX_OPEN_FILES) @@ -300,8 +324,8 @@ return STATUS_SUCCESS; } -NTSTATUS -disk_close(HANDLE handle) +static NTSTATUS +disk_close(NTHANDLE handle) { struct fileinfo *pfinfo; @@ -320,20 +344,36 @@ return STATUS_SUCCESS; } -NTSTATUS -disk_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) +static NTSTATUS +disk_read(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) { int n; - if (offset) - lseek(handle, offset, SEEK_SET); +#if 0 + /* browsing dir ???? */ + /* each request is 24 bytes */ + if (g_fileinfo[handle].flags_and_attributes & FILE_DIRECTORY_FILE) + { + *result = 0; + return STATUS_SUCCESS; + } +#endif + + lseek(handle, offset, SEEK_SET); + n = read(handle, data, length); if (n < 0) { - perror("read"); *result = 0; - return STATUS_INVALID_PARAMETER; + switch (errno) + { + case EISDIR: + return STATUS_FILE_IS_A_DIRECTORY; + default: + perror("read"); + return STATUS_INVALID_PARAMETER; + } } *result = n; @@ -341,13 +381,12 @@ return STATUS_SUCCESS; } -NTSTATUS -disk_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) +static NTSTATUS +disk_write(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) { int n; - if (offset) - lseek(handle, offset, SEEK_SET); + lseek(handle, offset, SEEK_SET); n = write(handle, data, length); @@ -355,7 +394,13 @@ { perror("write"); *result = 0; - return STATUS_ACCESS_DENIED; + switch (errno) + { + case ENOSPC: + return STATUS_DISK_FULL; + default: + return STATUS_ACCESS_DENIED; + } } *result = n; @@ -364,7 +409,7 @@ } NTSTATUS -disk_query_information(HANDLE handle, uint32 info_class, STREAM out) +disk_query_information(NTHANDLE handle, uint32 info_class, STREAM out) { uint32 file_attributes, ft_high, ft_low; struct stat filestat; @@ -383,21 +428,26 @@ // Set file attributes file_attributes = 0; if (S_ISDIR(filestat.st_mode)) - { file_attributes |= FILE_ATTRIBUTE_DIRECTORY; - } + filename = 1 + strrchr(path, '/'); if (filename && filename[0] == '.') - { file_attributes |= FILE_ATTRIBUTE_HIDDEN; - } + + if (!file_attributes) + file_attributes |= FILE_ATTRIBUTE_NORMAL; + + if (!(filestat.st_mode & S_IWUSR)) + file_attributes |= FILE_ATTRIBUTE_READONLY; // Return requested data switch (info_class) { - case 4: /* FileBasicInformation */ - - out_uint8s(out, 8); //create_time not available; + case FileBasicInformation: + seconds_since_1970_to_filetime(get_create_time(&filestat), &ft_high, + &ft_low); + out_uint32_le(out, ft_low); //create_access_time + out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(filestat.st_atime, &ft_high, &ft_low); out_uint32_le(out, ft_low); //last_access_time @@ -407,11 +457,14 @@ out_uint32_le(out, ft_low); //last_write_time out_uint32_le(out, ft_high); - out_uint8s(out, 8); //unknown zero + seconds_since_1970_to_filetime(filestat.st_ctime, &ft_high, &ft_low); + out_uint32_le(out, ft_low); //last_change_time + out_uint32_le(out, ft_high); + out_uint32_le(out, file_attributes); break; - case 5: /* FileStandardInformation */ + case FileStandardInformation: out_uint32_le(out, filestat.st_size); //Allocation size out_uint32_le(out, 0); @@ -422,7 +475,7 @@ out_uint8(out, S_ISDIR(filestat.st_mode) ? 1 : 0); //Directory break; - case 35: /* FileObjectIdInformation */ + case FileObjectIdInformation: out_uint32_le(out, file_attributes); /* File Attributes */ out_uint32_le(out, 0); /* Reparse Tag */ @@ -437,22 +490,103 @@ } NTSTATUS -disk_set_information(HANDLE handle, uint32 info_class, STREAM in, STREAM out) +disk_set_information(NTHANDLE handle, uint32 info_class, STREAM in, STREAM out) { uint32 device_id, length, file_attributes, ft_high, ft_low; char newname[256], fullpath[256]; struct fileinfo *pfinfo; + int mode; + struct stat filestat; + time_t write_time, change_time, access_time, mod_time; + struct utimbuf tvs; + struct STATFS_T stat_fs; + pfinfo = &(g_fileinfo[handle]); switch (info_class) { - case 4: /* FileBasicInformation */ + case FileBasicInformation: + write_time = change_time = access_time = 0; + + in_uint8s(in, 4); /* Handle of root dir? */ + in_uint8s(in, 24); /* unknown */ + + // CreationTime + in_uint32_le(in, ft_low); + in_uint32_le(in, ft_high); + + // AccessTime + in_uint32_le(in, ft_low); + in_uint32_le(in, ft_high); + if (ft_low || ft_high) + access_time = convert_1970_to_filetime(ft_high, ft_low); + + // WriteTime + in_uint32_le(in, ft_low); + in_uint32_le(in, ft_high); + if (ft_low || ft_high) + write_time = convert_1970_to_filetime(ft_high, ft_low); + + // ChangeTime + in_uint32_le(in, ft_low); + in_uint32_le(in, ft_high); + if (ft_low || ft_high) + change_time = convert_1970_to_filetime(ft_high, ft_low); + + in_uint32_le(in, file_attributes); + + if (fstat(handle, &filestat)) + return STATUS_ACCESS_DENIED; + + tvs.modtime = filestat.st_mtime; + tvs.actime = filestat.st_atime; + if (access_time) + tvs.actime = access_time; + + + if (write_time || change_time) + mod_time = MIN(write_time, change_time); + else + mod_time = write_time ? write_time : change_time; + + if (mod_time) + tvs.modtime = mod_time; + + + if (access_time || write_time || change_time) + { +#if WITH_DEBUG_RDP5 + printf("FileBasicInformation access time %s", + ctime(&tvs.actime)); + printf("FileBasicInformation modification time %s", + ctime(&tvs.modtime)); +#endif + if (utime(pfinfo->path, &tvs)) + return STATUS_ACCESS_DENIED; + } + + if (!file_attributes) + break; // not valid + + mode = filestat.st_mode; + + if (file_attributes & FILE_ATTRIBUTE_READONLY) + mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH); + else + mode |= S_IWUSR; + + mode &= 0777; +#if WITH_DEBUG_RDP5 + printf("FileBasicInformation set access mode 0%o", mode); +#endif + + if (fchmod(handle, mode)) + return STATUS_ACCESS_DENIED; - // Probably safe to ignore break; - case 10: /* FileRenameInformation */ + case FileRenameInformation: in_uint8s(in, 4); /* Handle of root dir? */ in_uint8s(in, 0x1a); /* unknown */ @@ -478,24 +612,61 @@ } break; - case 13: /* FileDispositionInformation */ - - //unimpl("IRP Set File Information class: FileDispositionInformation\n"); - // in_uint32_le(in, delete_on_close); - // disk_close(handle); - unlink(pfinfo->path); - break; + case FileDispositionInformation: + /* As far as I understand it, the correct + thing to do here is to *schedule* a delete, + so it will be deleted when the file is + closed. Subsequent + FileDispositionInformation requests with + DeleteFile set to FALSE should unschedule + the delete. See + http://www.osronline.com/article.cfm?article=245. Currently, + we are deleting the file immediately. I + guess this is a FIXME. */ + + //in_uint32_le(in, delete_on_close); + + /* Make sure we close the file before + unlinking it. Not doing so would trigger + silly-delete if using NFS, which might fail + on FAT floppies, for example. */ + disk_close(handle); - case 19: /* FileAllocationInformation */ + if ((pfinfo->flags_and_attributes & FILE_DIRECTORY_FILE)) // remove a directory + { + if (rmdir(pfinfo->path) < 0) + return STATUS_ACCESS_DENIED; + } + else if (unlink(pfinfo->path) < 0) // unlink a file + return STATUS_ACCESS_DENIED; - unimpl("IRP Set File Information class: FileAllocationInformation\n"); break; - case 20: /* FileEndOfFileInformation */ + case FileAllocationInformation: + /* Fall through to FileEndOfFileInformation, + which uses ftrunc. This is like Samba with + "strict allocation = false", and means that + we won't detect out-of-quota errors, for + example. */ + + case FileEndOfFileInformation: + in_uint8s(in, 28); /* unknown */ + in_uint32_le(in, length); /* file size */ + + /* prevents start of writing if not enough space left on device */ + if (STATFS_FN(g_rdpdr_device[pfinfo->device_id].local_path, &stat_fs) == 0) + if (stat_fs.f_bsize * stat_fs.f_bfree < length) + return STATUS_DISK_FULL; + + /* FIXME: Growing file with ftruncate doesn't + work with Linux FAT fs */ + if (ftruncate(handle, length) != 0) + { + perror("ftruncate"); + return STATUS_DISK_FULL; + } - unimpl("IRP Set File Information class: FileEndOfFileInformation\n"); break; - default: unimpl("IRP Set File Information class: 0x%x\n", info_class); @@ -504,61 +675,106 @@ return STATUS_SUCCESS; } -int fsstat(const char *path, struct fsinfo *buf) +static FsInfoType * +FsVolumeInfo(char *fpath) { - int ret; -#if defined(HAVE_STATFS) - struct statfs statbuf; -#elif defined(HAVE_STATVFS) - struct statvfs statbuf; -#endif -#if defined(HAVE_STATFS) - ret = statfs(path, &statbuf); -#elif defined(HAVE_STATVFS) - ret = statvfs(path, &statbuf); +#ifdef HAVE_MNTENT_H + FILE *fdfs; + struct mntent *e; + static FsInfoType info; + + /* initialize */ + memset(&info, 0, sizeof(info)); + strcpy(info.label, "RDESKTOP"); + strcpy(info.type, "RDPFS"); + + fdfs = setmntent(MNTENT_PATH, "r"); + if (!fdfs) + return &info; + + while ((e = getmntent(fdfs))) + { + if (strncmp(fpath, e->mnt_dir, strlen(fpath)) == 0) + { + strcpy(info.type, e->mnt_type); + strcpy(info.name, e->mnt_fsname); + if (strstr(e->mnt_opts, "vfat") || strstr(e->mnt_opts, "iso9660")) + { + int fd = open(e->mnt_fsname, O_RDONLY); + if (fd >= 0) + { + unsigned char buf[512]; + memset(buf, 0, sizeof(buf)); + if (strstr(e->mnt_opts, "vfat")) + /*FAT*/ + { + strcpy(info.type, "vfat"); + read(fd, buf, sizeof(buf)); + info.serial = + (buf[42] << 24) + (buf[41] << 16) + + (buf[40] << 8) + buf[39]; + strncpy(info.label, buf + 43, 10); + info.label[10] = '\0'; + } + else if (lseek(fd, 32767, SEEK_SET) >= 0) /* ISO9660 */ + { + read(fd, buf, sizeof(buf)); + strncpy(info.label, buf + 41, 32); + info.label[32] = '\0'; + //info.Serial = (buf[128]<<24)+(buf[127]<<16)+(buf[126]<<8)+buf[125]; + } + close(fd); + } + } + } + } + endmntent(fdfs); #else - ret=-1; -#endif + static FsInfoType info; - buf->f_blocks = statbuf.f_blocks; - buf->f_bfree = statbuf.f_bfree; - buf->f_bsize = statbuf.f_bsize; - buf->f_namelen = F_NAMELEN(statbuf); + /* initialize */ + memset(&info, 0, sizeof(info)); + strcpy(info.label, "RDESKTOP"); + strcpy(info.type, "RDPFS"); - return ret; +#endif + return &info; } + NTSTATUS -disk_query_volume_information(HANDLE handle, uint32 info_class, STREAM out) +disk_query_volume_information(NTHANDLE handle, uint32 info_class, STREAM out) { - char *volume, *fs_type; - struct fsinfo stat_fs; + struct STATFS_T stat_fs; struct fileinfo *pfinfo; + FsInfoType *fsinfo; pfinfo = &(g_fileinfo[handle]); - volume = "RDESKTOP"; - fs_type = "RDPFS"; - if (fsstat(pfinfo->path, &stat_fs) != 0) /* FIXME: statfs is not portable */ + if (STATFS_FN(pfinfo->path, &stat_fs) != 0) { perror("statfs"); return STATUS_ACCESS_DENIED; } + fsinfo = FsVolumeInfo(pfinfo->path); + switch (info_class) { - case 1: /* FileFsVolumeInformation */ + case FileFsVolumeInformation: out_uint32_le(out, 0); /* volume creation time low */ out_uint32_le(out, 0); /* volume creation time high */ - out_uint32_le(out, 0); /* serial */ - out_uint32_le(out, 2 * strlen(volume)); /* length of string */ + out_uint32_le(out, fsinfo->serial); /* serial */ + + out_uint32_le(out, 2 * strlen(fsinfo->label)); /* length of string */ + out_uint8(out, 0); /* support objects? */ - rdp_out_unistr(out, volume, 2 * strlen(volume) - 2); + rdp_out_unistr(out, fsinfo->label, 2 * strlen(fsinfo->label) - 2); break; - case 3: /* FileFsSizeInformation */ + case FileFsSizeInformation: out_uint32_le(out, stat_fs.f_blocks); /* Total allocation units low */ out_uint32_le(out, 0); /* Total allocation high units */ @@ -568,20 +784,22 @@ out_uint32_le(out, 0x200); /* Bytes per sector */ break; - case 5: /* FileFsAttributeInformation */ + case FileFsAttributeInformation: out_uint32_le(out, FS_CASE_SENSITIVE | FS_CASE_IS_PRESERVED); /* fs attributes */ - out_uint32_le(out, stat_fs.f_namelen); /* max length of filename */ - out_uint32_le(out, 2 * strlen(fs_type)); /* length of fs_type */ - rdp_out_unistr(out, fs_type, 2 * strlen(fs_type) - 2); + out_uint32_le(out, F_NAMELEN(stat_fs)); /* max length of filename */ + + out_uint32_le(out, 2 * strlen(fsinfo->type)); /* length of fs_type */ + rdp_out_unistr(out, fsinfo->type, 2 * strlen(fsinfo->type) - 2); break; - case 2: /* FileFsLabelInformation */ - case 4: /* FileFsDeviceInformation */ - case 6: /* FileFsControlInformation */ - case 7: /* FileFsFullSizeInformation */ - case 8: /* FileFsObjectIdInformation */ - case 9: /* FileFsMaximumInformation */ + case FileFsLabelInformation: + case FileFsDeviceInformation: + case FileFsControlInformation: + case FileFsFullSizeInformation: + case FileFsObjectIdInformation: + case FileFsMaximumInformation: + default: unimpl("IRP Query Volume Information class: 0x%x\n", info_class); @@ -591,7 +809,7 @@ } NTSTATUS -disk_query_directory(HANDLE handle, uint32 info_class, char *pattern, STREAM out) +disk_query_directory(NTHANDLE handle, uint32 info_class, char *pattern, STREAM out) { uint32 file_attributes, ft_low, ft_high; char *dirname, fullpath[256]; @@ -607,7 +825,7 @@ switch (info_class) { - case 3: //FIXME: Why 3? + case FileBothDirectoryInformation: // If a search pattern is received, remember this pattern, and restart search if (pattern[0] != 0) @@ -619,34 +837,48 @@ // find next dirent matching pattern pdirent = readdir(pdir); while (pdirent && fnmatch(pfinfo->pattern, pdirent->d_name, 0) != 0) - { pdirent = readdir(pdir); - } if (pdirent == NULL) - { return STATUS_NO_MORE_FILES; - } // Get information for directory entry sprintf(fullpath, "%s/%s", dirname, pdirent->d_name); - /* JIF - printf("Stat: %s\n", fullpath); */ + if (stat(fullpath, &fstat)) { - perror("stat"); - out_uint8(out, 0); - return STATUS_ACCESS_DENIED; + switch (errno) + { + case ENOENT: + case ELOOP: + case EACCES: + /* These are non-fatal errors. */ + memset(&fstat, 0, sizeof(fstat)); + break; + default: + /* Fatal error. By returning STATUS_NO_SUCH_FILE, + the directory list operation will be aborted */ + perror(fullpath); + out_uint8(out, 0); + return STATUS_NO_SUCH_FILE; + } } if (S_ISDIR(fstat.st_mode)) file_attributes |= FILE_ATTRIBUTE_DIRECTORY; if (pdirent->d_name[0] == '.') file_attributes |= FILE_ATTRIBUTE_HIDDEN; + if (!file_attributes) + file_attributes |= FILE_ATTRIBUTE_NORMAL; + if (!(fstat.st_mode & S_IWUSR)) + file_attributes |= FILE_ATTRIBUTE_READONLY; // Return requested information out_uint8s(out, 8); //unknown zero - out_uint8s(out, 8); //create_time not available in posix; + + seconds_since_1970_to_filetime(get_create_time(&fstat), &ft_high, &ft_low); + out_uint32_le(out, ft_low); // create time + out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(fstat.st_atime, &ft_high, &ft_low); out_uint32_le(out, ft_low); //last_access_time @@ -656,7 +888,10 @@ out_uint32_le(out, ft_low); //last_write_time out_uint32_le(out, ft_high); - out_uint8s(out, 8); //unknown zero + seconds_since_1970_to_filetime(fstat.st_ctime, &ft_high, &ft_low); + out_uint32_le(out, ft_low); //change_write_time + out_uint32_le(out, ft_high); + out_uint32_le(out, fstat.st_size); //filesize low out_uint32_le(out, 0); //filesize high out_uint32_le(out, fstat.st_size); //filesize low @@ -670,6 +905,9 @@ break; default: + /* FIXME: Support FileDirectoryInformation, + FileFullDirectoryInformation, and + FileNamesInformation */ unimpl("IRP Query Directory sub: 0x%x\n", info_class); return STATUS_INVALID_PARAMETER; @@ -678,10 +916,38 @@ return STATUS_SUCCESS; } + + +static NTSTATUS +disk_device_control(NTHANDLE handle, uint32 request, STREAM in, STREAM out) +{ + uint32 result; + + if (((request >> 16) != 20) || ((request >> 16) != 9)) + return STATUS_INVALID_PARAMETER; + + /* extract operation */ + request >>= 2; + request &= 0xfff; + + printf("DISK IOCTL %d\n", request); + + switch (request) + { + case 25: // ? + case 42: // ? + default: + unimpl("DISK IOCTL %d\n", request); + return STATUS_INVALID_PARAMETER; + } + + return STATUS_SUCCESS; +} + DEVICE_FNS disk_fns = { disk_create, disk_close, disk_read, disk_write, - NULL /* device_control */ + disk_device_control /* device_control */ };