grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Lustig <peter.lustig.7db@gmail.com>
To: grub-devel@gnu.org
Subject: Re: Re: New command to check NT's hibernation state
Date: Fri, 02 Dec 2011 00:16:49 -0500	[thread overview]
Message-ID: <4ED85F41.5070605@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 799 bytes --]

> Thanks for this. However the coding style isn't according to our 
> standards. Could you adjust to the standard? I recommend looking at 
> other code and reading GNU Coding Standard. E.g. we don't use {0} 
> initialiser, return value of commands, C++ comments or M$-$tyle 
> variables. 

Sorry about that.  I've reviewed the standard and perused the source 
tree, as you suggested, to determine the best style.  Attached is my 
updated file.

Glad to be of help,
Peter Lustig

P.S.  Do you know if there are any plans in the future to implement 
write operations for filesystems?  (I noticed raw disk writing is 
currently supported.)  If not, is the reason due to the technical 
challenges (e.g. NTFS only had read-only access in GNU/Linux for quite a 
while) or just a general lack of interest?

[-- Attachment #2: nthibr.c --]
[-- Type: text/plain, Size: 4206 bytes --]

/* nthibr.c - tests whether an MS Windows system partition is hibernated */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2007,2008,2009,2011  Free Software Foundation, Inc.
 *
 *  GRUB 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/types.h>
#include <grub/disk.h>
#include <grub/file.h>
#include <grub/misc.h>
#include <grub/dl.h>
#include <grub/extcmd.h>
#include <grub/lib/arg.h>
#include <grub/err.h>
#include <grub/i18n.h>

GRUB_MOD_LICENSE("GPLv3+");

/* Define this 'empty' array to let the '-h' and '-u' switches be processed */
static const struct grub_arg_option options[] = {
  {0, 0, 0, 0, 0, 0}
};

static grub_err_t 
grub_cmd_nthibr (grub_extcmd_context_t ctxt __attribute__ ((unused)),
                 int argc, char **args)
{
  grub_err_t status = GRUB_ERR_NONE;
  char *partition_name, hibr_file_path[32], hibr_file_magic[5];
  grub_ssize_t length;
  grub_disk_t partition;
  grub_file_t hibr_file = 0;

# define ABORT(_code, _message)                     \
    do                                              \
      {                                             \
        status = grub_error ((_code), (_message));  \
        goto exit;                                  \
      }                                             \
    while (0)

  /* Check argument count */
  if (!argc)
    ABORT( GRUB_ERR_BAD_ARGUMENT, N_("too few arguments specified") );
  else if (argc > 1)
    ABORT( GRUB_ERR_BAD_ARGUMENT, N_("too many arguments specified") );

  partition_name = args[0];
  length = (grub_ssize_t) grub_strlen (partition_name);

  /* Check if partition specifier 'looks right' */
  if (partition_name[0] != '(' || partition_name[length - 1] != ')')
    ABORT( GRUB_ERR_BAD_FILENAME, N_("invalid partition specifier") );

  /* Check if partition actually exists */
  partition_name[length - 1] = '\0';
  partition = grub_disk_open (partition_name + 1);
  if (!partition)
    ABORT( GRUB_ERR_UNKNOWN_DEVICE, N_("partition not found") );
  else 
    {
      grub_disk_close (partition);
      partition_name[length - 1] = ')';
    }

  /* Build path to 'hiberfil.sys' */
  grub_strncpy (hibr_file_path, partition_name, sizeof (hibr_file_path) - 1);
  grub_strncat (hibr_file_path, "/hiberfil.sys", 
                sizeof (hibr_file_path) - grub_strlen (hibr_file_path) - 1);

  /* Try to open 'hiberfil.sys' */
  hibr_file = grub_file_open (hibr_file_path);
  if (!hibr_file)
    ABORT( GRUB_ERR_FILE_NOT_FOUND, N_("'hiberfil.sys' not found") );

  /* Try to read magic number of 'hiberfil.sys' */
  length = (grub_ssize_t) (sizeof (hibr_file_magic) - 1);
  grub_memset (hibr_file_magic, 0, sizeof (hibr_file_magic));
  if (grub_file_read (hibr_file, hibr_file_magic, length) < length)
    ABORT( GRUB_ERR_BAD_FILE_TYPE, N_("'hiberfil.sys' too small") );

  /* Return SUCCESS if magic indicates file is active; else return FAILURE */
  if (!grub_strncasecmp ("hibr", hibr_file_magic, length))
    grub_puts (N_("The system is hibernated."));
  else 
    {
      grub_puts (N_("The system is NOT hibernated."));
      status = GRUB_ERR_TEST_FAILURE;
    }

exit: 
  /* Ensure 'hiberfil.sys' is closed */
  if (hibr_file)
    grub_file_close (hibr_file);

# undef ABORT
  return status;
}
\f
static grub_extcmd_t cmd;

GRUB_MOD_INIT (nthibr) 
{
  cmd = grub_register_extcmd ("nthibr", grub_cmd_nthibr, 0,
                              N_("DEVICE"),
                              N_("Test whether an NT system partition "
                                 "is hibernated."), 
                              options);
}

GRUB_MOD_FINI (nthibr) 
{
  grub_unregister_extcmd (cmd);
}

             reply	other threads:[~2011-12-02  5:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-02  5:16 Peter Lustig [this message]
2011-12-02  6:05 ` New command to check NT's hibernation state Vladimir 'φ-coder/phcoder' Serbinenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4ED85F41.5070605@gmail.com \
    --to=peter.lustig.7db@gmail.com \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).