From a791687e90fbc26a80113b200ba8bf05f94d6118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Fri, 19 Feb 2010 16:52:55 +0000 Subject: [PATCH] * Applied slightly cleaned up patch by romain that sanitizes the volume names. * This closes bug #4602. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35523 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/add-ons/kernel/file_systems/fat/dosfs.c | 22 +++++++-------------- src/add-ons/kernel/file_systems/fat/util.c | 19 ++++++++++++++++++ src/add-ons/kernel/file_systems/fat/util.h | 5 ++++- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/add-ons/kernel/file_systems/fat/dosfs.c b/src/add-ons/kernel/file_systems/fat/dosfs.c index d81db3a50c..b832125df1 100644 --- a/src/add-ons/kernel/file_systems/fat/dosfs.c +++ b/src/add-ons/kernel/file_systems/fat/dosfs.c @@ -187,7 +187,7 @@ volume_init(int fd, uint8* buf, uint8 media_buf[512]; int i; status_t err; - + if ((vol = (nspace *)calloc(sizeof(nspace), 1)) == NULL) { dprintf("dosfs error: out of memory\n"); return NULL; @@ -303,7 +303,7 @@ volume_init(int fd, uint8* buf, 0x00, 0x02, 0x04, 0x01, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0xf8, 0xc0, 0x00, 0x20, 0x00, 0x40, 0x00, 0x20, 0x00, 0x00 }; - + if (memcmp(buf + 0x0b, bogus_zip_data, sizeof(bogus_zip_data)) == 0 && vol->total_sectors == 196576 && ((off_t)geo->sectors_per_track * (off_t)geo->cylinder_count @@ -497,7 +497,7 @@ mount_fat_disk(const char *path, fs_volume *_vol, const int flags, int vol_flags; vol_flags = B_FS_IS_PERSISTENT | B_FS_HAS_MIME; - + // open read-only for now if ((err = (fd = open(path, O_RDONLY | O_NOCACHE))) < 0) { dprintf("dosfs error: unable to open %s (%s)\n", path, strerror(err)); @@ -575,7 +575,7 @@ mount_fat_disk(const char *path, fs_volume *_vol, const int flags, dprintf("dosfs error: error reading boot sector\n"); goto error1; } - + vol = volume_init(fd, buf, vol_flags, fs_flags, &geo); /* check that the partition is large enough to contain the file system */ @@ -765,6 +765,7 @@ dosfs_identify_partition(int fd, partition_data *partition, void **_cookie) cookie->bytes_per_sector = bytes_per_sector; cookie->total_sectors = total_sectors; + sanitize_name(name, 12); strlcpy(cookie->name, name, 12); *_cookie = cookie; @@ -994,7 +995,6 @@ static status_t dosfs_read_fs_stat(fs_volume *_vol, struct fs_info * fss) { nspace* vol = (nspace*)_vol->private_volume; - int i; LOCK_VOL(vol); @@ -1021,19 +1021,11 @@ dosfs_read_fs_stat(fs_volume *_vol, struct fs_info * fss) strncpy(fss->device_name, vol->device, sizeof(fss->device_name)); if (vol->vol_entry > -2) - strncpy(fss->volume_name, vol->vol_label, sizeof(fss->volume_name)); + strlcpy(fss->volume_name, vol->vol_label, sizeof(fss->volume_name)); else strcpy(fss->volume_name, "no name"); - // XXX: should sanitize name as well - for (i=10;i>0;i--) - if (fss->volume_name[i] != ' ') - break; - fss->volume_name[i+1] = 0; - for (;i>=0;i--) { - if ((fss->volume_name[i] >= 'A') && (fss->volume_name[i] <= 'Z')) - fss->volume_name[i] += 'a' - 'A'; - } + sanitize_name(fss->volume_name, 12); // File system name strcpy(fss->fsh_name, "fat"); diff --git a/src/add-ons/kernel/file_systems/fat/util.c b/src/add-ons/kernel/file_systems/fat/util.c index 201ceb243d..7e015ad327 100644 --- a/src/add-ons/kernel/file_systems/fat/util.c +++ b/src/add-ons/kernel/file_systems/fat/util.c @@ -2,6 +2,8 @@ Copyright 1999-2001, Be Incorporated. All Rights Reserved. This file may be used under the terms of the Be Sample Code License. */ + + #include #include @@ -164,3 +166,20 @@ hash_msdos_name(const char *name) c = (c << 7) + (c >> 1) + *(p++); return c; } + + +void +sanitize_name(char *name, int length) +{ + int i; + + for (i = length - 1; i > 0; i--) { + if (name[i] != ' ') + break; + } + name[i + 1] = 0; + for (; i >= 0; i--) { + if (name[i] >= 'A' && name[i] <= 'Z') + name[i] += 'a' - 'A'; + } +} diff --git a/src/add-ons/kernel/file_systems/fat/util.h b/src/add-ons/kernel/file_systems/fat/util.h index 00d400b19d..6fb2b4149d 100644 --- a/src/add-ons/kernel/file_systems/fat/util.h +++ b/src/add-ons/kernel/file_systems/fat/util.h @@ -5,8 +5,10 @@ #ifndef _DOSFS_UTIL_H_ #define _DOSFS_UTIL_H_ + #include + // debugging functions #ifndef ASSERT #ifndef DEBUG @@ -25,6 +27,7 @@ time_t dos2time_t(uint32 t); uint32 time_t2dos(time_t s); uint8 hash_msdos_name(const char *name); +void sanitize_name(char *name, int length); #if 0 #define read32(buffer,off) \ @@ -41,4 +44,4 @@ uint8 hash_msdos_name(const char *name); #define read16(buffer,off) \ B_LENDIAN_TO_HOST_INT16(*(uint16 *)&buffer[off]) -#endif +#endif /* _DOSFS_UTIL_H_ */