`
standalone
  • 浏览: 616884 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

How to use fallocate system call

    博客分类:
  • c++
阅读更多
/*
 * fallocate - utility to use the fallocate system call
 *
 * Copyright (C) 2008 Red Hat, Inc. All rights reserved.
 * Written by Eric Sandeen <sandeen@redhat.com>
 *
 * cvtnum routine taken from xfsprogs,
 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
 *
 * 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.
 *
 * This program is distributed in the hope that it would 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, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

#include <linux/falloc.h>

void usage(void)
{
	printf("Usage: fallocate [-n] [-o offset] -l length filename\n");
	exit(EXIT_FAILURE);
}

#define EXABYTES(x)     ((long long)(x) << 60)
#define PETABYTES(x)    ((long long)(x) << 50)
#define TERABYTES(x)    ((long long)(x) << 40)
#define GIGABYTES(x)    ((long long)(x) << 30)
#define MEGABYTES(x)    ((long long)(x) << 20)
#define KILOBYTES(x)    ((long long)(x) << 10)

long long
cvtnum(char *s)
{
	long long	i;
	char		*sp;
	int		c;

	i = strtoll(s, &sp, 0);
	if (i == 0 && sp == s)
		return -1LL;
	if (*sp == '\0')
		return i;
	if (sp[1] != '\0')
		return -1LL;

	c = tolower(*sp);
	switch (c) {
	case 'k':
		return KILOBYTES(i);
	case 'm':
		return MEGABYTES(i);
	case 'g':
		return GIGABYTES(i);
	case 't':
		return TERABYTES(i);
	case 'p':
		return PETABYTES(i);
	case 'e':
		return  EXABYTES(i);
	}

	return -1LL;
}

int main(int argc, char **argv)
{
	int	fd;
	char	*fname;
	int	opt;
	loff_t	length = -2LL;
	loff_t	offset = 0;
	int	falloc_mode = 0;
	int	error;

	while ((opt = getopt(argc, argv, "nl:o:")) != -1) {
		switch(opt) {
		case 'n':
			/* do not change filesize */
			falloc_mode = FALLOC_FL_KEEP_SIZE;
			break;
		case 'l':
			length = cvtnum(optarg);
			break;
		case 'o':
			offset = cvtnum(optarg);
			break;
		default:
			usage();
		}
	}

	if (length == -2LL) {
		printf("Error: no length argument specified\n");
		usage();
	}

	if (length <= 0) {
		printf("Error: invalid length value specified\n");
		usage();
	}

	if (offset < 0) {
		printf("Error: invalid offset value specified\n");
		usage();
	}

	if (optind == argc) {
		printf("Error: no filename specified\n");
		usage();
	}

	fname = argv[optind++];

	/* Should we create the file if it doesn't already exist? */
	fd = open(fname, O_WRONLY|O_CREAT);
	if (fd < 0) {
		perror("Error opening file");
		exit(EXIT_FAILURE);
	}

	error = fallocate(fd, falloc_mode, offset, length);
	// error = syscall(SYS_fallocate, fd, falloc_mode, offset, length);

	if (error < 0) {
		perror("fallocate failed");
		exit(EXIT_FAILURE);
	}

	close(fd);
	return 0;
}

 

分享到:
评论

相关推荐

    How To Use REF Cursors in JDBC Program

    cstmt = conn.prepareCall("{call test_ref_cursor.test_ref_cursor(?, ?)}"); ``` #### 步骤3:注册REF Cursor输出参数 为了能够接收存储过程返回的REF Cursor,我们需要使用`registerOutParameter`方法注册输出...

    Undocumented Windows NT 带图完整英文版chm

    HOW TO USE THE CALLGATE TECHNIQUE PAGING ISSUES SUMMARY Chapter 11: Portable Executable File Format OVERVIEW OF A PE FILE STRUCTURE OF A PE FILE RELATIVE VIRTUAL ADDRESS DETAILS OF THE PE ...

    Undocumented Windows NT 英文完整版chm

    HOW TO USE THE CALLGATE TECHNIQUE PAGING ISSUES SUMMARY Chapter 11: Portable Executable File Format OVERVIEW OF A PE FILE STRUCTURE OF A PE FILE RELATIVE VIRTUAL ADDRESS DETAILS OF THE PE ...

    UE(官方下载)

    How to use TaskMatch Environments in UltraEdit and UEStudio Configure FTP backup Save a local copy of your files when you transfer them to FTP directories Encrypt and Decrypt Text Files Use UltraEdit ...

    Introduction to 64Bit Windows Assembly

    The book also discusses how to use the free integrated development environment, ebe, designed by the author specifically to meet the needs of assembly language programmers. Ebe is a C++ program which...

    System Hardware Information Finder

    We will make use of RegQueryValueEx API call to get the information. LONG RegQueryValueEx( HKEY hKey, // handle to key to query LPTSTR lpValueName, // address of name of value to query LPDWORD ...

    VB编程资源大全(英文源码 API)

    Complete source included to demonstrate how to translate a long to red, green and blue values&lt;END&gt;&lt;br&gt;35,sleep2.zip This is a small project showing how to use the Sleep API call from within your ...

    Android代码-anycall

    This project can run as root to call system service IPC from normal apps. You can find the native part here Use This module simple demonstrates how to call the PowerManager.goToSleep(long uptimeMillis...

    A Gentle Introduction to ROS .pdf

    In which we call services and respond to service requests. 9 Recording and replayingmessages 129 In which we use bag files to record and replaymessages. 10 Conclusion 137 In which we preview some ...

    微软内部资料-SQL性能优化2

    Application memory tuning provides more of the computer's virtual memory to applications by providing less virtual memory to the operating system. Although a system having less than 2 GB of physical ...

    IRI2012电离层模型 matlab代码

    An example script for how to use the function is given as iritest.m. This is also the script that generated the attached screenshot. It took a little less than 15 minutes to run on my computer. As...

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    In this chapter, you’ll explore the foundation of debugging, namely, a system call responsible for a process attaching itself to another process: ptrace. 14. Dynamic Frameworks With dynamic ...

    Python: Penetration Testing for Developers

    Familiarize yourself with the generation of Metasploit resource files and use the Metasploit Remote Procedure Call to automate exploit generation and execution Exploit the Remote File Inclusion to ...

    Introduction to EECS I (MIT)

    - **Definition:** How to define and call procedures. - **Procedure Calls:** Understanding how parameters are passed and returned. - **Non-Local References:** Working with variables outside the scope ...

    VB编程资源大全(英文源码 表单)

    The sample application includes how to use the Windows API for doing file operations, putting an application into the system tray&lt;END&gt;&lt;br&gt;51,cwindowontop.zip This little routine will, if included ...

    vld-2.5.1-setup.exe

    It's pretty easy to use. After installing it, you just need to tell Visual C++ where to find the included header and library file. Then it can be used with any C/C++ project simply by adding the ...

    C Programming

    - **Pointers to Structures**: How to use pointers to access and manipulate the fields of structures. - **Defining Your Own Types**: Explanation of how to create custom data types using `typedef`. ###...

    Learning Penetration Testing with Python(PACKT,2015)

    Use the Metasploit Remote Procedure Call (MSFRPC) to automate exploit generation and execution Use Python’s Scrapy, network, socket, office, Nmap libraries, and custom modules Parse Microsoft Office ...

    Introduction to WinAPI

    All programming languages that can call to such functions and to operate these types of data in the programs executed under Windows, can use this API. In particular, the languages C ++, Pascal, ...

Global site tag (gtag.js) - Google Analytics