網頁

2013年3月26日

Bypass the 2GB file size limit on 32-bit Linux

在 32 位元的 Linux 上面寫超過 2GB 的檔案會發生錯誤,有時候你會發現不管怎麼寫資料,檔案的大小都是一個 magic number 2,147,483,647,有時候甚至導致程式意外的終止執行,簡直是摸不清楚頭緒。

這是因為 Linux 的系統內部處理檔案時用的指標定義為 long,而 long 在 32 位元的系統上的大小為 32 位元,因此最大只能支援 2^31-1 = 2,147,483,647 bits 等於是 2GB 扣掉 1 bite 的檔案大小。

64 位元的系統 (例如 AMD64 或 IA64) 則因為 long 定義成 64 位元,所以不會有問題。因此在 32 位元的 Linux 上面,程式需要作一些額外處理才能正確寫超過 2GB 的檔案。

首先我們先準備一個簡單的測試檔 test.cpp,大致上就是透過 fprintf 每一次輸出 3 bits 的資料,然後迴圈的個數是 1 Giga 次,也就是我們希望可以輸出 3 bits * 1 Giga = 3.0 Giga 的資料量,用這個來測試我們是否可以成功突破 linux 上 2.0 G 的檔案大小限制。

diff and patch

Linux 中 Diff 命令的功能為逐行比較兩個文字檔案,列出其不同之處。它對給出的檔進行系統的檢查,並顯示出兩個檔中所有不同的行,不要求事先對檔進行排序。

用 diff 工具可以將兩個檔案的差異性輸出成一個補丁檔。有了這個補丁檔,我們可以利用 patch 工具幫助我們從舊檔案升級成新檔案,或者從新檔案還原成舊檔案。所以 diff 和 patch 搭配在一起,威力無窮啊。

首先我們要做個小實驗,準備兩個文字檔叫做 old 和 new,檔案內容分別如下

Valgrind: a powerful memory profiling tool

Install

apt-get install valgrind

Usage

valgrind --leak-check=full --log-file=vglog ./a.out

Reference

wwwsqldesigner: Visual web-based SQL modelling tool

WWW SQL Designer allows users to create database designs, which can be saved/loaded and exported to SQL scripts. Various databases and languages are supported. Ability to import existing database design.

About

Hi and welcome to WWW SQL Designer! This tool allows you to draw and create database schemas (E-R diagrams) directly in browser, without the need for any external programs (flash). You only need JavaScript enabled. The Designer works perfectly in Mozillas (Firefox, Seamonkey), Internet Explorers (6, 7, 8), Safari and Operas. Konqueror works, but the experience is limited.

Many database features are supported, such as keys, foreign key constraints, comments and indexes. You can either save your design (for further loading & modifications), print it or export as SQL script. It is possible to retrieve (import) schema from existing database.

Install IMDbPY

Install Tools

apt-get install python-setuptools
apt-get install python-dev
apt-get install libmysqlclient15-dev
easy_install -U SQLObject
easy_install MySQL-python
easy_install psycopg2

  • SQLObject is a popular Object Relational Manager for providing an object interface to your database, with tables as classes, rows as instances, and columns as attributes.
  • MySQL-python is the most popular MySQL adapter for the Python
  • Psycopg2 is the most popular PostgreSQL adapter for the Python