From nobody Mon Nov 25 07:36:47 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1716717511; cv=none; d=zohomail.com; s=zohoarc; b=VVmi9uf5mANnPQiykEEbF5754SxW/fIAaZTPlW/DLgwqQWlh1GK5gb355iP1w9kkXlbj7dLDgPrZ5f+XjipWSuEvhFgiFynMC4tNfcUMFOIlhNfFB9kVa2uVOVQkbsEMCm0j2MATwETh2ZoC1Kw4I6I7AtHls5cWdg/fMokEKOM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716717511; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Ln+3lJy+RB8fW9LtHsTAOIqoDd/xm29GUGTXmizOcVk=; b=JOCbh4fPO8EgGx9YREZx9WBb8KiSMCMQpNuM7EAODAOymFwoOGRu3patXvBQfe2rljvxgcIEdWHvm3T33Qrh2+4Swgi2/41v9EEiyunKoRzdHkOWG0G9jeCUDARswz0gzEv9P8mIQxUvrTCShcE0W6wrshgQv+NRvGG5+hQOPfo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716717511738964.3147329095067; Sun, 26 May 2024 02:58:31 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sBAcN-0006CH-BO; Sun, 26 May 2024 05:56:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBAcM-0006BE-93; Sun, 26 May 2024 05:56:42 -0400 Received: from mail-ot1-x32b.google.com ([2607:f8b0:4864:20::32b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sBAcK-0003ob-Q4; Sun, 26 May 2024 05:56:41 -0400 Received: by mail-ot1-x32b.google.com with SMTP id 46e09a7af769-6f8d688ba3cso1032770a34.3; Sun, 26 May 2024 02:56:40 -0700 (PDT) Received: from amjad-pc.. ([192.228.203.229]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-682227f1838sm4021368a12.46.2024.05.26.02.56.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 02:56:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716717398; x=1717322198; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ln+3lJy+RB8fW9LtHsTAOIqoDd/xm29GUGTXmizOcVk=; b=De7WgquOQme9n5mY2cIH6cs67HrgrgnncZ5CIoGpjPYv9fY3CsZIbTu4euV7Yi/Ya7 nF0jl9QzuK18W+5Ba+TyMjxgQ2SQMHT3g3iUUeh5e2lTHt8oG+AiWyg4ebJlOHAELwQc qYTfmOd+Vv1GPV46/b6jpYbZ384qnGyvXxPai6SQ9b9zT0CPxK+5jl4DDed1SozLScea Z8Gk0Wn9BfIB4QLpSfhlzSQlgyQuBXOCaHmTW4w0L21oWlobNOLKc8K73qEtZLl170k2 6FUpjR9fe3QWzBa0SPtnpHgYxUUtc16cm6rjH9xprLs5Ng9yfDyY67z8lBPFFJoSXAVv LLkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716717398; x=1717322198; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ln+3lJy+RB8fW9LtHsTAOIqoDd/xm29GUGTXmizOcVk=; b=af6IAhtBQh1MymwakGecUFbKPj5JsieJg5wZu69b5jjXxnmLF81C2ieqNU95wUHHbl jOIxS1oj9Yd1NKOHI4I8zkP0HOXlb6G7DEF3wrfPZnYn/nC55E9gr1ZUQN/ZSrLZ8+l/ 6rJrT5EPixVwvfUMHIUFUqYUvbIZyPo4GwTaSaS2OsDr2b30bMsNwDeIfUuWKUThyBg+ xNZguzWuSrhP0VKXx/xiT/MstHUcs31jHDvZypstqsSZfN7Ow4+bQh+00qsGgQw/6wYq 1WSCUkHZ1cB5uHg20zB/n1HRCs9Q+gWw1NFlD4v7hEZv5VNLxxYQGY6sssu13Eix6CN7 ivWA== X-Forwarded-Encrypted: i=1; AJvYcCWDyVpDMYC0WWZZxmxFCM943d4lw9k+qIjYuu+wbaptqzWyEctB42cnl0P4JkkWxuxk7CyqIHqgNolXgysnmUPae5ALlRw= X-Gm-Message-State: AOJu0YxcECYl6BjfSCR6sdWVapgJryRoHd4qBHsAGsGrBRvxR7B1l1sZ KY30Oi3aAaVmCbF1ri2RHvJkmHu8bT2vBRGSUhBQaM9uFgQ8Fmt0ueG7AeZV X-Google-Smtp-Source: AGHT+IGZDp16L8jUPuHhauGGkhx5LYLcMz1nEySgT9nKoAM/gdwxCcuyps1XFHPM1A+TV4XSacQzhQ== X-Received: by 2002:a05:6830:110d:b0:6f0:6170:3762 with SMTP id 46e09a7af769-6f8d0ba95f4mr6596480a34.38.1716717398697; Sun, 26 May 2024 02:56:38 -0700 (PDT) From: Amjad Alsharafi To: qemu-devel@nongnu.org Cc: Hanna Reitz , Kevin Wolf , "open list:vvfat" , Amjad Alsharafi Subject: [PATCH v3 1/6] vvfat: Fix bug in writing to middle of file Date: Sun, 26 May 2024 17:56:01 +0800 Message-ID: <2871281d8ea41fb4d7ef8f9beeaba017a1717019.1716717181.git.amjadsharafi10@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::32b; envelope-from=amjadsharafi10@gmail.com; helo=mail-ot1-x32b.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1716717513716100009 Content-Type: text/plain; charset="utf-8" Before this commit, the behavior when calling `commit_one_file` for example with `offset=3D0x2000` (second cluster), what will happen is that we won't fetch the next cluster from the fat, and instead use the first cluster for the read operation. This is due to off-by-one error here, where `i=3D0x2000 !< offset=3D0x2000`, thus not fetching the next cluster. Signed-off-by: Amjad Alsharafi Reviewed-by: Kevin Wolf Tested-by: Kevin Wolf --- block/vvfat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/vvfat.c b/block/vvfat.c index 9d050ba3ae..ab342f0743 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -2525,7 +2525,7 @@ commit_one_file(BDRVVVFATState* s, int dir_index, uin= t32_t offset) return -1; } =20 - for (i =3D s->cluster_size; i < offset; i +=3D s->cluster_size) + for (i =3D s->cluster_size; i <=3D offset; i +=3D s->cluster_size) c =3D modified_fat_get(s, c); =20 fd =3D qemu_open_old(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666); --=20 2.45.0 From nobody Mon Nov 25 07:36:47 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1716717452; cv=none; d=zohomail.com; s=zohoarc; b=SH5wjJW3gWIkzkWrBkqB0I/kVOyj7bSSnfZXhVylOXAJt3wkpyUJ1RgGGhN6dMNeV10CKXQ2DDKzBYH6XLpt11GcbaCRHn31XrVZG3ubKMVe2NCLufmK9abf17Kw5As/ErsrhmDZwxb5yf7FH3FMYrUPCOu49cARL/EqGL/B/GQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716717452; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=/aGPsWq5Naz4uqiF5AmaN5boMB6iQaQGhjOrt7FuaD8=; b=P2GzlPYcAMpfXXd2lzOjMvshfMvCkSgW/MqxYld3EoPkvKKNK238TI0nAjXijT+7rB2hCueJ9YtsB7WdMP4/T4qohIeS/tjcWxC6q2gVuxPlYB26X2eIm+7V1U93px9paBrEQ7VivZAjL/XHyJVnki8cSshSp+EvhpcBWdUyySA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716717452673796.8794969514843; Sun, 26 May 2024 02:57:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sBAcP-0006DM-TB; Sun, 26 May 2024 05:56:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBAcO-0006Co-IC; Sun, 26 May 2024 05:56:44 -0400 Received: from mail-oo1-xc2c.google.com ([2607:f8b0:4864:20::c2c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sBAcM-0003oq-VE; Sun, 26 May 2024 05:56:44 -0400 Received: by mail-oo1-xc2c.google.com with SMTP id 006d021491bc7-5b680c1fdd4so3031968eaf.1; Sun, 26 May 2024 02:56:42 -0700 (PDT) Received: from amjad-pc.. ([192.228.203.229]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-682227f1838sm4021368a12.46.2024.05.26.02.56.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 02:56:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716717401; x=1717322201; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/aGPsWq5Naz4uqiF5AmaN5boMB6iQaQGhjOrt7FuaD8=; b=cqtFrlpTfWF3oLWZGUS0VpUtipoEyofj3s4zA6w8w6cVM51ZmNpGWMqicLabtgYHCl G5xj55IdWqZPX9b+DcWmJejcC9f1RC4DyAVbVlUrYmUYCeuMiyyPDhlOJVWogtOGAryA prA73+0pIWHjbJe/CDYGNPL3XVKjRARQ1y5y8KAMKHrGLH2vZe4ryj7o+sAsrhNaxxrV iBPboS3S1Xt3T1/3b2uqFjFwPOhZkd7c8v5vgOGy+ZIk+qCEUt/OalVi3dGFcteC/DCG w6lyDeaRpRSUALTR03vxa1xxWFh0jAw4a/lf9jBG3FgHt41Y4ixfLtgorX9lfldW80nq en4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716717401; x=1717322201; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/aGPsWq5Naz4uqiF5AmaN5boMB6iQaQGhjOrt7FuaD8=; b=XFADQgT9byKLZIwjZBPNXeP+QMPS7VMHFtWa3jnfJ+ib8k4rESSE5xbXBVP8kL80/9 f5b4gH/MECxAIiQtdDxmvmLNJuNtxJjvlhLktrtk0/sr+AeHNzKpBQAqnZX/sFPsA4k6 V4IDOnH0B4JT+mmWBS1vC4tkZaUsIU9C65TGfpQ8U/s/qMMAVDcJkappVVrQpCS7MibH jxk/gGV5N36NxK8fHFGfZ+U6A1GzJnQXx8L6coQ+18FdYa+9TDAWgpILlR0ye5lqRzFQ Eh4Bhavku5XFxMAROaabMGjNGoxAtdB+c1+WUbeChYUHL+DmtRC+U89swxQyrqukOaqg SLPg== X-Forwarded-Encrypted: i=1; AJvYcCWisa0VIVhtk1sSvwzV5qAzqaOAE/6t+eIaTXVWDhEeGqqb6oFnjGNBXw6C17cn68LPs/3yElwVKPcSQUSTRZMudjw/FuY= X-Gm-Message-State: AOJu0Ywptra4sGKGxjtEMcshWw52dB48JMnfCxYm24Nu/nvJ8Nxe/FDb KCMKtoCz00oNUFyIzPQw5jYG0TAiSj5WaM4NeM8dNQkece34HB3vApTBETOG X-Google-Smtp-Source: AGHT+IF02c68RQd9m1+GGCU7SgrZ1UbneVRrtUhdeit643bsF4MvBK5VFkvPFD2lad4SmuiWYaCg/A== X-Received: by 2002:a05:6358:71cc:b0:18d:9e17:455f with SMTP id e5c5f4694b2df-197e5101158mr891480055d.12.1716717400688; Sun, 26 May 2024 02:56:40 -0700 (PDT) From: Amjad Alsharafi To: qemu-devel@nongnu.org Cc: Hanna Reitz , Kevin Wolf , "open list:vvfat" , Amjad Alsharafi Subject: [PATCH v3 2/6] vvfat: Fix usage of `info.file.offset` Date: Sun, 26 May 2024 17:56:02 +0800 Message-ID: <836a0ea26f641a4f0ff4b2bd8e9c9d17c59ef2d7.1716717181.git.amjadsharafi10@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::c2c; envelope-from=amjadsharafi10@gmail.com; helo=mail-oo1-xc2c.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1716717453850100002 Content-Type: text/plain; charset="utf-8" The field is marked as "the offset in the file (in clusters)", but it was being used like this `cluster_size*(nums)+mapping->info.file.offset`, which is incorrect. Additionally, removed the `abort` when `first_mapping_index` does not match, as this matches the case when adding new clusters for files, and its inevitable that we reach this condition when doing that if the clusters are not after one another, so there is no reason to `abort` here, execution continues and the new clusters are written to disk correctly. Signed-off-by: Amjad Alsharafi --- block/vvfat.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/block/vvfat.c b/block/vvfat.c index ab342f0743..cb3ab81e29 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -1408,7 +1408,7 @@ read_cluster_directory: =20 assert(s->current_fd); =20 - offset=3Ds->cluster_size*(cluster_num-s->current_mapping->begin)+s= ->current_mapping->info.file.offset; + offset=3Ds->cluster_size*((cluster_num - s->current_mapping->begin= ) + s->current_mapping->info.file.offset); if(lseek(s->current_fd, offset, SEEK_SET)!=3Doffset) return -3; s->cluster=3Ds->cluster_buffer; @@ -1929,8 +1929,8 @@ get_cluster_count_for_direntry(BDRVVVFATState* s, dir= entry_t* direntry, const ch (mapping->mode & MODE_DIRECTORY) =3D=3D 0) { =20 /* was modified in qcow */ - if (offset !=3D mapping->info.file.offset + s->cluster= _size - * (cluster_num - mapping->begin)) { + if (offset !=3D s->cluster_size + * ((cluster_num - mapping->begin) + mapping->i= nfo.file.offset)) { /* offset of this cluster in file chain has change= d */ abort(); copy_it =3D 1; @@ -1944,7 +1944,6 @@ get_cluster_count_for_direntry(BDRVVVFATState* s, dir= entry_t* direntry, const ch =20 if (mapping->first_mapping_index !=3D first_mapping_in= dex && mapping->info.file.offset > 0) { - abort(); copy_it =3D 1; } =20 @@ -2404,7 +2403,7 @@ static int commit_mappings(BDRVVVFATState* s, (mapping->end - mapping->begin); } else next_mapping->info.file.offset =3D mapping->info.file.offs= et + - mapping->end - mapping->begin; + (mapping->end - mapping->begin); =20 mapping =3D next_mapping; } --=20 2.45.0 From nobody Mon Nov 25 07:36:47 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1716717452; cv=none; d=zohomail.com; s=zohoarc; b=PT1T3JG3OO6P9/y6ZFBoOmDhlCvJIfbd9h7Bv3fhHTYqqSgNGizxklgxdzHZVGC0d77LYjablII/zsKEyPPy+vNGjEuKgM14T26LImMapdvPD2ZUPHJ+z/uxSHasrzr/u+qJMVHSlKjGbUdVZ6ENszwouEq6lpiqpwbzbwBaqbo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716717452; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Pai5/RNvpgrHRxkL4/w4wO2z+CuTDolRdw2cxdAsR5I=; b=S6QzUKDDj3I6JqKDD6z7eQ1Vd5tCy4Km68/Ef+1bAnYPNCDhs/VJX/0WMaQis+96c8hQ86PlYvpurkLBPb/NLFTlTf6fgDqnX/19lXuwLoeZ0/7rpvNz/0jTz4bgcwzcr4EBworH/IkSZx7r2Qn6VhwdV548CzUAoHadUe21HgA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716717452775538.4131253019445; Sun, 26 May 2024 02:57:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sBAcS-0006FD-K4; Sun, 26 May 2024 05:56:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBAcQ-0006Du-OI; Sun, 26 May 2024 05:56:46 -0400 Received: from mail-oo1-xc2e.google.com ([2607:f8b0:4864:20::c2e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sBAcP-0003p6-6Y; Sun, 26 May 2024 05:56:46 -0400 Received: by mail-oo1-xc2e.google.com with SMTP id 006d021491bc7-5b53350921fso3993412eaf.3; Sun, 26 May 2024 02:56:44 -0700 (PDT) Received: from amjad-pc.. ([192.228.203.229]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-682227f1838sm4021368a12.46.2024.05.26.02.56.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 02:56:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716717403; x=1717322203; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Pai5/RNvpgrHRxkL4/w4wO2z+CuTDolRdw2cxdAsR5I=; b=kWV4Hq/r2lfj+w9D5vdUKNjeLFievvAr0dNhAa6lsJN0220Vrc0qi1GVUq82tj4SsP TA5Axd695luCY1Bg7hgblLFt/SW3WJUxedWYmS4O8k9ozm+LYbfQsv6OUaw/eZJXfIkT fj64RXywYkoPlKeJhx9NoXoly7EFUsQk/AHzSSnOe/AP6/9nb/cDlUsSOpzgT4wODT9p IE/6CEXc+7b6xK8vOZ72l5HL7VCpEPjWLwjx4AwRA7gdmxv1w7Xa5Rb46HtdoFCYrWT/ rvoaXbR6R2iCjoV/wX6jgdTqsFN1/mRiceVo5/j8SsvvPlZwiJ16EqiqqDnW+mWkG8T/ tV6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716717403; x=1717322203; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Pai5/RNvpgrHRxkL4/w4wO2z+CuTDolRdw2cxdAsR5I=; b=ArOf98COsLPP8ZI2RybpCee4HEMW8O76Wl/F4BkPcxFOxHBfqtySzIBds/9aYI80x2 2Cfzyf9xUSFnBN0mtKY9epWUh+sU1ZKYF4S/0cIC1HLv1IWBUocNdiFg+4aWQkr9K48z IHRMSJAiTbe8qcP5TWE+xh4cK/SRAZoiuHzIlnyVd7261WZtfdtpc/bAH/gGxuzAvcWl /QI21C/wz+6ihUhFsbIaJ62KblI336GuyV9W311Rjlu6wY/hKwvNpAYvciNLtD/e7w5g emXL/MkgH1cPRrZIvIv9Fkt9rn6KYxiN2I4EDCuKBYqVaTZdsdFO/kwVJIPbrStI8vB7 ayYQ== X-Forwarded-Encrypted: i=1; AJvYcCVH8VZAceWmxpQxChJMgMA9k6MJTJYC2haaZXBbBGt1wX8Xd8ZlfUgCksKsKvNuXC5y7NAAtQFdgmQ9gdgJElgJUvY2eSs= X-Gm-Message-State: AOJu0YzKcbNmL7/fjkZYQiOrIkcCWhPrKXs8MEq3V3Vjt8A1l0aV8lZw tg2zS5dzAg8E9T/rVN2xkfEmC7vbtjwxTveKwJwLNbOEmoXwMYFqLRpvbC08 X-Google-Smtp-Source: AGHT+IHu+siSMxZfcH5hrtIN6CAYdqiMQSQ+ncv3eik0rEEINB+F//JsiGEtDTeriLOKV6i1D9Ihiw== X-Received: by 2002:a05:6359:a10:b0:186:1d2a:a457 with SMTP id e5c5f4694b2df-197e521706bmr657718355d.15.1716717402719; Sun, 26 May 2024 02:56:42 -0700 (PDT) From: Amjad Alsharafi To: qemu-devel@nongnu.org Cc: Hanna Reitz , Kevin Wolf , "open list:vvfat" , Amjad Alsharafi Subject: [PATCH v3 3/6] vvfat: Fix reading files with non-continuous clusters Date: Sun, 26 May 2024 17:56:03 +0800 Message-ID: X-Mailer: git-send-email 2.45.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::c2e; envelope-from=amjadsharafi10@gmail.com; helo=mail-oo1-xc2e.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1716717453849100001 Content-Type: text/plain; charset="utf-8" When reading with `read_cluster` we get the `mapping` with `find_mapping_for_cluster` and then we call `open_file` for this mapping. The issue appear when its the same file, but a second cluster that is not immediately after it, imagine clusters `500 -> 503`, this will give us 2 mappings one has the range `500..501` and another `503..504`, both point to the same file, but different offsets. When we don't open the file since the path is the same, we won't assign `s->current_mapping` and thus accessing way out of bound of the file. From our example above, after `open_file` (that didn't open anything) we will get the offset into the file with `s->cluster_size*(cluster_num-s->current_mapping->begin)`, which will give us `0x2000 * (504-500)`, which is out of bound for this mapping and will produce some issues. Signed-off-by: Amjad Alsharafi --- block/vvfat.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/block/vvfat.c b/block/vvfat.c index cb3ab81e29..87165abc26 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -1360,15 +1360,22 @@ static int open_file(BDRVVVFATState* s,mapping_t* m= apping) { if(!mapping) return -1; + int new_path =3D 1; if(!s->current_mapping || - strcmp(s->current_mapping->path,mapping->path)) { - /* open file */ - int fd =3D qemu_open_old(mapping->path, + s->current_mapping->first_mapping_index!=3Dmapping->first_mapp= ing_index || + (new_path =3D strcmp(s->current_mapping->path,mapping->path)))= { + + if (new_path) { + /* open file */ + int fd =3D qemu_open_old(mapping->path, O_RDONLY | O_BINARY | O_LARGEFILE); - if(fd<0) - return -1; - vvfat_close_current_file(s); - s->current_fd =3D fd; + if(fd<0) + return -1; + vvfat_close_current_file(s); + + s->current_fd =3D fd; + } + assert(s->current_fd); s->current_mapping =3D mapping; } return 0; --=20 2.45.0 From nobody Mon Nov 25 07:36:47 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1716717497; cv=none; d=zohomail.com; s=zohoarc; b=etoYnW/1WjNEnMlzHlnrGdqzvlqDjMAbL9Qu33HlBlENMQXId7oNy7TPBYAwmu6Ii4mg6Ss7uLBR9DiU5c4jA9n1mWMW4g0K1otwOoqVmuFdmF1DFo0ENZ5P7OTD6icp208+XqubG9EfLcMmIZpO5PDE/cJQI66mN+KwrDQMJDA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716717497; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=/a7uUOY/eUlRvo64Ic/EHJFXKkCA+7DXi0mUI+sS6JI=; b=DnZArKqJZPoALYw9kP5zM1gDLLpJHHE+H8MbRZRjJDUu+oqUBP1Ckf9eGQqanw5qXv01nLoAZQshFD95aQQhP1JSiY3VmgfNlaP9IKZyfARZyh4Wzf6ytXMhaamnaY31PMs3MqwD5eIToEEOI1Ze7NZax5HOHuP2Ngq7tgh2Tmo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716717497399827.5776173357887; Sun, 26 May 2024 02:58:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sBAcY-0006Hg-LZ; Sun, 26 May 2024 05:56:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBAcW-0006Gj-Kd; Sun, 26 May 2024 05:56:52 -0400 Received: from mail-ot1-x32d.google.com ([2607:f8b0:4864:20::32d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sBAcS-0003pQ-0p; Sun, 26 May 2024 05:56:52 -0400 Received: by mail-ot1-x32d.google.com with SMTP id 46e09a7af769-6f1016f41e2so4356067a34.1; Sun, 26 May 2024 02:56:47 -0700 (PDT) Received: from amjad-pc.. ([192.228.203.229]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-682227f1838sm4021368a12.46.2024.05.26.02.56.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 02:56:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716717406; x=1717322206; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/a7uUOY/eUlRvo64Ic/EHJFXKkCA+7DXi0mUI+sS6JI=; b=XQj9WK4QdAESu9dneI/MdRuUEpX3y7igeGaNc2w5ZQimPOEYYS9ghzfjOMbONjQb8S Ox/mIJMcHtLdBIpSrIyaS4K5AHoXV/IHSOJYSag66a1/zzJbZDlnMliak2m+xNxbbc57 zY6v7yx3+EfYsibRFJ/MPS3xY3W9jA6RxBW2by86YgRYchHo1XBZLDKkxT9+oB/Tagdm FfQygay0PMRwdHE5SLYcpeH6cgifPKzIP0/QGkvFkEdv2fw08V4bjs2zH7LyCzp4/RLT E9R5sVwxPejbaP4/TILxe6I7QWIWHxdw3rn+1a0SDv3vR2wK/DODSq3i58YlxiLARdKl dVVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716717406; x=1717322206; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/a7uUOY/eUlRvo64Ic/EHJFXKkCA+7DXi0mUI+sS6JI=; b=WzghpX4ZBgiWtC1jco/a9oofV1nyZEOBAJU6fcz3ZI0kMcZyCwVIiSti3uTAZkLfqH L1RD3pkx0ZR0JKr5OLJNoqELJIQ40ThIX21G7rhelyw3r+FEgY/wM2frKE6BwVJy3+9a 6dJSk8e/t2XWO3PQ3i4A9K+7jN/WLJf77tgeRBfr/AyWDATVCBCvTgecta/kGRA80pxf BcRqeymU22Rb6R45tKLK40vcYaR9HVrxfSqkH4q9Lz+rYRBF4CTfMk+M8DUDuJ3RDBR4 U0DMwaXunTaHtjty3WzYLszdma3iCl7JV44mfY0c4d36/hwizHYHN8DhI33o2lmxSf2H b4mA== X-Forwarded-Encrypted: i=1; AJvYcCVuHS1+SURSbpgG4n1Zw2QcCiXhtmg5Z3gn/oo/R2q09DBPbN6QmuVY0g5wm1KyiYaKzzDH5m8ipTiIQ23gsJbegEwk6Yw= X-Gm-Message-State: AOJu0Yz4VIUqoIsWl6kMbQdAx2AIjn8Q6zYDZuBjX9V5qrPCk8fSx/VV 3KDrde7cGE/KqQXYYnE3sxjRo9pJnll54DHyT/2l30SIpnquh1bdTiMjVCMW X-Google-Smtp-Source: AGHT+IGZPNF35bhpNvOiEIgZWzDue6+QBdRKgkgKxtclrhpN7db8pbvLB4xdSt83nQPOpwTkZG2Eqg== X-Received: by 2002:a05:6358:e4a7:b0:190:47ea:71af with SMTP id e5c5f4694b2df-19863571404mr213391555d.10.1716717405009; Sun, 26 May 2024 02:56:45 -0700 (PDT) From: Amjad Alsharafi To: qemu-devel@nongnu.org Cc: Hanna Reitz , Kevin Wolf , "open list:vvfat" , Amjad Alsharafi Subject: [PATCH v3 4/6] iotests: Add `vvfat` tests Date: Sun, 26 May 2024 17:56:04 +0800 Message-ID: X-Mailer: git-send-email 2.45.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::32d; envelope-from=amjadsharafi10@gmail.com; helo=mail-ot1-x32d.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1716717499740100003 Content-Type: text/plain; charset="utf-8" Added several tests to verify the implementation of the vvfat driver. We needed a way to interact with it, so created a basic `fat16.py` driver t= hat handled writing correct sectors for us. Signed-off-by: Amjad Alsharafi --- tests/qemu-iotests/check | 2 +- tests/qemu-iotests/fat16.py | 507 +++++++++++++++++++++++++++++ tests/qemu-iotests/tests/vvfat | 400 +++++++++++++++++++++++ tests/qemu-iotests/tests/vvfat.out | 5 + 4 files changed, 913 insertions(+), 1 deletion(-) create mode 100644 tests/qemu-iotests/fat16.py create mode 100755 tests/qemu-iotests/tests/vvfat create mode 100755 tests/qemu-iotests/tests/vvfat.out diff --git a/tests/qemu-iotests/check b/tests/qemu-iotests/check index 56d88ca423..545f9ec7bd 100755 --- a/tests/qemu-iotests/check +++ b/tests/qemu-iotests/check @@ -84,7 +84,7 @@ def make_argparser() -> argparse.ArgumentParser: p.set_defaults(imgfmt=3D'raw', imgproto=3D'file') =20 format_list =3D ['raw', 'bochs', 'cloop', 'parallels', 'qcow', 'qcow2', - 'qed', 'vdi', 'vpc', 'vhdx', 'vmdk', 'luks', 'dmg'] + 'qed', 'vdi', 'vpc', 'vhdx', 'vmdk', 'luks', 'dmg', 'vv= fat'] g_fmt =3D p.add_argument_group( ' image format options', 'The following options set the IMGFMT environment variable. ' diff --git a/tests/qemu-iotests/fat16.py b/tests/qemu-iotests/fat16.py new file mode 100644 index 0000000000..6ac5508d8d --- /dev/null +++ b/tests/qemu-iotests/fat16.py @@ -0,0 +1,507 @@ +# A simple FAT16 driver that is used to test the `vvfat` driver in QEMU. +# +# Copyright (C) 2024 Amjad Alsharafi +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from typing import List + +SECTOR_SIZE =3D 512 +DIRENTRY_SIZE =3D 32 + + +class MBR: + def __init__(self, data: bytes): + assert len(data) =3D=3D 512 + self.partition_table =3D [] + for i in range(4): + partition =3D data[446 + i * 16 : 446 + (i + 1) * 16] + self.partition_table.append( + { + "status": partition[0], + "start_head": partition[1], + "start_sector": partition[2] & 0x3F, + "start_cylinder": ((partition[2] & 0xC0) << 2) | parti= tion[3], + "type": partition[4], + "end_head": partition[5], + "end_sector": partition[6] & 0x3F, + "end_cylinder": ((partition[6] & 0xC0) << 2) | partiti= on[7], + "start_lba": int.from_bytes(partition[8:12], "little"), + "size": int.from_bytes(partition[12:16], "little"), + } + ) + + def __str__(self): + return "\n".join( + [f"{i}: {partition}" for i, partition in enumerate(self.partit= ion_table)] + ) + + +class FatBootSector: + def __init__(self, data: bytes): + assert len(data) =3D=3D 512 + self.bytes_per_sector =3D int.from_bytes(data[11:13], "little") + self.sectors_per_cluster =3D data[13] + self.reserved_sectors =3D int.from_bytes(data[14:16], "little") + self.fat_count =3D data[16] + self.root_entries =3D int.from_bytes(data[17:19], "little") + self.media_descriptor =3D data[21] + self.fat_size =3D int.from_bytes(data[22:24], "little") + self.sectors_per_fat =3D int.from_bytes(data[22:24], "little") + self.sectors_per_track =3D int.from_bytes(data[24:26], "little") + self.heads =3D int.from_bytes(data[26:28], "little") + self.hidden_sectors =3D int.from_bytes(data[28:32], "little") + self.total_sectors =3D int.from_bytes(data[32:36], "little") + self.drive_number =3D data[36] + self.volume_id =3D int.from_bytes(data[39:43], "little") + self.volume_label =3D data[43:54].decode("ascii").strip() + self.fs_type =3D data[54:62].decode("ascii").strip() + + def root_dir_start(self): + """ + Calculate the start sector of the root directory. + """ + return self.reserved_sectors + self.fat_count * self.sectors_per_f= at + + def root_dir_size(self): + """ + Calculate the size of the root directory in sectors. + """ + return ( + self.root_entries * DIRENTRY_SIZE + self.bytes_per_sector - 1 + ) // self.bytes_per_sector + + def data_sector_start(self): + """ + Calculate the start sector of the data region. + """ + return self.root_dir_start() + self.root_dir_size() + + def first_sector_of_cluster(self, cluster: int): + """ + Calculate the first sector of the given cluster. + """ + return self.data_sector_start() + (cluster - 2) * self.sectors_per= _cluster + + def cluster_bytes(self): + """ + Calculate the number of bytes in a cluster. + """ + return self.bytes_per_sector * self.sectors_per_cluster + + def __str__(self): + return ( + f"Bytes per sector: {self.bytes_per_sector}\n" + f"Sectors per cluster: {self.sectors_per_cluster}\n" + f"Reserved sectors: {self.reserved_sectors}\n" + f"FAT count: {self.fat_count}\n" + f"Root entries: {self.root_entries}\n" + f"Total sectors: {self.total_sectors}\n" + f"Media descriptor: {self.media_descriptor}\n" + f"Sectors per FAT: {self.sectors_per_fat}\n" + f"Sectors per track: {self.sectors_per_track}\n" + f"Heads: {self.heads}\n" + f"Hidden sectors: {self.hidden_sectors}\n" + f"Drive number: {self.drive_number}\n" + f"Volume ID: {self.volume_id}\n" + f"Volume label: {self.volume_label}\n" + f"FS type: {self.fs_type}\n" + ) + + +class FatDirectoryEntry: + def __init__(self, data: bytes, sector: int, offset: int): + self.name =3D data[0:8].decode("ascii").strip() + self.ext =3D data[8:11].decode("ascii").strip() + self.attributes =3D data[11] + self.reserved =3D data[12] + self.create_time_tenth =3D data[13] + self.create_time =3D int.from_bytes(data[14:16], "little") + self.create_date =3D int.from_bytes(data[16:18], "little") + self.last_access_date =3D int.from_bytes(data[18:20], "little") + high_cluster =3D int.from_bytes(data[20:22], "little") + self.last_mod_time =3D int.from_bytes(data[22:24], "little") + self.last_mod_date =3D int.from_bytes(data[24:26], "little") + low_cluster =3D int.from_bytes(data[26:28], "little") + self.cluster =3D (high_cluster << 16) | low_cluster + self.size_bytes =3D int.from_bytes(data[28:32], "little") + + # extra (to help write back to disk) + self.sector =3D sector + self.offset =3D offset + + def as_bytes(self) -> bytes: + return ( + self.name.ljust(8, " ").encode("ascii") + + self.ext.ljust(3, " ").encode("ascii") + + self.attributes.to_bytes(1, "little") + + self.reserved.to_bytes(1, "little") + + self.create_time_tenth.to_bytes(1, "little") + + self.create_time.to_bytes(2, "little") + + self.create_date.to_bytes(2, "little") + + self.last_access_date.to_bytes(2, "little") + + (self.cluster >> 16).to_bytes(2, "little") + + self.last_mod_time.to_bytes(2, "little") + + self.last_mod_date.to_bytes(2, "little") + + (self.cluster & 0xFFFF).to_bytes(2, "little") + + self.size_bytes.to_bytes(4, "little") + ) + + def whole_name(self): + if self.ext: + return f"{self.name}.{self.ext}" + else: + return self.name + + def __str__(self): + return ( + f"Name: {self.name}\n" + f"Ext: {self.ext}\n" + f"Attributes: {self.attributes}\n" + f"Reserved: {self.reserved}\n" + f"Create time tenth: {self.create_time_tenth}\n" + f"Create time: {self.create_time}\n" + f"Create date: {self.create_date}\n" + f"Last access date: {self.last_access_date}\n" + f"Last mod time: {self.last_mod_time}\n" + f"Last mod date: {self.last_mod_date}\n" + f"Cluster: {self.cluster}\n" + f"Size: {self.size_bytes}\n" + ) + + def __repr__(self): + # convert to dict + return str(vars(self)) + + +class Fat16: + def __init__( + self, + start_sector: int, + size: int, + sector_reader: callable, + sector_writer: callable, + ): + self.start_sector =3D start_sector + self.size_in_sectors =3D size + self.sector_reader =3D sector_reader + self.sector_writer =3D sector_writer + + self.boot_sector =3D FatBootSector(self.sector_reader(start_sector= )) + + fat_size_in_sectors =3D self.boot_sector.fat_size * self.boot_sect= or.fat_count + self.fats =3D self.read_sectors( + self.boot_sector.reserved_sectors, fat_size_in_sectors + ) + self.fats_dirty_sectors =3D set() + + def read_sectors(self, start_sector: int, num_sectors: int) -> bytes: + return self.sector_reader(start_sector + self.start_sector, num_se= ctors) + + def write_sectors(self, start_sector: int, data: bytes): + return self.sector_writer(start_sector + self.start_sector, data) + + def directory_from_bytes( + self, data: bytes, start_sector: int + ) -> List[FatDirectoryEntry]: + """ + Convert `bytes` into a list of `FatDirectoryEntry` objects. + Will ignore long file names. + Will stop when it encounters a 0x00 byte. + """ + + entries =3D [] + for i in range(0, len(data), DIRENTRY_SIZE): + entry =3D data[i : i + DIRENTRY_SIZE] + + current_sector =3D start_sector + (i // SECTOR_SIZE) + current_offset =3D i % SECTOR_SIZE + + if entry[0] =3D=3D 0: + break + elif entry[0] =3D=3D 0xE5: + # Deleted file + continue + + if entry[11] & 0xF =3D=3D 0xF: + # Long file name + continue + + entries.append(FatDirectoryEntry(entry, current_sector, curren= t_offset)) + return entries + + def read_root_directory(self) -> List[FatDirectoryEntry]: + root_dir =3D self.read_sectors( + self.boot_sector.root_dir_start(), self.boot_sector.root_dir_s= ize() + ) + return self.directory_from_bytes(root_dir, self.boot_sector.root_d= ir_start()) + + def read_fat_entry(self, cluster: int) -> int: + """ + Read the FAT entry for the given cluster. + """ + fat_offset =3D cluster * 2 # FAT16 + return int.from_bytes(self.fats[fat_offset : fat_offset + 2], "lit= tle") + + def write_fat_entry(self, cluster: int, value: int): + """ + Write the FAT entry for the given cluster. + """ + fat_offset =3D cluster * 2 + self.fats =3D ( + self.fats[:fat_offset] + + value.to_bytes(2, "little") + + self.fats[fat_offset + 2 :] + ) + self.fats_dirty_sectors.add(fat_offset // SECTOR_SIZE) + =20 + def flush_fats(self): + """ + Write the FATs back to the disk. + """ + for sector in self.fats_dirty_sectors: + data =3D self.fats[sector * SECTOR_SIZE : (sector + 1) * SECTO= R_SIZE] + sector =3D self.boot_sector.reserved_sectors + sector + self.write_sectors(sector, data) + self.fats_dirty_sectors =3D set() + + def next_cluster(self, cluster: int) -> int | None: + """ + Get the next cluster in the chain. + If its `None`, then its the last cluster. + The function will crash if the next cluster is `FREE` (unexpected)= or invalid entry. + """ + fat_entry =3D self.read_fat_entry(cluster) + if fat_entry =3D=3D 0: + raise Exception("Unexpected: FREE cluster") + elif fat_entry =3D=3D 1: + raise Exception("Unexpected: RESERVED cluster") + elif fat_entry >=3D 0xFFF8: + return None + elif fat_entry >=3D 0xFFF7: + raise Exception("Invalid FAT entry") + else: + return fat_entry + =20 + def next_free_cluster(self) -> int: + """ + Find the next free cluster. + """ + # simple linear search + for i in range(2, 0xFFFF): + if self.read_fat_entry(i) =3D=3D 0: + return i + raise Exception("No free clusters") + + def read_cluster(self, cluster: int) -> bytes: + """ + Read the cluster at the given cluster. + """ + return self.read_sectors( + self.boot_sector.first_sector_of_cluster(cluster), + self.boot_sector.sectors_per_cluster, + ) + + def write_cluster(self, cluster: int, data: bytes): + """ + Write the cluster at the given cluster. + """ + assert len(data) =3D=3D self.boot_sector.cluster_bytes() + return self.write_sectors( + self.boot_sector.first_sector_of_cluster(cluster), + data, + ) + + def read_directory(self, cluster: int) -> List[FatDirectoryEntry]: + """ + Read the directory at the given cluster. + """ + entries =3D [] + while cluster is not None: + data =3D self.read_cluster(cluster) + entries.extend( + self.directory_from_bytes( + data, self.boot_sector.first_sector_of_cluster(cluster) + ) + ) + cluster =3D self.next_cluster(cluster) + return entries + + def update_direntry(self, entry: FatDirectoryEntry): + """ + Write the directory entry back to the disk. + """ + sector =3D self.read_sectors(entry.sector, 1) + sector =3D ( + sector[: entry.offset] + + entry.as_bytes() + + sector[entry.offset + DIRENTRY_SIZE :] + ) + self.write_sectors(entry.sector, sector) + + def find_direntry(self, path: str) -> FatDirectoryEntry | None: + """ + Find the directory entry for the given path. + """ + assert path[0] =3D=3D "/", "Path must start with /" + + path =3D path[1:] # remove the leading / + parts =3D path.split("/") + directory =3D self.read_root_directory() + + current_entry =3D None + + for i, part in enumerate(parts): + is_last =3D i =3D=3D len(parts) - 1 + + for entry in directory: + if entry.whole_name() =3D=3D part: + current_entry =3D entry + break + if current_entry is None: + return None + + if is_last: + return current_entry + else: + if current_entry.attributes & 0x10 =3D=3D 0: + raise Exception(f"{current_entry.whole_name()} is not = a directory") + else: + directory =3D self.read_directory(current_entry.cluste= r) + + def read_file(self, entry: FatDirectoryEntry) -> bytes: + """ + Read the content of the file at the given path. + """ + if entry is None: + return None + if entry.attributes & 0x10 !=3D 0: + raise Exception(f"{entry.whole_name()} is a directory") + + data =3D b"" + cluster =3D entry.cluster + while cluster is not None and len(data) <=3D entry.size_bytes: + data +=3D self.read_cluster(cluster) + cluster =3D self.next_cluster(cluster) + return data[: entry.size_bytes] + + def truncate_file(self, entry: FatDirectoryEntry, new_size: int): + """ + Truncate the file at the given path to the new size. + """ + if entry is None: + return Exception("entry is None") + if entry.attributes & 0x10 !=3D 0: + raise Exception(f"{entry.whole_name()} is a directory") + + def clusters_from_size(size: int): + return (size + self.boot_sector.cluster_bytes() - 1) // self.b= oot_sector.cluster_bytes() + + =20 + # First, allocate new FATs if we need to + required_clusters =3D clusters_from_size(new_size) + current_clusters =3D clusters_from_size(entry.size_bytes) + + affected_clusters =3D set() + + # Keep at least one cluster, easier to manage this way + if required_clusters =3D=3D 0: + required_clusters =3D 1 + if current_clusters =3D=3D 0: + current_clusters =3D 1 + + if required_clusters > current_clusters: + # Allocate new clusters + cluster =3D entry.cluster + to_add =3D required_clusters + for _ in range(current_clusters - 1): + to_add -=3D 1 + cluster =3D self.next_cluster(cluster) + assert required_clusters > 0, "No new clusters to allocate" + assert cluster is not None, "Cluster is None" + assert self.next_cluster(cluster) is None, "Cluster is not the= last cluster" + + # Allocate new clusters + for _ in range(to_add - 1): + new_cluster =3D self.next_free_cluster() + self.write_fat_entry(cluster, new_cluster) + self.write_fat_entry(new_cluster, 0xFFFF) + cluster =3D new_cluster + =20 + elif required_clusters < current_clusters: + # Truncate the file + cluster =3D entry.cluster + for _ in range(required_clusters - 1): + cluster =3D self.next_cluster(cluster) + assert cluster is not None, "Cluster is None" + + next_cluster =3D self.next_cluster(cluster) + # mark last as EOF + self.write_fat_entry(cluster, 0xFFFF) + # free the rest + while next_cluster is not None: + cluster =3D next_cluster + next_cluster =3D self.next_cluster(next_cluster) + self.write_fat_entry(cluster, 0) + + self.flush_fats() + + # verify number of clusters + cluster =3D entry.cluster + count =3D 0 + while cluster is not None: + count +=3D 1 + affected_clusters.add(cluster) + cluster =3D self.next_cluster(cluster) + assert count =3D=3D required_clusters, f"Expected {required_cluste= rs} clusters, got {count}" + + # update the size + entry.size_bytes =3D new_size + self.update_direntry(entry) + + # trigger every affected cluster + for cluster in affected_clusters: + first_sector =3D self.boot_sector.first_sector_of_cluster(clus= ter) + first_sector_data =3D self.read_sectors(first_sector, 1) + self.write_sectors(first_sector, first_sector_data) + + def write_file(self, entry: FatDirectoryEntry, data: bytes): + """ + Write the content of the file at the given path. + """ + if entry is None: + return Exception("entry is None") + if entry.attributes & 0x10 !=3D 0: + raise Exception(f"{entry.whole_name()} is a directory") + + data_len =3D len(data) + + self.truncate_file(entry, data_len) + + cluster =3D entry.cluster + while cluster is not None: + data_to_write =3D data[: self.boot_sector.cluster_bytes()] + last_data =3D False + if len(data_to_write) < self.boot_sector.cluster_bytes(): + last_data =3D True + old_data =3D self.read_cluster(cluster) + data_to_write +=3D old_data[len(data_to_write) :] + + self.write_cluster(cluster, data_to_write) + data =3D data[self.boot_sector.cluster_bytes() :] + if len(data) =3D=3D 0: + break + cluster =3D self.next_cluster(cluster) + + assert len(data) =3D=3D 0, "Data was not written completely, clust= ers missing" diff --git a/tests/qemu-iotests/tests/vvfat b/tests/qemu-iotests/tests/vvfat new file mode 100755 index 0000000000..e0e23d1ab8 --- /dev/null +++ b/tests/qemu-iotests/tests/vvfat @@ -0,0 +1,400 @@ +#!/usr/bin/env python3 +# group: rw vvfat +# +# Test vvfat driver implementation +# Here, we use a simple FAT16 implementation and check the behavior of the= vvfat driver. +# +# Copyright (C) 2024 Amjad Alsharafi +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import os, shutil +import iotests +from iotests import imgfmt, QMPTestCase +from fat16 import MBR, Fat16, DIRENTRY_SIZE + +filesystem =3D os.path.join(iotests.test_dir, "filesystem") + +nbd_sock =3D iotests.file_path("nbd.sock", base_dir=3Diotests.sock_dir) +nbd_uri =3D "nbd+unix:///disk?socket=3D" + nbd_sock + +SECTOR_SIZE =3D 512 + + +class TestVVFatDriver(QMPTestCase): + def setUp(self) -> None: + if os.path.exists(filesystem): + if os.path.isdir(filesystem): + shutil.rmtree(filesystem) + else: + print(f"Error: {filesystem} exists and is not a directory") + exit(1) + os.mkdir(filesystem) + + # Add some text files to the filesystem + for i in range(10): + with open(os.path.join(filesystem, f"file{i}.txt"), "w") as f: + f.write(f"Hello, world! {i}\n") + + # Add 2 large files, above the cluster size (8KB) + with open(os.path.join(filesystem, "large1.txt"), "wb") as f: + # write 'A' * 1KB, 'B' * 1KB, 'C' * 1KB, ... + for i in range(8 * 2): # two clusters + f.write(bytes([0x41 + i] * 1024)) + + with open(os.path.join(filesystem, "large2.txt"), "wb") as f: + # write 'A' * 1KB, 'B' * 1KB, 'C' * 1KB, ... + for i in range(8 * 3): # 3 clusters + f.write(bytes([0x41 + i] * 1024)) + + self.vm =3D iotests.VM() + + self.vm.add_blockdev( + self.vm.qmp_to_opts( + { + "driver": imgfmt, + "node-name": "disk", + "rw": "true", + "fat-type": "16", + "dir": filesystem, + } + ) + ) + + self.vm.launch() + + self.vm.qmp_log("block-dirty-bitmap-add", **{"node": "disk", "name= ": "bitmap0"}) + + # attach nbd server + self.vm.qmp_log( + "nbd-server-start", + **{"addr": {"type": "unix", "data": {"path": nbd_sock}}}, + filters=3D[], + ) + + self.vm.qmp_log( + "nbd-server-add", + **{"device": "disk", "writable": True, "bitmap": "bitmap0"}, + ) + + self.qio =3D iotests.QemuIoInteractive("-f", "raw", nbd_uri) + + def tearDown(self) -> None: + self.qio.close() + self.vm.shutdown() + # print(self.vm.get_log()) + shutil.rmtree(filesystem) + + def read_sectors(self, sector: int, num: int =3D 1) -> bytes: + """ + Read `num` sectors starting from `sector` from the `disk`. + This uses `QemuIoInteractive` to read the sectors into `stdout` an= d then parse the output. + """ + self.assertGreater(num, 0) + # The output contains the content of the sector in hex dump format + # We need to extract the content from it + output =3D self.qio.cmd(f"read -v {sector * SECTOR_SIZE} {num * SE= CTOR_SIZE}") + # Each row is 16 bytes long, and we are writing `num` sectors + rows =3D num * SECTOR_SIZE // 16 + output_rows =3D output.split("\n")[:rows] + + hex_content =3D "".join( + [(row.split(": ")[1]).split(" ")[0] for row in output_rows] + ) + bytes_content =3D bytes.fromhex(hex_content) + + self.assertEqual(len(bytes_content), num * SECTOR_SIZE) + + return bytes_content + + def write_sectors(self, sector: int, data: bytes): + """ + Write `data` to the `disk` starting from `sector`. + This uses `QemuIoInteractive` to write the data into the disk. + """ + + self.assertGreater(len(data), 0) + self.assertEqual(len(data) % SECTOR_SIZE, 0) + + temp_file =3D os.path.join(iotests.test_dir, "temp.bin") + with open(temp_file, "wb") as f: + f.write(data) + + self.qio.cmd(f"write -s {temp_file} {sector * SECTOR_SIZE} {len(da= ta)}") + + os.remove(temp_file) + + def init_fat16(self): + mbr =3D MBR(self.read_sectors(0)) + return Fat16( + mbr.partition_table[0]["start_lba"], + mbr.partition_table[0]["size"], + self.read_sectors, + self.write_sectors, + ) + + # Tests + + def test_fat_filesystem(self): + """ + Test that vvfat produce a valid FAT16 and MBR sectors + """ + mbr =3D MBR(self.read_sectors(0)) + + self.assertEqual(mbr.partition_table[0]["status"], 0x80) + self.assertEqual(mbr.partition_table[0]["type"], 6) + + fat16 =3D Fat16( + mbr.partition_table[0]["start_lba"], + mbr.partition_table[0]["size"], + self.read_sectors, + self.write_sectors, + ) + self.assertEqual(fat16.boot_sector.bytes_per_sector, 512) + self.assertEqual(fat16.boot_sector.volume_label, "QEMU VVFAT") + + def test_read_root_directory(self): + """ + Test the content of the root directory + """ + fat16 =3D self.init_fat16() + + root_dir =3D fat16.read_root_directory() + + self.assertEqual(len(root_dir), 13) # 12 + 1 special file + + files =3D { + "QEMU VVF.AT": 0, # special empty file + "FILE0.TXT": 16, + "FILE1.TXT": 16, + "FILE2.TXT": 16, + "FILE3.TXT": 16, + "FILE4.TXT": 16, + "FILE5.TXT": 16, + "FILE6.TXT": 16, + "FILE7.TXT": 16, + "FILE8.TXT": 16, + "FILE9.TXT": 16, + "LARGE1.TXT": 0x2000 * 2, + "LARGE2.TXT": 0x2000 * 3, + } + + for entry in root_dir: + self.assertIn(entry.whole_name(), files) + self.assertEqual(entry.size_bytes, files[entry.whole_name()]) + + def test_direntry_as_bytes(self): + """ + Test if we can convert Direntry back to bytes, so that we can writ= e it back to the disk safely. + """ + fat16 =3D self.init_fat16() + + root_dir =3D fat16.read_root_directory() + first_entry_bytes =3D fat16.read_sectors(fat16.boot_sector.root_di= r_start(), 1) + # The first entry won't be deleted, so we can compare it with the = first entry in the root directory + self.assertEqual(root_dir[0].as_bytes(), first_entry_bytes[:DIRENT= RY_SIZE]) + + def test_read_files(self): + """ + Test reading the content of the files + """ + fat16 =3D self.init_fat16() + + for i in range(10): + file =3D fat16.find_direntry(f"/FILE{i}.TXT") + self.assertIsNotNone(file) + self.assertEqual( + fat16.read_file(file), f"Hello, world! {i}\n".encode("asci= i") + ) + + # test large files + large1 =3D fat16.find_direntry("/LARGE1.TXT") + with open(os.path.join(filesystem, "large1.txt"), "rb") as f: + self.assertEqual(fat16.read_file(large1), f.read()) + + large2 =3D fat16.find_direntry("/LARGE2.TXT") + self.assertIsNotNone(large2) + with open(os.path.join(filesystem, "large2.txt"), "rb") as f: + self.assertEqual(fat16.read_file(large2), f.read()) + + def test_write_file_same_content_direct(self): + """ + Similar to `test_write_file_in_same_content`, but we write the fil= e directly clusters + and thus we don't go through the modification of direntry. + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/FILE0.TXT") + self.assertIsNotNone(file) + + data =3D fat16.read_cluster(file.cluster) + fat16.write_cluster(file.cluster, data) + + with open(os.path.join(filesystem, "file0.txt"), "rb") as f: + self.assertEqual(fat16.read_file(file), f.read()) + + def test_write_file_in_same_content(self): + """ + Test writing the same content to the file back to it + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/FILE0.TXT") + self.assertIsNotNone(file) + + self.assertEqual(fat16.read_file(file), b"Hello, world! 0\n") + + fat16.write_file(file, b"Hello, world! 0\n") + + self.assertEqual(fat16.read_file(file), b"Hello, world! 0\n") + + with open(os.path.join(filesystem, "file0.txt"), "rb") as f: + self.assertEqual(f.read(), b"Hello, world! 0\n") + + def test_modify_content_same_clusters(self): + """ + Test modifying the content of the file without changing the number= of clusters + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/FILE0.TXT") + self.assertIsNotNone(file) + + new_content =3D b"Hello, world! Modified\n" + self.assertEqual(fat16.read_file(file), b"Hello, world! 0\n") + + fat16.write_file(file, new_content) + + self.assertEqual(fat16.read_file(file), new_content) + with open(os.path.join(filesystem, "file0.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + def test_truncate_file_same_clusters_less(self): + """ + Test truncating the file without changing number of clusters + Test decreasing the file size + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/FILE0.TXT") + self.assertIsNotNone(file) + + self.assertEqual(fat16.read_file(file), b"Hello, world! 0\n") + + fat16.truncate_file(file, 5) + + new_content =3D fat16.read_file(file) + + self.assertEqual(new_content, b"Hello") + + with open(os.path.join(filesystem, "file0.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + def test_truncate_file_same_clusters_more(self): + """ + Test truncating the file without changing number of clusters + Test increase the file size + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/FILE0.TXT") + self.assertIsNotNone(file) + + self.assertEqual(fat16.read_file(file), b"Hello, world! 0\n") + + fat16.truncate_file(file, 20) + + new_content =3D fat16.read_file(file) + + # random pattern will be appended to the file, and its not always = the same + self.assertEqual(new_content[:16], b"Hello, world! 0\n") + self.assertEqual(len(new_content), 20) + + with open(os.path.join(filesystem, "file0.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + =20 + def test_write_large_file(self): + """ + Test writing a large file + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/LARGE1.TXT") + self.assertIsNotNone(file) + + # The content of LARGE1 is A * 1KB, B * 1KB, C * 1KB, ..., P * 1KB + # Lets change it to be Z * 1KB, Y * 1KB, X * 1KB, ..., K * 1KB + # without changing the number of clusters or filesize + new_content =3D b"".join([bytes([0x5A - i] * 1024) for i in range(= 16)]) + + fat16.write_file(file, new_content) + + with open(os.path.join(filesystem, "large1.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + =20 + def test_truncate_file_change_clusters_less(self): + """ + Test truncating a file by reducing the number of clusters + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/LARGE1.TXT") + self.assertIsNotNone(file) + + fat16.truncate_file(file, 1) + + self.assertEqual(fat16.read_file(file), b"A") + + with open(os.path.join(filesystem, "large1.txt"), "rb") as f: + self.assertEqual(f.read(), b"A") + + =20 + def test_write_file_change_clusters_less(self): + """ + Test truncating a file by reducing the number of clusters + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/LARGE2.TXT") + self.assertIsNotNone(file) + + new_content =3D b"Hello, world! This was a large file\n" + new_content =3D b"Z" * 8 * 1024 * 2 + + fat16.write_file(file, new_content) + + with open(os.path.join(filesystem, "large2.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + =20 + def test_write_file_change_clusters_more(self): + """ + Test truncating a file by increasing the number of clusters + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/LARGE2.TXT") + self.assertIsNotNone(file) + + new_content =3D b"Z" * 8 * 1024 * 4 + + fat16.write_file(file, new_content) + + with open(os.path.join(filesystem, "large2.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + =20 + + +if __name__ =3D=3D "__main__": + # This is a specific test for vvfat driver + iotests.main(supported_fmts=3D["vvfat"], supported_protocols=3D["file"= ]) diff --git a/tests/qemu-iotests/tests/vvfat.out b/tests/qemu-iotests/tests/= vvfat.out new file mode 100755 index 0000000000..fa16b5ccef --- /dev/null +++ b/tests/qemu-iotests/tests/vvfat.out @@ -0,0 +1,5 @@ +............. +---------------------------------------------------------------------- +Ran 13 tests + +OK --=20 2.45.0 From nobody Mon Nov 25 07:36:47 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1716717510; cv=none; d=zohomail.com; s=zohoarc; b=kuOevj6z8ivQbPAbkyS7GLNuSz4x01ygElux3PVfxkMb9xaWxZAKjXj2otn8v5gWIDS7Do6no6teadedSTvGp1nw3OCeJkN5rmnwBLF58eofsq6hBBjnS5w516HShUe+vcW9QCHAKWivzJj6Ea19y1qLOIxtbhVR5KINaMhOd7w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716717510; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=OUVFVe5lOF2BIf1OvZZwpFj89+VMBRsujzHQgWOZLYI=; b=ZXpBUMoG0OnG5Avr00UoPP1T4nIxhfxSAeTlWk6E7YFWVLm+qgiJq0tBvbuqgE2C7wcGPY7JQ8eG678IUzOdNz4NPMiQFK4zbIEUu8QRLm5JQOwFMO9uHg3GYXPCjQAPm8GQtFcBOVS3Ty4VZCXkzT3y5fdblTVczAjd8R70Wt0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1716717510196132.06717075442896; Sun, 26 May 2024 02:58:30 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sBAcX-0006HJ-UJ; Sun, 26 May 2024 05:56:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBAcV-0006GQ-UY; Sun, 26 May 2024 05:56:52 -0400 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sBAcT-0003pW-E1; Sun, 26 May 2024 05:56:51 -0400 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-6f8ec7e054dso1492045b3a.2; Sun, 26 May 2024 02:56:48 -0700 (PDT) Received: from amjad-pc.. ([192.228.203.229]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-682227f1838sm4021368a12.46.2024.05.26.02.56.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 02:56:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716717407; x=1717322207; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OUVFVe5lOF2BIf1OvZZwpFj89+VMBRsujzHQgWOZLYI=; b=i4NXCnLEDKAE3mFsuCzJSY/UO1TRycWDL6b0tzYLY27YMUOxCF2Jxz+k3Es+iyw0ej h00m2vyLmonVnCoskEfNi7yu4c5pihtFk7IRL/HiN6gTM6m4oh9tIxYmeDPZ2o+xUa3A eG5WpQzPRl9BUHofmHX0KtqXPhXO3FjVMhTAQ/NHs+fIQIQfEZfyDzrD5aLdkOq4IBoS nCHHyzF93QjRUjKEJyKVe0LV5Dg9PtMysPx5nuXA5YhsP60Luol2fd9iXohJj/197ryp 710QXE+g8fvj8vAj59ouLNoHZz4Mp2EHt937+wvqd7IutrM2sRSsevm8oUWGAO1MTy/N SAEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716717407; x=1717322207; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OUVFVe5lOF2BIf1OvZZwpFj89+VMBRsujzHQgWOZLYI=; b=fsgdwfpEsmVvbSSEJu66hUyzAOjCrFzyZ82hpYabfL76LGjKH99j/sfmz+au/3ePpO 086/eGPWAupgsB7nvxZVYZJE2gyHKTbsjvYN5z7UYn1HLEv3oZh4iyPE+xZYN+AidqjB CKGC3+7fyhQdOad+QBWtns1Ws+r8+840AUSRapbyLmlJR7jpz513r2LkQOlhJTJQrmP8 T55IUhgIOWjhnhj2iKdndfYufKjzEcCnxMGYCHBMwqYt0UdHxvj7vypTNM99rbHHaDty ACIXGmQ5mXxOrb17XFncOZN6YaeKOE75JH50A2UMbzWdDS4dxtwiE0Zxcsc3+7vQvm28 vfYw== X-Forwarded-Encrypted: i=1; AJvYcCWh985LbgmDiv2AX1yvIirjy8QjFw6h+usXrMXYfj2BWot42SoRuo5XsYf/DFgpfuv+gRigwSKt2Y5pnc2hjR2AuN1papc= X-Gm-Message-State: AOJu0YxnVqDXR8fLkHpDK141aX2TxojNF3ns7HSFWjQ8vy2RTK2dKxH5 oSvDgy+olglENdfWqDXOUCzbiLjKfexpDklKKSEADZNp5Na2q0F9aKdBems+ X-Google-Smtp-Source: AGHT+IE2p452NMO3WCrpN5BmtgryB+XOluTp2lfP/TVnEoo/JQIu70zZz8OsnSZsRo4U8GTaPer1Ng== X-Received: by 2002:a05:6a21:3115:b0:1af:d0fe:9094 with SMTP id adf61e73a8af0-1b212cc036bmr11438579637.11.1716717407097; Sun, 26 May 2024 02:56:47 -0700 (PDT) From: Amjad Alsharafi To: qemu-devel@nongnu.org Cc: Hanna Reitz , Kevin Wolf , "open list:vvfat" , Amjad Alsharafi Subject: [PATCH v3 5/6] iotests: Filter out `vvfat` fmt from failing tests Date: Sun, 26 May 2024 17:56:05 +0800 Message-ID: X-Mailer: git-send-email 2.45.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42b; envelope-from=amjadsharafi10@gmail.com; helo=mail-pf1-x42b.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1716717511920100005 Content-Type: text/plain; charset="utf-8" `vvfat` is a special format and not all tests (even generic) can run withou= t crashing. So, added `unsupported_fmt: vvfat` to all failling tests. Also added `vvfat` format into `meson.build`, vvfaat tests can be run on th= e `block-thorough` suite. Signed-off-by: Amjad Alsharafi --- .gitlab-ci.d/buildtest.yml | 1 + tests/qemu-iotests/001 | 1 + tests/qemu-iotests/002 | 1 + tests/qemu-iotests/003 | 1 + tests/qemu-iotests/005 | 1 + tests/qemu-iotests/008 | 1 + tests/qemu-iotests/009 | 1 + tests/qemu-iotests/010 | 1 + tests/qemu-iotests/011 | 1 + tests/qemu-iotests/012 | 1 + tests/qemu-iotests/021 | 1 + tests/qemu-iotests/032 | 1 + tests/qemu-iotests/033 | 1 + tests/qemu-iotests/052 | 1 + tests/qemu-iotests/094 | 1 + tests/qemu-iotests/120 | 2 +- tests/qemu-iotests/140 | 1 + tests/qemu-iotests/145 | 1 + tests/qemu-iotests/157 | 1 + tests/qemu-iotests/159 | 2 +- tests/qemu-iotests/170 | 2 +- tests/qemu-iotests/192 | 1 + tests/qemu-iotests/197 | 2 +- tests/qemu-iotests/208 | 2 +- tests/qemu-iotests/215 | 2 +- tests/qemu-iotests/236 | 2 +- tests/qemu-iotests/251 | 1 + tests/qemu-iotests/307 | 2 +- tests/qemu-iotests/308 | 2 +- tests/qemu-iotests/meson.build | 3 ++- tests/qemu-iotests/tests/export-incoming-iothread | 2 +- tests/qemu-iotests/tests/fuse-allow-other | 1 + tests/qemu-iotests/tests/mirror-ready-cancel-error | 2 +- tests/qemu-iotests/tests/regression-vhdx-log | 1 + 34 files changed, 35 insertions(+), 12 deletions(-) diff --git a/.gitlab-ci.d/buildtest.yml b/.gitlab-ci.d/buildtest.yml index cfdff175c3..a46c179a6b 100644 --- a/.gitlab-ci.d/buildtest.yml +++ b/.gitlab-ci.d/buildtest.yml @@ -347,6 +347,7 @@ build-tcg-disabled: 124 132 139 142 144 145 151 152 155 157 165 194 196 200 202 208 209 216 218 227 234 246 247 248 250 254 255 257 258 260 261 262 263 264 270 272 273 277 279 image-fleecing + - ./check -vvfat vvfat =20 build-user: extends: .native_build_job_template diff --git a/tests/qemu-iotests/001 b/tests/qemu-iotests/001 index 6f980fd34d..cf905b5d00 100755 --- a/tests/qemu-iotests/001 +++ b/tests/qemu-iotests/001 @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic =20 =20 diff --git a/tests/qemu-iotests/002 b/tests/qemu-iotests/002 index 5ce1647531..1e557fad8c 100755 --- a/tests/qemu-iotests/002 +++ b/tests/qemu-iotests/002 @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic _unsupported_imgopts "subformat=3DstreamOptimized" =20 diff --git a/tests/qemu-iotests/003 b/tests/qemu-iotests/003 index 03f902a83c..6e74f1faeb 100755 --- a/tests/qemu-iotests/003 +++ b/tests/qemu-iotests/003 @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic _unsupported_imgopts "subformat=3DstreamOptimized" =20 diff --git a/tests/qemu-iotests/005 b/tests/qemu-iotests/005 index ba377543b0..28ae66bfcd 100755 --- a/tests/qemu-iotests/005 +++ b/tests/qemu-iotests/005 @@ -41,6 +41,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic _supported_os Linux _unsupported_imgopts "subformat=3DtwoGbMaxExtentFlat" \ diff --git a/tests/qemu-iotests/008 b/tests/qemu-iotests/008 index fa4990b513..80850ecf12 100755 --- a/tests/qemu-iotests/008 +++ b/tests/qemu-iotests/008 @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic =20 =20 diff --git a/tests/qemu-iotests/009 b/tests/qemu-iotests/009 index efa852bad3..408617b0bc 100755 --- a/tests/qemu-iotests/009 +++ b/tests/qemu-iotests/009 @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic _unsupported_imgopts "subformat=3DstreamOptimized" =20 diff --git a/tests/qemu-iotests/010 b/tests/qemu-iotests/010 index 4ae9027b47..c9f6279255 100755 --- a/tests/qemu-iotests/010 +++ b/tests/qemu-iotests/010 @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic _unsupported_imgopts "subformat=3DstreamOptimized" =20 diff --git a/tests/qemu-iotests/011 b/tests/qemu-iotests/011 index 5c99ac987f..92039fa949 100755 --- a/tests/qemu-iotests/011 +++ b/tests/qemu-iotests/011 @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic _unsupported_imgopts "subformat=3DstreamOptimized" =20 diff --git a/tests/qemu-iotests/012 b/tests/qemu-iotests/012 index 3a24d2ca8d..5b0f1338e6 100755 --- a/tests/qemu-iotests/012 +++ b/tests/qemu-iotests/012 @@ -40,6 +40,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto file =20 # Remove once all tests are fixed to use TEST_IMG_FILE diff --git a/tests/qemu-iotests/021 b/tests/qemu-iotests/021 index 0fc89df2fe..475f9b2116 100755 --- a/tests/qemu-iotests/021 +++ b/tests/qemu-iotests/021 @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic =20 =20 diff --git a/tests/qemu-iotests/032 b/tests/qemu-iotests/032 index ebbe7cb0ba..b58141f132 100755 --- a/tests/qemu-iotests/032 +++ b/tests/qemu-iotests/032 @@ -42,6 +42,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 =20 # This works for any image format (though unlikely to segfault for raw) _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic _unsupported_imgopts "subformat=3DstreamOptimized" =20 diff --git a/tests/qemu-iotests/033 b/tests/qemu-iotests/033 index 4bc7a071bd..6410c8717e 100755 --- a/tests/qemu-iotests/033 +++ b/tests/qemu-iotests/033 @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic _unsupported_imgopts "subformat=3DstreamOptimized" =20 diff --git a/tests/qemu-iotests/052 b/tests/qemu-iotests/052 index 2f23ac9b65..5b3545c8b9 100755 --- a/tests/qemu-iotests/052 +++ b/tests/qemu-iotests/052 @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto file =20 # Don't do O_DIRECT on tmpfs diff --git a/tests/qemu-iotests/094 b/tests/qemu-iotests/094 index 4766e9a458..d8da955c1b 100755 --- a/tests/qemu-iotests/094 +++ b/tests/qemu-iotests/094 @@ -42,6 +42,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.qemu =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto nbd _unsupported_imgopts "subformat=3DmonolithicFlat" "subformat=3DtwoGbMaxExt= entFlat" =20 diff --git a/tests/qemu-iotests/120 b/tests/qemu-iotests/120 index ac7bd8c4e3..d8e5f4241a 100755 --- a/tests/qemu-iotests/120 +++ b/tests/qemu-iotests/120 @@ -40,7 +40,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 =20 _supported_fmt generic _supported_proto file fuse -_unsupported_fmt luks +_unsupported_fmt luks vvfat _require_drivers raw =20 _make_test_img 64M diff --git a/tests/qemu-iotests/140 b/tests/qemu-iotests/140 index d923b777e2..42a96d9097 100755 --- a/tests/qemu-iotests/140 +++ b/tests/qemu-iotests/140 @@ -45,6 +45,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.qemu =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto file fuse _supported_os Linux =20 diff --git a/tests/qemu-iotests/145 b/tests/qemu-iotests/145 index a2ce92516d..ff9c6ff54f 100755 --- a/tests/qemu-iotests/145 +++ b/tests/qemu-iotests/145 @@ -39,6 +39,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic =20 _make_test_img 1M diff --git a/tests/qemu-iotests/157 b/tests/qemu-iotests/157 index aa2ebbfb4b..419d3b8b7a 100755 --- a/tests/qemu-iotests/157 +++ b/tests/qemu-iotests/157 @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto file =20 _require_devices virtio-blk diff --git a/tests/qemu-iotests/159 b/tests/qemu-iotests/159 index 4eb476d3a8..70a1079ae5 100755 --- a/tests/qemu-iotests/159 +++ b/tests/qemu-iotests/159 @@ -39,7 +39,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 =20 _supported_fmt generic _supported_proto file -_unsupported_fmt luks +_unsupported_fmt luks vvfat =20 TEST_SIZES=3D"5 512 1024 1999 1K 64K 1M" =20 diff --git a/tests/qemu-iotests/170 b/tests/qemu-iotests/170 index 41387e4d66..f08fb0e8bd 100755 --- a/tests/qemu-iotests/170 +++ b/tests/qemu-iotests/170 @@ -39,7 +39,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 =20 _supported_fmt generic _supported_proto file -_unsupported_fmt luks +_unsupported_fmt luks vvfat =20 echo echo "=3D=3D Creating image =3D=3D" diff --git a/tests/qemu-iotests/192 b/tests/qemu-iotests/192 index e66e1a4f06..ca72b0b7c8 100755 --- a/tests/qemu-iotests/192 +++ b/tests/qemu-iotests/192 @@ -42,6 +42,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.qemu =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto file =20 if [ "$QEMU_DEFAULT_MACHINE" !=3D "pc" ]; then diff --git a/tests/qemu-iotests/197 b/tests/qemu-iotests/197 index 69849c800e..76b30672d9 100755 --- a/tests/qemu-iotests/197 +++ b/tests/qemu-iotests/197 @@ -53,7 +53,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 _supported_fmt generic _supported_proto generic # LUKS support may be possible, but it complicates things. -_unsupported_fmt luks +_unsupported_fmt luks vvfat _unsupported_imgopts "subformat=3DstreamOptimized" =20 echo diff --git a/tests/qemu-iotests/208 b/tests/qemu-iotests/208 index 6117f165fa..f08c83c0c1 100755 --- a/tests/qemu-iotests/208 +++ b/tests/qemu-iotests/208 @@ -23,7 +23,7 @@ =20 import iotests =20 -iotests.script_initialize(supported_fmts=3D['generic']) +iotests.script_initialize(supported_fmts=3D['generic'], unsupported_fmts= =3D['vvfat']) =20 with iotests.FilePath('disk.img') as disk_img_path, \ iotests.FilePath('disk-snapshot.img') as disk_snapshot_img_path, \ diff --git a/tests/qemu-iotests/215 b/tests/qemu-iotests/215 index 6babbcdc1f..3bd03c741e 100755 --- a/tests/qemu-iotests/215 +++ b/tests/qemu-iotests/215 @@ -50,7 +50,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 _supported_fmt generic _supported_proto generic # LUKS support may be possible, but it complicates things. -_unsupported_fmt luks +_unsupported_fmt luks vvfat _unsupported_imgopts "subformat=3DstreamOptimized" =20 echo diff --git a/tests/qemu-iotests/236 b/tests/qemu-iotests/236 index 20419bbb9e..4bcca355ab 100755 --- a/tests/qemu-iotests/236 +++ b/tests/qemu-iotests/236 @@ -23,7 +23,7 @@ import iotests from iotests import log =20 -iotests.script_initialize(supported_fmts=3D['generic']) +iotests.script_initialize(supported_fmts=3D['generic'], unsupported_fmts= =3D['vvfat']) size =3D 64 * 1024 * 1024 granularity =3D 64 * 1024 =20 diff --git a/tests/qemu-iotests/251 b/tests/qemu-iotests/251 index 794cad58b2..ac83f69d9a 100755 --- a/tests/qemu-iotests/251 +++ b/tests/qemu-iotests/251 @@ -39,6 +39,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.qemu =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto file _supported_os Linux _unsupported_imgopts "subformat=3DstreamOptimized" diff --git a/tests/qemu-iotests/307 b/tests/qemu-iotests/307 index b429b5aa50..548d2f040d 100755 --- a/tests/qemu-iotests/307 +++ b/tests/qemu-iotests/307 @@ -27,7 +27,7 @@ import os # luks which requires special command lines) iotests.script_initialize( supported_fmts=3D['generic'], - unsupported_fmts=3D['luks', 'vpc'], + unsupported_fmts=3D['luks', 'vpc', 'vvfat'], supported_platforms=3D['linux'], ) =20 diff --git a/tests/qemu-iotests/308 b/tests/qemu-iotests/308 index ea81dc496a..993dd2a5f9 100755 --- a/tests/qemu-iotests/308 +++ b/tests/qemu-iotests/308 @@ -47,7 +47,7 @@ if [ "$IMGOPTSSYNTAX" =3D "true" ]; then fi # We need the image to have exactly the specified size, and VPC does # not allow that by default -_unsupported_fmt vpc +_unsupported_fmt vpc vvfat =20 _supported_proto file # We create the FUSE export manually _supported_os Linux # We need /dev/urandom diff --git a/tests/qemu-iotests/meson.build b/tests/qemu-iotests/meson.build index fad340ad59..e87cf71fc4 100644 --- a/tests/qemu-iotests/meson.build +++ b/tests/qemu-iotests/meson.build @@ -23,7 +23,8 @@ qemu_iotests_formats =3D { 'raw': 'slow', 'qed': 'thorough', 'vmdk': 'thorough', - 'vpc': 'thorough' + 'vpc': 'thorough', + 'vvfat': 'thorough', } =20 foreach k, v : emulators diff --git a/tests/qemu-iotests/tests/export-incoming-iothread b/tests/qemu= -iotests/tests/export-incoming-iothread index d36d6194e0..9535046dfd 100755 --- a/tests/qemu-iotests/tests/export-incoming-iothread +++ b/tests/qemu-iotests/tests/export-incoming-iothread @@ -75,5 +75,5 @@ class TestExportIncomingIothread(iotests.QMPTestCase): =20 if __name__ =3D=3D '__main__': iotests.main(supported_fmts=3D['generic'], - unsupported_fmts=3D['luks'], # Would need a secret + unsupported_fmts=3D['luks', 'vvfat'], # Would need a secr= et supported_protocols=3D['file']) diff --git a/tests/qemu-iotests/tests/fuse-allow-other b/tests/qemu-iotests= /tests/fuse-allow-other index 19f494aefb..6cfbe9ef1f 100755 --- a/tests/qemu-iotests/tests/fuse-allow-other +++ b/tests/qemu-iotests/tests/fuse-allow-other @@ -38,6 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ../common.qemu =20 _supported_fmt generic +_unsupported_fmt vvfat =20 _supported_proto file # We create the FUSE export manually =20 diff --git a/tests/qemu-iotests/tests/mirror-ready-cancel-error b/tests/qem= u-iotests/tests/mirror-ready-cancel-error index ed2e46447e..3b36764ecb 100755 --- a/tests/qemu-iotests/tests/mirror-ready-cancel-error +++ b/tests/qemu-iotests/tests/mirror-ready-cancel-error @@ -138,5 +138,5 @@ class TestMirrorReadyCancelError(iotests.QMPTestCase): if __name__ =3D=3D '__main__': # LUKS would require special key-secret handling in add_blockdevs() iotests.main(supported_fmts=3D['generic'], - unsupported_fmts=3D['luks'], + unsupported_fmts=3D['luks', 'vvfat'], supported_protocols=3D['file']) diff --git a/tests/qemu-iotests/tests/regression-vhdx-log b/tests/qemu-iote= sts/tests/regression-vhdx-log index ca264e93d6..eb216c27dd 100755 --- a/tests/qemu-iotests/tests/regression-vhdx-log +++ b/tests/qemu-iotests/tests/regression-vhdx-log @@ -40,6 +40,7 @@ cd .. . ./common.filter =20 _supported_fmt generic +_unsupported_fmt vvfat _supported_proto generic _unsupported_imgopts "subformat=3DstreamOptimized" =20 --=20 2.45.0 From nobody Mon Nov 25 07:36:47 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1716717505; cv=none; d=zohomail.com; s=zohoarc; b=G1cHMT/RUC+v7dGFIRPNXt80kcM0wApfPpvuiByEfxfxPHBC6ZkGyk9Ih2MqnLPlQSeOXOS+0E5USIxUCj76J5r9dFV77CPRRO9l6nrPG+WWkMZYdIPhsiLe/ihvOhQdRr6M8ogVTPXjwO36Mj7/UqD23HsAW35niLmRlD/GLdM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1716717505; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=tSbMFsmslcfQACtPzQzMghuQaxr5NDYpNXTcVsfGjAU=; b=Gn3e2mYUWmQPwQRyGk2EZZ4T5VK0xOV2sVc73wjKC25K7lz0UG4ZkNMH+tEDYFwY+USr3MSJkEdSlByMj01+u3PsldQX6z/XT/dYooIChbg+nescIKlNkFDavj23yZHih6eNyZLdca02rMp8pBUlpgJKkDZEM+1fZYWiMKgXrwk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 171671750586012.976301716336138; Sun, 26 May 2024 02:58:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sBAce-0006KQ-6E; Sun, 26 May 2024 05:57:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sBAcc-0006JR-Hl; Sun, 26 May 2024 05:56:58 -0400 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sBAcV-0003pc-V0; Sun, 26 May 2024 05:56:58 -0400 Received: by mail-ot1-x336.google.com with SMTP id 46e09a7af769-6f855b2499cso1675970a34.1; Sun, 26 May 2024 02:56:50 -0700 (PDT) Received: from amjad-pc.. ([192.228.203.229]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-682227f1838sm4021368a12.46.2024.05.26.02.56.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 02:56:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716717409; x=1717322209; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tSbMFsmslcfQACtPzQzMghuQaxr5NDYpNXTcVsfGjAU=; b=aWBJ/uAqPQ3Bbi9FmhUAwdQ6g5K8/2O2QpfcVCmvUhGRxrva5coQseaNRzlh9lB2WT pjXmaU1O1x/lGMhsJjO7qkWgET34juRgT93AwLviqFgULFNAD+BleUwIM+/F4nMaZeVv uWsPID9pQvnx+Z/qPqJgHsrRse1CRxxFEIpZZr2+8XLgOARZr8rWdpcag3iRCd4SQPm7 98kPjbZmiHogTRQotwVQCDjpDHS1xXbZvqyFYn47yDdAT3TT3f9Jmon4morwj/cbSjzj ZuqTseSX8H9UXL1vPs7z3ScENx5Kv0HEhONMrW/2KxlUK+QJAj/eoU6Z445+axOG/+sG Khzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716717409; x=1717322209; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tSbMFsmslcfQACtPzQzMghuQaxr5NDYpNXTcVsfGjAU=; b=XCVeZHdC+PcZHhVkjRDpPZbyv46WHkz9Dy5gDkLzdwbXhwWGheOfR0meQDgbWHdnE6 Y2jjcYEWLDunyuJyI+tOKiGuKp048cL/HdM2pu4ghVj0YppNItqzE7hJJOsefOGztrff R5aHAnW9Y8px1Y5A4UenAGFW/jm95zqdu1rzJLmvwaUgcAWhe0LwBGtom/CJkPnP2edP vrOQndmsQEEQsQyvRFPZEswDC1lks5whMyPnevyZmaKtjKfOqam5DAkGCvUV+dJoDncR FsbP+kuuCwJ7uZXmZVquD3fJW9Au8s0V9U0uFbJSMBLuaLUrlCs8cU4EiH9cooZqVAi9 zNVA== X-Forwarded-Encrypted: i=1; AJvYcCW510tut0Id2d4bOzBg6YuYdTpd6Gq60tzT/UrscrWaAGikejHDAB71pUqo/obVQtbSlN+JXArHI2tEo2o0fxXCjNMfCbA= X-Gm-Message-State: AOJu0Ywg5D6VBpl0A3lxig0lH4qiD8KAWP3O9imtkR1DATepi18yHT06 SZxSep1WTvnQYwklvv3la56EJRF3e8XRZItxk9JPstbUWqZfCfrVUtxHm3l+ X-Google-Smtp-Source: AGHT+IFbt8X2EKHda3ZHbJUVt2P6mPBbnXa9Uu7sY0sFEKedg8bgJO5YFPN1B6CULm46nWMSY8Xhhw== X-Received: by 2002:a05:6830:148d:b0:6f0:e470:1915 with SMTP id 46e09a7af769-6f8d0b22c30mr7300501a34.24.1716717409154; Sun, 26 May 2024 02:56:49 -0700 (PDT) From: Amjad Alsharafi To: qemu-devel@nongnu.org Cc: Hanna Reitz , Kevin Wolf , "open list:vvfat" , Amjad Alsharafi Subject: [PATCH v3 6/6] iotests: Add `create_file` test for `vvfat` driver Date: Sun, 26 May 2024 17:56:06 +0800 Message-ID: <3e3e3887ddd9f95cba952fad6b5fb1040e126140.1716717181.git.amjadsharafi10@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::336; envelope-from=amjadsharafi10@gmail.com; helo=mail-ot1-x336.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1716717507701100002 Content-Type: text/plain; charset="utf-8" We test the ability to create new files in the filesystem, this is done by adding an entry in the desired directory list. The file will also be created in the host filesystem with matching filename. Signed-off-by: Amjad Alsharafi --- tests/qemu-iotests/fat16.py | 124 +++++++++++++++++++++++++++-- tests/qemu-iotests/tests/vvfat | 29 +++++-- tests/qemu-iotests/tests/vvfat.out | 4 +- 3 files changed, 144 insertions(+), 13 deletions(-) diff --git a/tests/qemu-iotests/fat16.py b/tests/qemu-iotests/fat16.py index 6ac5508d8d..e86bdd0b10 100644 --- a/tests/qemu-iotests/fat16.py +++ b/tests/qemu-iotests/fat16.py @@ -16,9 +16,11 @@ # along with this program. If not, see . =20 from typing import List +import string =20 SECTOR_SIZE =3D 512 DIRENTRY_SIZE =3D 32 +ALLOWED_FILE_CHARS =3D set("!#$%&'()-@^_`{}~" + string.digits + string.asc= ii_uppercase) =20 =20 class MBR: @@ -265,7 +267,7 @@ def write_fat_entry(self, cluster: int, value: int): + self.fats[fat_offset + 2 :] ) self.fats_dirty_sectors.add(fat_offset // SECTOR_SIZE) - =20 + def flush_fats(self): """ Write the FATs back to the disk. @@ -293,7 +295,7 @@ def next_cluster(self, cluster: int) -> int | None: raise Exception("Invalid FAT entry") else: return fat_entry - =20 + def next_free_cluster(self) -> int: """ Find the next free cluster. @@ -338,6 +340,67 @@ def read_directory(self, cluster: int) -> List[FatDire= ctoryEntry]: cluster =3D self.next_cluster(cluster) return entries =20 + def add_direntry(self, cluster: int | None, name: str, ext: str, attri= butes: int): + """ + Add a new directory entry to the given cluster. + If the cluster is `None`, then it will be added to the root direct= ory. + """ + + def find_free_entry(data: bytes): + for i in range(0, len(data), DIRENTRY_SIZE): + entry =3D data[i : i + DIRENTRY_SIZE] + if entry[0] =3D=3D 0 or entry[0] =3D=3D 0xE5: + return i + return None + + assert len(name) <=3D 8, "Name must be 8 characters or less" + assert len(ext) <=3D 3, "Ext must be 3 characters or less" + assert attributes % 0x15 !=3D 0x15, "Invalid attributes" + + # initial dummy data + new_entry =3D FatDirectoryEntry(b"\0" * 32, 0, 0) + new_entry.name =3D name.ljust(8, " ") + new_entry.ext =3D ext.ljust(3, " ") + new_entry.attributes =3D attributes + new_entry.reserved =3D 0 + new_entry.create_time_tenth =3D 0 + new_entry.create_time =3D 0 + new_entry.create_date =3D 0 + new_entry.last_access_date =3D 0 + new_entry.last_mod_time =3D 0 + new_entry.last_mod_date =3D 0 + new_entry.cluster =3D self.next_free_cluster() + new_entry.size_bytes =3D 0 + + # mark as EOF + self.write_fat_entry(new_entry.cluster, 0xFFFF) + + if cluster is None: + for i in range(self.boot_sector.root_dir_size()): + sector_data =3D self.read_sectors( + self.boot_sector.root_dir_start() + i, 1 + ) + offset =3D find_free_entry(sector_data) + if offset is not None: + new_entry.sector =3D self.boot_sector.root_dir_start()= + i + new_entry.offset =3D offset + self.update_direntry(new_entry) + return new_entry + else: + while cluster is not None: + data =3D self.read_cluster(cluster) + offset =3D find_free_entry(data) + if offset is not None: + new_entry.sector =3D self.boot_sector.first_sector_of_= cluster( + cluster + ) + (offset // SECTOR_SIZE) + new_entry.offset =3D offset % SECTOR_SIZE + self.update_direntry(new_entry) + return new_entry + cluster =3D self.next_cluster(cluster) + + raise Exception("No free directory entries") + def update_direntry(self, entry: FatDirectoryEntry): """ Write the directory entry back to the disk. @@ -406,9 +469,10 @@ def truncate_file(self, entry: FatDirectoryEntry, new_= size: int): raise Exception(f"{entry.whole_name()} is a directory") =20 def clusters_from_size(size: int): - return (size + self.boot_sector.cluster_bytes() - 1) // self.b= oot_sector.cluster_bytes() + return ( + size + self.boot_sector.cluster_bytes() - 1 + ) // self.boot_sector.cluster_bytes() =20 - =20 # First, allocate new FATs if we need to required_clusters =3D clusters_from_size(new_size) current_clusters =3D clusters_from_size(entry.size_bytes) @@ -438,7 +502,7 @@ def clusters_from_size(size: int): self.write_fat_entry(cluster, new_cluster) self.write_fat_entry(new_cluster, 0xFFFF) cluster =3D new_cluster - =20 + elif required_clusters < current_clusters: # Truncate the file cluster =3D entry.cluster @@ -464,7 +528,9 @@ def clusters_from_size(size: int): count +=3D 1 affected_clusters.add(cluster) cluster =3D self.next_cluster(cluster) - assert count =3D=3D required_clusters, f"Expected {required_cluste= rs} clusters, got {count}" + assert ( + count =3D=3D required_clusters + ), f"Expected {required_clusters} clusters, got {count}" =20 # update the size entry.size_bytes =3D new_size @@ -505,3 +571,49 @@ def write_file(self, entry: FatDirectoryEntry, data: b= ytes): cluster =3D self.next_cluster(cluster) =20 assert len(data) =3D=3D 0, "Data was not written completely, clust= ers missing" + + def create_file(self, path: str): + """ + Create a new file at the given path. + """ + assert path[0] =3D=3D "/", "Path must start with /" + + path =3D path[1:] # remove the leading / + + parts =3D path.split("/") + + directory_cluster =3D None + directory =3D self.read_root_directory() + + parts, filename =3D parts[:-1], parts[-1] + + for i, part in enumerate(parts): + current_entry =3D None + for entry in directory: + if entry.whole_name() =3D=3D part: + current_entry =3D entry + break + if current_entry is None: + return None + + if current_entry.attributes & 0x10 =3D=3D 0: + raise Exception(f"{current_entry.whole_name()} is not a di= rectory") + else: + directory =3D self.read_directory(current_entry.cluster) + directory_cluster =3D current_entry.cluster + + # add new entry to the directory + + filename, ext =3D filename.split(".") + + if len(ext) > 3: + raise Exception("Ext must be 3 characters or less") + if len(filename) > 8: + raise Exception("Name must be 8 characters or less") + + for c in filename + ext: + + if c not in ALLOWED_FILE_CHARS: + raise Exception("Invalid character in filename") + + return self.add_direntry(directory_cluster, filename, ext, 0) diff --git a/tests/qemu-iotests/tests/vvfat b/tests/qemu-iotests/tests/vvfat index e0e23d1ab8..d8d802589d 100755 --- a/tests/qemu-iotests/tests/vvfat +++ b/tests/qemu-iotests/tests/vvfat @@ -323,7 +323,7 @@ class TestVVFatDriver(QMPTestCase): =20 with open(os.path.join(filesystem, "file0.txt"), "rb") as f: self.assertEqual(f.read(), new_content) - =20 + def test_write_large_file(self): """ Test writing a large file @@ -342,7 +342,7 @@ class TestVVFatDriver(QMPTestCase): =20 with open(os.path.join(filesystem, "large1.txt"), "rb") as f: self.assertEqual(f.read(), new_content) - =20 + def test_truncate_file_change_clusters_less(self): """ Test truncating a file by reducing the number of clusters @@ -359,7 +359,6 @@ class TestVVFatDriver(QMPTestCase): with open(os.path.join(filesystem, "large1.txt"), "rb") as f: self.assertEqual(f.read(), b"A") =20 - =20 def test_write_file_change_clusters_less(self): """ Test truncating a file by reducing the number of clusters @@ -376,7 +375,7 @@ class TestVVFatDriver(QMPTestCase): =20 with open(os.path.join(filesystem, "large2.txt"), "rb") as f: self.assertEqual(f.read(), new_content) - =20 + def test_write_file_change_clusters_more(self): """ Test truncating a file by increasing the number of clusters @@ -392,7 +391,27 @@ class TestVVFatDriver(QMPTestCase): =20 with open(os.path.join(filesystem, "large2.txt"), "rb") as f: self.assertEqual(f.read(), new_content) - =20 + + def test_create_file(self): + """ + Test creating a new file + """ + fat16 =3D self.init_fat16() + + new_file =3D fat16.create_file("/NEWFILE.TXT") + + self.assertIsNotNone(new_file) + self.assertEqual(new_file.size_bytes, 0) + + new_content =3D b"Hello, world! New file\n" + fat16.write_file(new_file, new_content) + + self.assertEqual(fat16.read_file(new_file), new_content) + + with open(os.path.join(filesystem, "newfile.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + # TODO: support deleting files =20 =20 if __name__ =3D=3D "__main__": diff --git a/tests/qemu-iotests/tests/vvfat.out b/tests/qemu-iotests/tests/= vvfat.out index fa16b5ccef..6323079e08 100755 --- a/tests/qemu-iotests/tests/vvfat.out +++ b/tests/qemu-iotests/tests/vvfat.out @@ -1,5 +1,5 @@ -............. +.............. ---------------------------------------------------------------------- -Ran 13 tests +Ran 14 tests =20 OK --=20 2.45.0